The function of the difference fractional group is to modify the interval, query the point
The time complexity of the modified interval is O (1), and the time complexity of the query point is O (n)
We have to choose the method flexibly according to the data range, not to be stuck in the difference score Group
void Update (int x,int y,int z) { b[x]+ =Z; B[y+1]-=z;}
The above is to modify the interval operation, x position plus the amount of modification, y+1 position minus the amount of modification, so that the entire interval element is equivalent to modify the
int sum (int x) { int ans=0; for (int i=1; i<=x;i++) ans+ =b[i] ; return ans;}
It was convenient, but I need to add it all at the time of the inquiry.
And when it comes to preprocessing.
b[1]=a[1]; for (int i=2; i<=n;i++) b[i]=a[i]-a[i-1];
The following is a complete implementation code:
1 //aininot2602 //modify interval, query point3#include <iostream>4#include <cstring>5 using namespacestd;6 Const intmaxn=100005;7 Const intmaxq=100005;8 intA[MAXN];9 intB[MAXN];Ten intn,q; One voidUpdateintXintYintz) A { -b[x]+=Z; -b[y+1]-=Z; the } - intSumintx) - { - intans=0; + for(intI=1; i<=x;i++) -ans+=B[i]; + returnans; A } at intMain () - { -Cin>>N; - for(intI=1; i<=n;i++) -Cin>>A[i]; -b[1]=a[1]; in for(intI=2; i<=n;i++) -b[i]=a[i]-a[i-1]; toCin>>Q; + while(q--) - { the intx; *Cin>>x; $ if(x==1)Panax Notoginseng { - inty,z,w; theCin>>y>>z>>W; + Update (Y,Z,W); A } the if(x==2) + { - inty; $Cin>>y; $Cout<<sum (y) <<Endl; - } - } the return 0; -}
Implementation of differential fractional groups