loj #6277. 数列分块入门 1
分块第一题。。。
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=50005; int belong[N],block,n,q,l[N],r[N],num; ll a[N],s[N]; void build(){ block=sqrt(n); num=n/block;if(n%block)++num; for(int i=1;i<=num;++i)l[i]=(i-1)*block+1,r[i]=i*block; r[num]=n; for(int i=1;i<=n;++i)belong[i]=(i-1)/block+1; } ll ask(int x){ return a[x]+s[belong[x]]; } void update(int x,int y,ll add){ if(belong[x]==belong[y]){ for(int i=x;i<=y;++i)a[i]+=add; return ; } for(int i=x;i<=r[belong[x]];++i)a[i]+=add; for(int i=belong[x]+1;i<belong[y];++i)s[i]+=add; for(int i=l[belong[y]];i<=y;++i)a[i]+=add; return ; } int main(){ scanf("%d",&n); for(int i=1;i<=n;++i)scanf("%d",&a[i]); build(); for(int i=1;i<=n;++i){ int opt,l,r; ll c; scanf("%d%d%d%lld",&opt,&l,&r,&c); if(opt==1){ printf("%d\n",ask(r)); } else{ update(l,r,c); } } return 0; }