Template Problem CODEVS-1082
Give you the number of n, there are two operations:
1: Add x for all numbers of interval [a, b]
2: Ask for the number of intervals [A, b] and.
The first line is a positive integer n, and the next n rows n integers,
Then the next positive integer q, each line represents the number of operations,
If the first number is 1, followed by 3 positive integers,
Indicates that each number in the interval [a, b] is increased by x, if it is 2,
Represents the number of the and of the action 2 ask interval [A, b].
A one-dimensional tree array can be considered to be differential, but not to be extended to two dimensions.
So we make the increment of di= (Ai~an)
The idea is the same as the difference, can be extended to two-dimensional (not now understand)
#include <iostream>#include<cstdio>#include<algorithm>#defineRep (i,a,b) for (int i=a;i<=b;i++)using namespacestd;#defineLowbit (x) x& (-X)intn,m,a[1000005],c1[1000005],c2[1000005];voidUpdateintVintval) { for(intpp=v;pp<=n;c1[pp]+=val,c2[pp]+=val*v,pp+=lowbit (PP));}intQueryintLintR) { intans=0; for(intPp=r;pp>0; ans+= (r+1) *c1[pp],ans-=c2[pp],pp-=lowbit (PP)); L--; for(intPp=l;pp>0; ans-= (L +1) *c1[pp],ans+=c2[pp],pp-=lowbit (PP)); returnans;}intMain () {scanf ("%d%d",&n,&m); for(intI=1; i<=n;i++) scanf ("%d",&A[i]); for(intI=1; i<=n;i++) Update (I,a[i]-a[i-1]); for(intI=1; i<=m;i++) { intP,l,r,val; scanf (" %d%d%d",&p,&l,&R); if(p==1) {scanf ("%d",&val); Update (L,VAL); Update (R+1,-val); }Else{printf ("%d\n", Query (l,r)); } } return 0;}
One-dimensional array of arrays modified, query