Title Description
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.
Enter a description
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
Output Description
A total of M lines, each integer
Sample input
Sample Input
6
4
5
6
2
1
3
4
1 3 5
2 1 4
1 1 9
2 2 6
Sample output
Sample Output
22
22
Data range and Tips
Data Size & Hint
1≤n≤100000, m≤10000.
#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<vector>#include<queue>#definell Long Longusing namespacestd;Const intMAXN =100050;intN,M,NUM[MAXN],C[MAXN];intLowbit (intx) { returnx& (-x);}voidChangeintPintx) { for(inti = p;i <= n;i + lowbit (i)) c[i] + =x;}intQueryintAintb) { intsum =0; for(inti = b;i;i-= lowbit (i)) sum + =C[i]; for(inti = A-1; I;i-= Lowbit (i)) sum-=C[i]; returnsum;}intMain () {CIN>>N; intcmd,a,b; for(inti =1; I <= n;i++) {scanf ("%d",&Num[i]); for(intj = I-lowbit (i) +1; j<=i;j++) C[i] + =Num[j]; } CIN>>m; for(inti =1; I <= m;i++) {scanf ("%d%d%d",&cmd,&a,&b); if(cmd = =1) Change (a, b); Elseprintf"%d\n", query (A, b)); } return 0;}
#include <iostream>#include<cstdio>#include<string>#include<cstring>#include<algorithm>#include<vector>#include<queue>#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1using namespacestd;Const intMAXN =100050;intn,m,a[maxn<<4],sum[maxn<<4];voidPushup (intRT) {Sum[rt]= sum[rt<<1] + sum[rt<<1|1];} voidBuildtree (intLintRintRT) { if(L = =R) {Sum[rt]=A[l]; return; } intm = (l+r) >>1; Buildtree (Lson); Buildtree (Rson); Pushup (RT);}voidChangeintPintXintLintRintRT) { if(L = =R) {Sum[rt]+=x; return; } intm = (l+r) >>1; if(P <=m) Change (P,x,lson); ElseChange (P,x,rson); Pushup (RT); return;}intQueryintLintRintLintRintRT) { if(l <= l && R <=R) { returnSum[rt]; } intm = (l+r) >>1; intRET =0; if(L <= m) ret + =query (L,r,lson); if(R > m) ret + =query (L,r,rson); returnret;}intMain () {CIN>>N; for(inti =1; I <= n;i++) {scanf ("%d",&A[i]); } buildtree (1N1); CIN>>m; intcmd,a,b; for(inti =1; I <= m;i++) {scanf ("%d%d%d",&cmd,&a,&b); if(cmd = =1) Change (A, B,1N1); Elseprintf"%d\n", query (A, B,1N1)); } return 0;}
codevs1080 Line segment Tree Exercises