BZOJ 4216: Pig [分块]
询问区间和...内存3M
分块前缀和...
我连swap都是这么l=l^r , r=l^r , l=l^r写的....
#include <cstdio> typedef long long ll; const int N=5e5+5,M=1e4+5; int n,Q,type,a[N],l,r,block=50,m; ll s[M],last; int main(){ freopen("in","r",stdin); scanf("%d%d%d",&n,&Q,&type); m=(n-1)/block+1; for(int i=1;i<=n;i++) scanf("%d",&a[i]),s[(i-1)/block+1]+=a[i]; for(int i=1;i<=m;i++) s[i]+=s[i-1]; while(Q--){ scanf("%d%d",&l,&r); if(type){ if(last<0) last=-last; l=(l^last)%n+1;r=(r^last)%n+1; if(l>r) l=l^r , r=l^r , l=l^r; } int pl=(l-1)/block+1,pr=(r-1)/block+1; last=0; if(pl==pr) for(int i=l;i<=r;i++) last+=a[i]; else{ last=s[pr-1]-s[pl]; int _=pl==m ? n : pl*block; for(int i=l;i<=_;i++) last+=a[i]; for(int i=(pr-1)*block+1;i<=r;i++) last+=a[i]; } printf("%lld\n",last); } }
Copyright:http://www.cnblogs.com/candy99/