"Title description"
A row of n squares, beginning with an integer in each lattice. Some questions and modifications are presented dynamically: the form of questioning is the summation of all elements in a particular sub-interval [a, b]; the modified rule is to specify a lattice x, plus or minus a specific value, a. You are now asked to make the right answer to each question. 1≤n<100000, the total number of questions and modifications m<10000.
"Input description"
The input file first behaves as an integer n, followed by n rows n integers, representing the original integers in the lattice. followed by a positive integer m, followed by M-line, the M-query, the first integer to indicate the query code, the number 1 indicates an increase, the following two numbers x and a for the value on position x to increase a, ask the code 2 for the interval sum, followed by two integers for A and B, representing the interval between [A, a] and
"Output description"
A total of M-lines, each integer.
"Sample Input"
6
4
5
6
2
1
3
4
1 3 5
2 1 4
1 1 9
2 2 6
"Sample Output"
22
22
"Data range and Tips"
1≤n≤100000,m≤10000.
source code: #include<cstdio>intM,n,num (0), h[100001];structtreetype{intLeft,right,lefts,rights,sum;//in this code, the semi-open half-closed interval is applied. }i[200020];//The number of nodes of a complete binary tree is applied. voidX1 (intT1,intT2)//achievements. { intT=++num;//Note that in the line segment tree, always follow the left small right large principle. i[t].left=T1; I[t].right=T2; if(t1!=t2-1) {i[t].lefts=num+1;//using the left small right large principle, the number. X1 (T1, (T1+T2) >>1); I[t].rights=num+1; X1 ((T1+T2) >>1, T2);//Note that this is a half-open half-closed interval. I[t].sum=i[i[t].lefts].sum+i[i[t].rights].sum;//use backtracking to make intervals and updates. } ElseI[t].sum=h[t1];}voidX2 (intTintXintY//modified. { if(i[t].left==i[t].right-1)//Note that this is a half-open half-closed interval. i[t].sum+=y; Else //The end point has been found and processed, so no repetition and wrong handling is done. { if(x< (i[t].left+i[t].right) >>1)//using the dichotomy method, the node is modified. x2 (i[t].lefts,x,y); if(x>= (i[t].left+i[t].right) >>1)//Note that this is a half-open half-closed interval. x2 (i[t].rights,x,y); I[t].sum=i[i[t].lefts].sum+i[i[t].rights].sum;//use backtracking to make intervals and updates. }}intX3 (intTintXintY//sum. { if(X<=i[t].left&&y>=i[t].right)//at this point, the interval is included in the range. returni[t].sum; intAns0); if(x< (i[t].left+i[t].right) >>1)//Separate the desired interval. ans+=X3 (i[t].lefts,x,y); if(Y> (i[t].left+i[t].right) >>1)//Note that this is a half-open half-closed interval. ans+=X3 (i[t].rights,x,y); returnans;}intMain ()//Basic line segment tree. {scanf ("%d",&N); for(intA=1; a<=n;a++) scanf ("%d",&H[a]); X1 (1, n+1);//Note that this is a half-open half-closed interval. scanf"%d",&m); for(intA=1; a<=m;a++) { intx, Y, Z scanf ("%d%d%d",&x,&y,&z); if(x==1) X2 (1, y,z); Elseprintf ("%d\n", X3 (1, y,z+1));//Note that this is a half-open half-closed interval. } return 0;}
Line Tree Exercise