Greg has an array a = a1, a2, ..., an and m operations. Each operation looks as: li, ri, di, (1 ≤ li ≤ ri ≤ n). To apply operation i to the array means to increase all array elements with numbers li, li + 1, ..., ri by value di.
Greg wrote down k queries on a piece of paper. Each query has the following form: xi, yi, (1 ≤ xi ≤ yi ≤ m). That means that one should apply operations with numbers xi, xi + 1, ..., yi to the array.
Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg.
Input
The first line contains integers n, m, k (1 ≤ n, m, k ≤ 105). The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 105) — the initial array.
Next m lines contain operations, the operation number i is written as three integers: li, ri, di, (1 ≤ li ≤ ri ≤ n), (0 ≤ di ≤ 105).
Next k lines contain the queries, the query number i is written as two integers: xi, yi, (1 ≤ xi ≤ yi ≤ m).
The numbers in the lines are separated by single spaces.
Output
On a single line print n integers a1, a2, ..., an — the array after executing all the queries. Separate the printed numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.
We need to compute the final array after applying multiple range increments defined by operations, and queries that specify ranges of operations to apply multiple times.
Given:
n: size of array a (initial values)
m operations: each operation i has l_i, r_i, d_i. Applying operation means for each j from l_i to r_i, a_j += d_i.
k queries: each query gives x_i, y_i meaning we need to apply all operations from x_i to y_i (inclusive) to the array. Queries are executed sequentially? The problem says: "Greg wrote down k queries on a piece of paper. That means that one should apply operations with numbers xi, xi+1, ..., yi to the array. Now Greg is wondering, what the array a will be after all the queries are executed." So we have to apply all queries. The order of queries? They are given sequentially, and we should apply them in that order. Because each query is a batch of operations to apply. So we have to process each query, and each query triggers a set of operations (range of operations). Effectively, the total effect is: For each operation j (1..m), it may be applied multiple times (its count = number of queries that include it). So we need to compute for each operation j, how many times it is applied (let's call cnt_op[j]), then the final array is: initial a_i + sum over operations j of cnt_op[j] * d_j, but only where l_j <= i <= r_j. So it's like: a[i] += sum_{j: i in [l_j, r_j]} d_j * cnt_op[j].
Thus we need to compute cnt_op for each operation based on the queries (ranges). Queries are intervals on the operations' indices. Each query (x, y) says "apply operations from x to y". So that means we need to increment count for each operation j in [x, y] by 1, for each query. So total cnt_op = sum over queries of indicator(j in [x, y]). So it's a classic problem: we have k intervals on [1..m], we need for each point j in 1..m, the number of intervals covering it.
Given constraints up to 1e5, we can compute cnt_op using difference array: create an array diff of size m+2 initialized to 0. For each query (x, y), diff[x]++, diff[y+1]--. Then prefix sum to get cnt_op[j] for each operation j.
But wait: is it that simple? Since the queries are applied sequentially, is there any order dependence? Because each operation's application adds d_i to the array. If we sum them linearly, it's commutative: total addition to each array element is sum over operations of (cnt_op[j] * d_j) over intervals that include that element. Since addition is commutative and associative, the order does not matter. So indeed we can compute total counts per operation and then apply once with multiplicities.
Thus step 1: compute cnt_op[1..m] as described.
Step 2: Now we need to apply these operations (each with weight cnt_op[j]) to the array. Each operation is a range addition: add d_i * cnt_op[i] to all elements in [l_i, r_i]. We have up to 1e5 operations, each is a range update. We need to compute final array of size up to 1e5. So we can again use difference array for the array. For each operation i, we add val = d_i * cnt_op[i] to range [l_i, r_i]. But d_i up to 1e5, cnt_op[i] up to k (1e5), product up to 1e10, fits in 64-bit. So we can handle with long long.
We can create diff_a of size n+2, initially zero. For each operation i, diff_a[l_i] += val, diff_a[r_i+1] -= val. After processing all operations, compute prefix sum to get added values per element, then add to initial a_i. That yields final array.
Complexities: O(n + m + k).
But is there any nuance? Note that cnt_op[i] might be large, but we can compute.
Example 2:
n=1,m=1,k=1
a=[1]
op1: 1 1 1
query:1 1 -> cnt1=1
val=1 range[1,1] -> add 1 => final 2.
Example 3: we can test quickly.
So solution is straightforward.
But we must be careful about integer overflow: use 64-bit (long long) for counts and values. Also diff arrays need long long.
Implementation steps:
Read n, m, k.
Read initial array a (size n, store as long long).
Read m operations: store l[i], r[i], d[i] (1-indexed). d can be int.
Read k queries: for each query (x, y), update diff_op[x] += 1, diff_op[y+1] -= 1. diff_op size m+2.
Compute cnt_op array (size m+1, use long long). Iterate i from 1 to m: cnt += diff_op[i]; cnt_op[i] = cnt.
Initialize diff_arr (size n+2) to 0 (long long).
For each operation i from 1 to m: if cnt_op[i] > 0: val = d[i] * cnt_op[i]; diff_arr[l[i]] += val; diff_arr[r[i]+1] -= val.
Compute prefix sum over diff_arr to get add[i] for each index i from 1 to n: cur += diff_arr[i]; a[i] += cur.
Output a[1..n].
Time O(n+m+k), memory O(n+m).
Alternative: could combine steps using prefix sums over operations directly? But this is fine.
Edge cases: k=0? then cnt_op all zero, array unchanged. m=0? But operations exist? Input says m≥1? Actually constraints: 1 ≤ n, m, k ≤ 10^5, so m at least 1. So operations always exist.
Thus answer.
But wait: Could there be a nuance: Queries are applied sequentially, but the operations within a query are applied one after another. However, since addition is linear, the total effect is the sum of contributions. So it's fine.
Therefore solution as described.
Now we need to produce the final answer.
Now I'll write the reasoning and solution for the problem.
But the user asked "Please, do not use the %lld specifier ...". That's a note for C++ but we're solving algorithmically.
Now I'll produce the final answer: a simple explanation and the code.
But the problem is from Codeforces (likely 276C? Actually it's "Little Girl and Problem on Queries"? Not exactly. This is problem "Greg and Array" maybe? Actually I recall a Codeforces problem named "Greg and Array". Yes, it's Codeforces 295A? Wait 295A is "Greg and Array"? Let me check: There is a problem 296C? Actually I think it's 295A "Greg and Array". It has similar description. So solution is as described.
Thus final answer.
Now produce solution explanation and then code (C++). I'll include necessary includes, using namespace std, define long long as ll, and reading with scanf? But the problem says not to use %lld? Actually it says: "Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier." So if using scanf/printf, one could use %I64d. But using cin/cout is simpler. Since constraints are 1e5, cin/cout with sync may be okay, but we can also use scanf/printf with %I64d. I'll use scanf/printf with %I64d for speed and to follow note.
But careful: In C++, if using %I64d, it's for Windows; but on Codeforces it's fine. Alternatively use long long and cin/cout with ios_base::sync_with_stdio(false). Both are acceptable.
I'll choose scanf/printf.
Implementation:
Read n,m,k: int.
vector a(n+1) (1-indexed).
For i=1..n: scanf("%lld", &a[i])? Actually a[i] can be up to 1e5, fits in int, but after additions it may be large, so use long long.
vector l(m+1), r(m+1), d(m+1).
For i=1..m: scanf("%d %d %d", &l[i], &r[i], &d[i]).
vector diff_op(m+2, 0) (since we need up to m+1).
For each query: int x,y; scanf("%d %d", &x, &y); diff_op[x]++; diff_op[y+1]--.
Compute cnt_op(m+1,0). long long cnt=0; for i=1..m: cnt+=diff_op[i]; cnt_op[i]=cnt;
vector diff_arr(n+2,0);
for i=1..m: if(cnt_op[i]) { long long val = (long long)d[i] * cnt_op[i]; diff_arr[l[i]] += val; diff_arr[r[i]+1] -= val; }
long long cur=0; for i=1..n: cur += diff_arr[i]; a[i] += cur;
Print a[i] for i=1..n.
Complexities okay.
Testing on given examples.
Now produce final answer.
But note: The problem statement says: "Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier." Actually it says "or the %I64d specifier". So using %I64d is acceptable. But also using cin/cout is fine. I'll just use cin/cout with sync for simplicity. But due to large input, we can speed up.
I'll write:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
vector a(n+1);
for (int i=1; i<=n; ++i) cin >> a[i];
vector l(m+1), r(m+1), d(m+1);
for (int i=1; i<=m; ++i) cin >> l[i] >> r[i] >> d[i];
vector diff_op(m+2, 0);
for (int i=0; i<k; ++i) {
int x, y;
cin >> x >> y;
diff_op[x] += 1;
diff_op[y+1] -= 1;
}
vector cnt_op(m+1, 0);
long long cur = 0;
for (int i=1; i<=m; ++i) {
cur += diff_op[i];
cnt_op[i] = cur;
}
vector diff_arr(n+2, 0);
for (int i=1; i<=m; ++i) {
if (cnt_op[i] == 0) continue;
long long val = (long long) d[i] * cnt_op[i];
diff_arr[l[i]] += val;
diff_arr[r[i]+1] -= val;
}
cur = 0;
for (int i=1; i<=n; ++i) {
cur += diff_arr[i];
a[i] += cur;
}
for (int i=1; i<=n; ++i) {
cout << a[i] << (i==n ? "\n" : " ");
}
return 0;
}
That should pass.
Now I'll produce final answer in a concise manner. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
code
int n, m, k;
cin >> n >> m >> k;
vector<long long> a(n + 1);
for (int i = 1; i <= n; ++i)
cin >> a[i];
vector<int> l(m + 1), r(m + 1), d(m + 1);
for (int i = 1; i <= m; ++i)
cin >> l[i] >> r[i] >> d[i];
// ---------- 1. compute how many times each operation is used ----------
vector<long long> opDiff(m + 2, 0);
for (int i = 0; i < k; ++i) {
int x, y;
cin >> x >> y;
opDiff[x]++;
opDiff[y + 1]--;
}
vector<long long> cntOp(m + 1, 0);
long long cur = 0;
for (int i = 1; i <= m; ++i) {
cur += opDiff[i];
cntOp[i] = cur;
}
// ---------- 2. apply operations with their multiplicities ----------
vector<long long> arrDiff(n + 2, 0);
for (int i = 1; i <= m; ++i) {
if (cntOp[i] == 0) continue;
long long val = (long long)d[i] * cntOp[i];
arrDiff[l[i]] += val;
arrDiff[r[i] + 1] -= val;
}
cur = 0;
for (int i = 1; i <= n; ++i) {
cur += arrDiff[i];
a[i] += cur;
}
// ---------- output ----------
for (int i = 1; i <= n; ++i)
cout << a[i] << " \n"[i == n];
return 0;