Question: enemy troops
Standard line segment tree template code:
# Include <cstdio> # include <cstring> const int maxn = 500000 + 10; struct node {int left, right, count;} node [maxn]; int A [maxn]; /*************************************** **************************** I is the interval number ************** L is the left boundary of the interval I, R is the right boundary of range I. *** it starts from 1 to n and ends when the Interval Length is 1 ***, that is, L = R. The Count record interval and ************************************ **/void maketree (INT l, int R, int I) {node [I]. left = L; node [I]. right = r; If (L = r) {node [I]. count = A [l]; return;} int M = (L + r)/2; maketree (L, M, 2 * I); maketree (m + 1, R, 2 * I + 1); node [I]. count = node [2 * I]. count + node [2 * I + 1]. count ;} /*************************************** ************************************ I interval serial number, x: the vertex to be updated. Y: the value to be updated ********************* * ******************************/void updatetree (int I, int X, int y, int flag) {int L = node [I]. left; int r = node [I]. right; int M = (L + r)/2; If (r = L) {If (FLAG) node [I]. count + = y; else node [I]. count-= y; return;} If (x <= m) updatetree (2 * I, X, Y, flag); else updatetree (2 * I + 1, x, y, flag); If (FLAG) node [I]. count + = y; else node [I]. count-= y; return ;} /*************************************** **************************** * **********************/INT querytree (INT l, int R, int I) {int M = (node [I]. left + node [I]. right)/2; If (node [I]. right <= R & node [I]. left> = L) return node [I]. count; int ans = 0; If (r <= m) return querytree (L, R, 2 * I); else if (L> m) return querytree (L, R, 2 * I + 1); else return querytree (L, M, 2 * I) + querytree (m + 1, R, 2 * I + 1);} int main () {int t, n; char STR [20]; scanf ("% d", & T); For (INT I = 1; I <= T; I ++) {printf ("case % d: \ n", I); scanf ("% d", & N); For (INT I = 1; I <= N; I ++) scanf ("% d", & A [I]); maketree (1, n, 1); int X, Y; while (scanf ("% s", STR) {If (STR [0] = 'E') break; scanf ("% d", & X, & Y); If (STR [0] = 'q') printf ("% d \ n", querytree (X, Y, 1 )); else if (STR [0] = 'A') updatetree (1, x, y, true); else updatetree (1, x, y, false );}} return 0 ;}
Beautiful line segment Tree Code:
# Include <cstdio>/****************************** flexible use of macro definition * * ***************************/# define lson l, m, RT <1 # define rson m + 1, R, RT <1 | 1 const int maxn = 55555; int sum [maxn <2]; void pushup (int rt) {sum [RT] = sum [RT <1] + sum [RT <1 | 1];} /*************************************** * ************************** no struct is used here, only the range and sum are recorded, but l, ********************************** */void build (INT l, int R, int RT) {If (L = r) {scanf ("% d", & sum [RT]); return ;} int M = (L + r)> 1; build (lson); Build (rson); pushup (RT );} /*************************************** ********************************** the update method is not marked here (add or subtract ), cleverly handled positive and negative numbers during the call, reduce function parameters ************************************* **/void Update (int p, int add, int L, int R, int RT) {If (L = r) {sum [RT] + = add; return ;} int M = (L + r)> 1; if (P <= m) Update (p, add, lson); else Update (p, add, rson ); pushup (RT );} /*************************************** *********************************** cleverly reference the RET variable, this reduces the number of M discussions ********************************** * ***/INT query (INT l, int R, int L, int R, int RT) {If (L <= L & R <= r) {return sum [RT];} int M = (L + r)> 1; int ret = 0; If (L <= m) RET + = query (L, R, lson ); if (r> m) RET + = query (L, R, rson); return ret;} int main () {int t, n; scanf ("% d ", & T); For (int cas = 1; CAS <= T; CAS ++) {printf ("case % d: \ n", CAS ); scanf ("% d", & N); Build (1, n, 1); char op [10]; while (scanf ("% s", OP )) {If (OP [0] = 'E') break; int A, B; scanf ("% 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) ;}} return 0 ;}
The above two types of code have the same idea, but the code style is different. The running time and memory usage are the same. This question only involves single-point update and interval summation. Therefore, you can use a tree array to solve the problem. The code is more concise and the running speed is faster. However, the tree array can calculate the range and the maximum value of the range. The general solution is to use the line tree to solve the problem.
Tree array code:
# Include <cstdio> # include <cstring> using namespace STD; const int maxn = 50000 + 10; int Len, a [maxn]; char STR [50]; int lowbit (int x) {return X & (-x );} ******** * **********************/void Update (int I, int v) {While (I <= Len) {A [I] + = V; I + = lowbit (I );}} ********* * *******************/INT sum (int I) {int sum = 0; while (I> 0) {sum + = A [I]; I-= lowbit (I) ;}return sum ;} int main () {int T, V; scanf ("% d", & T); For (INT I = 1; I <= T; I ++) {memset (A, 0, sizeof (a); scanf ("% d", & Len); For (Int J = 1; j <= Len; j ++) {scanf ("% d", & V); Update (J, v) ;}printf ("case % d: \ n", I ); while (scanf ("% s", STR) {If (STR [0] = 'E') break; int X, Y; scanf ("% d", & X, & Y); If (STR [0] = 'A') Update (x, y ); else if (STR [0] = 's') Update (x,-y); else printf ("% d \ n", sum (y) -sum (x-1);} return 0 ;}