Title Link: Http://codeforces.com/problemset/problem/295/A
Test instructions
Give you an array of n numbers, and an operation of M-shaped such as (L, R, v), representing the array from the first l to the R all plus V; Then the operation of K-shaped as (L, R) is given, which represents for this m operation, which performs the first operation of the first to the first B operations each time.
Ideas:
This problem can be decomposed into two same sub-problems, set the original array as a, the first action is B, the second action is C, you can see that B is for a array from L to R Plus V, and the C array is the number of executions of B from L to R plus 1. These two problems are similar, setting a property to B CNT represents how many times this command is executed, and is not executed at first, so initialize to 0, set auxiliary array tp[i] =b[i].cnt-b[i-1].cnt; then array b from L to R plus 1 is equivalent to tp[l]++, Tp[r + 1]--; Finally, from 1 to m the array b executes b[i].cnt = b[i-1].cnt + tp[i]. At this point, the complexity of O (N) can be used for Operation B, and how many times each of its operations is performed. And then, using the same method, set an auxiliary array of tp[i] = a[i]-a[i-1] to the array a from L to R Plus V, to execute the CNT, Just as with the execution tp[l] + = cnt * V, tp[r + 1]-= cnt * V, and so on after all operations are completed, again for array a recovery, from 1 to N, array a execution a[i] = A[i-1] + b[i]. Then you can (n + m) to solve the problem in time complexity.
Code:
1#include <bits/stdc++.h>2 3 using namespacestd;4typedefLong LongLL;5 6 Const intMAXN =100000;7 8 structOpera {LL L; LL R; LL v; LL CNT;};//The first operation, L R is the cumulative value for the left and right interval V, and CNT represents the number of times this operation was performed9Opera OPA[MAXN +3];Ten One intMain () { AIos_base::sync_with_stdio (0); Cin.tie (0); - intN, M, K; CIN >> n >> M >>K; -LL ARV[MAXN +3] = {0}; the for(inti =1; I <= N; i++) Cin >>Arv[i]; - for(inti =1; I <= m; i++) Cin >> opa[i].l >> OPA[I].R >>opa[i].v; -LL BOP[MAXN +3] = {0};//Bop[i] = opa[i].v-opa[i-1].v, opa[].v initialized to 0, so this array is not initialized - for(inti =0; I < K; i++) { + intL, R; CIN >> L >>R; -bop[l]++, Bop[r +1]--;//The array opa[].v from L to R plus 1, which is equivalent to bop[l]++, Bop[r + 1]--. + } A for(inti =1; I <= m; i++) opa[i].cnt = opa[i-1].cnt + bop[i];//you can get the number of times for each operation by restoring it backwards. atLL BA[MAXN +3] = {0};//Ba[i] = a[i]-a[i-1] - for(inti =1; I <= N; i++) Ba[i] = arv[i]-arv[i-1];//a[] Each value is indeterminate and needs to be initialized with an array of BA - for(inti =1; I <= m; i++) {//an equivalent transformation of an array a from OPA[I].L to OPA[I].R Plus V, and to perform CNT times -ba[OPA[I].L] + = OPA[I].V *opa[i].cnt; -ba[OPA[I].R +1]-= OPA[I].V *opa[i].cnt; - } in for(inti =1; I <= N; i++) Arv[i] = arv[i-1] + ba[i];//Change back Again - for(inti =1; I <= N; i++) cout << arv[i] << (i = = n?)'\ n':' '); to return 0; +}
Codeforces 295A. Greg and Array