敌兵布阵
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
1 10 1 2 3 4 5 6 7 8 9 10 Query 1 3 Add 3 6 Query 2 7 Sub 10 2 Add 6 3 Query 3 10 End
Sample Output
Case 1: 6 33 59
#include<cstring> #include<string> #include<iostream> #include<queue> #include<cstdio> #include<algorithm> #include<map> #include<cstdlib> #include<cmath> #include<vector> //#pragma comment(linker, "/STACK:1024000000,1024000000"); using namespace std; #define INF 0x3f3f3f3f #define maxn 4*50000 struct node { int l,r; int sum; } T[maxn]; void init(int l,int r,int k) { T[k].l=l; T[k].r=r; if(l==r) { scanf("%d",&T[k].sum); return ; } int mid=(l+r)>>1; init(l,mid,2*k); init(mid+1,r,2*k+1); T[k].sum=T[2*k].sum+T[2*k+1].sum; } void Insert(int p,int d,int k) { if(T[k].l==T[k].r&&T[k].r==p) { T[k].sum+=d; return ; } int mid=(T[k].l+T[k].r)>>1; if(p<=mid) Insert(p,d,2*k); else if(p>mid) Insert(p,d,2*k+1); T[k].sum=T[2*k].sum+T[2*k+1].sum; } int query(int l,int r,int k) { if(T[k].l==l&&T[k].r==r) { return T[k].sum; } int mid=(T[k].l+T[k].r)>>1; if(r<=mid) return query(l,r,2*k); else if(l>mid) return query(l,r,2*k+1); else { return query(l,mid,2*k)+query(mid+1,r,2*k+1); } } int main() { int c=1; int T; scanf("%d",&T); while(T--) { printf("Case %d:\n",c++); char str[10]; int n; scanf("%d",&n); init(1,n,1); while(1) { scanf("%s",str); if(!strcmp(str,"Add")) { int a,b; scanf("%d%d",&a,&b); Insert(a,b,1); } else if(!strcmp(str,"Sub")) { int a,b; scanf("%d%d",&a,&b); Insert(a,-b,1); } else if(!strcmp(str,"Query")) { int a,b; scanf("%d%d",&a,&b); int ans=query(a,b,1); printf("%d\n",ans); } else break; } } return 0; }
#include<cstring> #include<string> #include<iostream> #include<queue> #include<cstdio> #include<algorithm> #include<map> #include<cstdlib> #include<cmath> #include<vector> //#pragma comment(linker, "/STACK:1024000000,1024000000"); using namespace std; #define INF 0x3f3f3f3f #define keytree (ch[ch[root][1]][0]) #define maxn 50005 int ch[maxn][2],sum[maxn],pre[maxn],val[maxn],tmp[maxn],siz[maxn]; int top,root; inline void newnode(int &x,int p,int v) { x=++top; ch[x][0]=ch[x][1]=0; sum[x]=v; pre[x]=p; val[x]=v; siz[x]=1; } inline void pushup(int x) { sum[x]=sum[ch[x][0]]+sum[ch[x][1]]+val[x]; siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1; } inline void build(int &x,int l,int r,int p) { int mid=(l+r)>>1; newnode(x,p,tmp[mid]); if(l<mid) build(ch[x][0],l,mid-1,x); if(r>mid) build(ch[x][1],mid+1,r,x); pushup(x); } inline void init(int n) { for(int i=0; i<n; i++) scanf("%d",&tmp[i]); ch[0][0]=ch[0][1]=siz[0]=pre[0]=sum[0]=val[0]=0; root=top=0; newnode(root,0,-1); newnode(ch[root][1],root,-1); build(keytree,0,n-1,ch[root][1]); pushup(ch[root][1]); pushup(root); } inline void Rotate(int x,int kind) { int y=pre[x]; ch[y][!kind]=ch[x][kind]; pre[ch[x][kind]]=y; if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x; pre[x]=pre[y]; ch[x][kind]=y; pre[y]=x; pushup(y); } void splay(int x,int goal) { while(pre[x]!=goal) { if(pre[pre[x]]==goal) Rotate(x,ch[pre[x]][0]==x); else { int y=pre[x]; int kind=ch[pre[y]][0]==y; if(ch[y][kind]==x) { Rotate(x,!kind); Rotate(x,kind); } else { Rotate(y,kind); Rotate(x,kind); } } } pushup(x); if(!goal) root=x; } inline int get_kth(int x,int k) { int t=siz[ch[x][0]]+1; if(t==k) return x; if(k<t) return get_kth(ch[x][0],k); else return get_kth(ch[x][1],k-t); } int main() { int T; scanf("%d",&T); int c=1; while(T--) { printf("Case %d:\n",c++); int n; scanf("%d",&n); init(n); char str[10]; while(1) { int a,b; scanf("%s",str); if(!strcmp(str,"Query")) { scanf("%d%d",&a,&b); splay(get_kth(root,a),0); splay(get_kth(root,b+2),root); int ans=sum[keytree]; printf("%d\n",ans); } else if(!strcmp(str,"Add")) { scanf("%d%d",&a,&b); splay(get_kth(root,a+1),0); val[root]+=b; pushup(root); } else if(!strcmp(str,"Sub")) { scanf("%d%d",&a,&b); splay(get_kth(root,a+1),0); val[root]-=b; pushup(root); } else break; } } return 0; }