Topic description
input format
output format
Ideas:
The simplest sequence of blocks
First clarify the concept of a wave of blocks:
Divide a long sequence of n into k pieces (generally k=sqrt(n));
Then the modification of the interval becomes the modification of the complete block and the violence of the incomplete blocks at both ends of the interval
How to describe the modification and query of the interval?
Lazy Dafa is good! !
Create a marker array to store changes to the entire range
Finally, the sum of the tag array and the original value can be output.
Code:
#include<iostream> #include<cstdio> #include<cmath> using namespace std; int x[50005],sy[50005],fk[50005],n,opt,l,r,c,a,b,d,dx,cnt,bnt; intmain() { // freopen("a1.in","r",stdin); // freopen("1.out","w",stdout); cin>>n; dx=sqrt(n); bnt=1; for(a=1;a<=n;a++) { scanf("%d",&x[a]); and [a] = (a-1)/dx+1; } for(a=1;a<=n;a++) { scanf("%d%d%d%d",&opt,&l,&r,&c); if(opt==1) { printf("%d\n",x[r]+fk[sy[r]]); } else { if (sy [l] == sy [r]) { for(b=l;b<=r;b++) { x[b]+=c; } continue; } int lf, ri; lf = sy [l]; ri = sy [r]; for(b=l;b<=lf*dx;b++) { x[b]+=c; } for(b=(ri-1)*dx+1;b<=r;b++) { x[b]+=c; } for(b=lf+1;b<=ri-1;b++) { fk[b]+=c; } } } }