The subject is the most basic segmented tree operation. or generally called line tree, but it seems to have nothing to do with the line, just segmented.
Without using the lazy flag, the update is just a single point of updating.
If you do not use a segmented tree, then the update time efficiency requires only O (1), and using the segmented tree update efficiency requires O (LGN).
But not with the segmented tree, the time efficiency of the query is O (n), and the segmentation tree query efficiency is O (LGN)
This is the time amortize apportioned, and lgn really fast, the data is not very large time, close to the constant.
Therefore, we need to use a segmented tree.
#include <cstdio>class enemyinfo{static const int SIZE = 50001;int segtree[size<<2];inline int lchild (int r) { Return r<<1; }inline int rchild (int r) {return r<<1|1;} void pushup (int root) {Segtree[root] = Segtree[lchild (root)] + segtree[rchild (root)];} void Buildtree (int l, int r, int rt) {if (L = = r) {scanf ("%d", &segtree[rt]); return;} int m = l + ((r-l) >>1) Buildtree (L, M, Lchild (RT)), Buildtree (m+1, R, Rchild (RT));p Ushup (RT);} void Update (int addpoint, int addnum, int l, int r, int rt) {if (L = = r) {Segtree[rt] + = Addnum;return;} int m = l + ((r-l) >>1), if (Addpoint <= m) Update (Addpoint, Addnum, L, M, Lchild (RT)); Else update (Addpoint, Addnum , M+1, R, Rchild (RT));p Ushup (RT);} int query (const int l, const int R, int L, int r, int rt) {if (L <= l && R <= R) return segtree[rt];int m = L + ((r-l) >>1); int res = 0;if (l <= m) Res + = query (l, R, L, M, Lchild (RT)), if (R > m) Res + = query (l, R, M+1, R, Rchild (RT)); return res;} Public: Enemyinfo () {int T, n, A, b;scanf ("%d", &t), for (int T = 1; t <= t; t + +) {printf ("Case%d:\n", T), scanf ("%d", &n ); Buildtree (1, N, 1); Char Op[6];while (scanf ("%s", op) && op[0]! = ' E ') {scanf ("%d%d", &a,&b); if (op[0] = = ' Q ') printf ("%d\n", query (A, B, 1, N, 1)), else if (op[0] = = ' S ') update (A,-B, 1, n, 1); Else update (A, B, 1, N, 1);}}};