Topic description
input format
output format
Ideas:
Slightly more complex chunking
Compared with the sequence block 1, the more is only interval query
! ! ! Don't look at the difference of only 2 words, the two questions are much different! ! !
First of all, because it is an interval query, each interval should also maintain a sum
At the same time, when adding, the mark of the non-complete interval should be decentralized (run violently)
When querying, output the sum of the complete interval and the sum of each element of the incomplete interval.
ok
Code:
#include<iostream> #include<cstdio> #include<cmath> using namespace std; long long x[50005],sy[50005],bj[10005],sum[10005],n,c,opt,l,r,cd; void pushdown(int wz) { for (register int ltt = (wz-1)*cd+1; ltt <= wz*cd; ltt ++) { x[ltt]+=bj[wz]; } bj[wz]=0; } void add(int zuo,int you,int v) { int k = sy [zuo]; int j = sy [you]; if(k==j) { pushdown(k); for (register int ltt = zuo; ltt <= you; ltt ++) { x[ltt]+=v; sum [k] + = v; } return; } for (register int ltt = k+1; ltt <= j-1; ltt ++) { bj [ltt]+= v; sum [ltt]+= v*cd; } pushdown(k); pushdown(j); for (register int ltt = zuo; ltt <= k*cd; ltt ++) { x[ltt]+=v; sum [sy [ltt]]+= v; } for (register int ltt = (j-1)*cd+1; ltt <= you; ltt ++) { x[ltt]+=v; sum [sy [ltt]]+= v; } } long long query(int zuo,int you,int mod) { long long ans=0; int k = sy [zuo]; int j = sy [you]; if(k==j) { // pushdown(k); for (register int ltt = zuo; ltt <= you; ltt ++) { ans+=x[ltt]+bj[k]; } ans=ans%(mod+1); return ans; } for(register int kkk=k+1;kkk<=j-1;kkk++) { ans + = sum [kkk]; } for (register int ltt = zuo; ltt <= k*cd; ltt ++) { ans+=x[ltt]+bj[k]; } for (register int ltt = (j-1)*cd+1; ltt <= you; ltt ++) { ans+=x[ltt]+bj[j]; } ans=ans%(mod+1); return ans; } intmain() { // freopen("a1.in","r",stdin); // freopen("1.out","w",stdout); scanf("%d",&n); cd=sqrt(n); int cnt=1; int bnt=0; for(register int i=1;i<=n;i++) { scanf("%d",&x[i]); bnt++; and [i] = cnt; sum[cnt]+=x[i]; if(bnt==cd) { bnt=0; cnt++; } } for(register int i=1;i<=n;i++) { scanf("%d%d%d%d",&opt,&l,&r,&c); if(opt==0) { add(l,r,c); } else { printf("%d\n",query(l,r,c)); } } // system("fc 1.out a1.out"); // system("pause"); }