标签:bool cst operator \n sizeof ini getc CQ font
CQQ分治
#include <cstdio> #include <cstring> #define N 50010 struct info{ int x,p,v; info(int a,int b,int c):x(a),p(b),v(c){} info(){x=p=v=0;} friend bool operator < (info a,info b){ return a.p==b.p?a.x<b.x:a.p<b.p; } }que[N*3],tmp[N*3]; int n,qn,an,Ans[N]; inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } inline void Init(){ qn=an=0; memset(Ans,0,sizeof(Ans)); n=read(); for(int i=1,x;i<=n;++i) x=read(),que[qn++]=info(1,i,x); char s[10]; for(;;){ scanf("%s",s); if(s[0]==‘E‘) break; if(s[0]==‘Q‘){ int l=read(),r=read(); que[qn++]=info(2,l-1,an); que[qn++]=info(3,r,an++); }else{ int p=read(),v=read(); if(s[0]==‘S‘) v=-v; que[qn++]=info(1,p,v); } } } void solve(int l,int r){ if(l+1>=r) return; int m=(l+r)>>1; solve(l,m),solve(m,r); int p=l,q=m,cnt=0,sum=0; while(p<m&&q<r){ if(que[p]<que[q]){ if(que[p].x==1) sum+=que[p].v; tmp[cnt++]=que[p++]; }else{ if(que[q].x==2) Ans[que[q].v]-=sum; else if(que[q].x==3) Ans[que[q].v]+=sum; tmp[cnt++]=que[q++]; } } while(p<m) tmp[cnt++]=que[p++]; while(q<r){ if(que[q].x==2) Ans[que[q].v]-=sum; else if(que[q].x==3) Ans[que[q].v]+=sum; tmp[cnt++]=que[q++]; } for(int i=0;i<cnt;++i) que[i+l]=tmp[i]; } int main(){ for(int T=read(),i=1;i<=T;++i){ Init(); solve(0,qn); printf("Case %d:\n",i); for(int i=0;i<an;++i) printf("%d\n",Ans[i]); } }
标签:bool cst operator \n sizeof ini getc CQ font
原文地址:https://www.cnblogs.com/void-f/p/9069459.html