分块。利用根号平衡保证复杂度。修改跨过一整块的直接打标记,不足一整块的暴力修改。复杂度为$O(nsqrt{n})$
LOJ 6277
#include<cstdio> #include<algorithm> #include<cmath> #define N (500010) #define rg register using namespace std; int n,a[N],bl[N],tag[N]; inline int read(){ int k=0,f=1; char c=getchar(); while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar(); while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar(); return k*f; } int main(){ n=read(); int block=sqrt(n); for(rg int i=1;i<=n;i++) a[i]=read(),bl[i]=(i-1)/block+1; for(rg int i=1;i<=n;i++){ int opt=read(),l=read(),r=read(),del=read(); if(!opt){ int st=bl[l]+((l-1)%block?1:0),ed=bl[r]-(r%block?1:0); for(rg int j=st;j<=ed;j++) tag[j]+=del; if(bl[l]==bl[r]&&(r-l+1)<block){ for(rg int j=l;j<=r;j++) a[j]+=del; continue; } if(st!=bl[l]) for(rg int j=l;j<=bl[l]*block;j++) a[j]+=del; if(ed!=bl[r]) for(rg int j=ed*block+1;j<=r;j++) a[j]+=del; } else printf("%d ",a[r]+tag[bl[r]]); } return 0; }