You have n integers, A1, A2 ,..., One. You need to process two types of operations. One type of operation is to add some given numbers at a given interval. The other is the sum of the required quantities at a given interval.
Difficulties: the lazy mark is mainly used to identify the problems. In fact, the lazy mark is that not all the currently changed values are updated. It will be updated when used, which saves a lot of time.
Question link: http://poj.org/problem? Id = 3468
Code:
# Include <stdio. h> # include <string. h> # define maxn 100005 # define lc l, M, RT <1 # define RC m + 1, R, RT <1 | 1 # define ll _ int64ll sum [maxn <2], add [maxn <2]; // The add array is the void pushup (int rt) to be added for temporary storage) {sum [RT] = sum [RT <1] + sum [RT <1 | 1];} void Pushdown (int rt, int m) // This is difficult, after understanding this question, it is almost the same {If (add [RT]) {Add [RT <1] + = add [RT]; add [RT <1 | 1] + = add [RT]; sum [RT <1] + = add [RT] * (m-(M> 1 )); sum [RT <1 | 1] + = add [RT] * (M> 1); add [RT] = 0 ;}} void creat (INT l, int R, int RT) {Add [RT] = 0; If (L = r) {scanf ("% i64d", & sum [RT]); return ;} int M = (L + r)> 1; creat (LC); creat (RC); pushup (RT);} void Update (INT le, int Ri, int num, int L, int R, int RT) {If (Le <= L & R <= RI) {Add [RT] + = num; sum [RT] + = (LL) num * (R-l + 1); return;} Pushdown (RT, R-l + 1 ); int M = (L + r)> 1; if (Le <= m) Update (Le, RI, num, LC); If (RI> m) Update (Le, ri, num, RC); pushup (RT);} ll query (INT le, int Ri, int L, int R, int RT) {If (Le <= L & R <= RI) {return sum [RT];} Pushdown (RT, R-l + 1); LL res = 0; ll M = (L + r)> 1; if (Le <= m) RES + = query (Le, RI, LC); If (RI> m) res + = query (Le, RI, RC); Return res;} int main () {int n, m; scanf ("% d", & N, & M); creat (1, n, 1); char s [2]; int A, B, C; while (M --) {scanf ("% s ", s); If (s [0] = 'q') {scanf ("% d", & A, & B ); printf ("% i64d \ n", query (A, B, 1, n, 1);} else {scanf ("% d", &, & B, & C); Update (a, B, c, 1, n, 1) ;}} return 0 ;}