1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include <bits/stdc++.h> using namespace std; const int N=40000100,M=300010; long long val[N]; int son[N][2],idx,dat[N],n,m,Q,sz[N],rt[M]; void up(int p){sz[p]=sz[son[p][0]]+sz[son[p][1]]+1;} void copy(int x,int y){ val[x]=val[y]; dat[x]=dat[y]; son[x][0]=son[y][0]; son[x][1]=son[y][1]; sz[x]=sz[y]; } int newnode(int p){ ++idx;copy(idx,p); return idx; } int new_node(long long x){ ++idx;val[idx]=x;dat[idx]=rand();sz[idx]=1; return idx; } void split(int p,int x,int &l,int &r,int t){ if(!p){l=r=0;return ;} if(t)p=newnode(p); if(sz[son[p][0]]<x){ l=p; split(son[p][1],x-sz[son[p][0]]-1,son[p][1],r,t); } else{ r=p; split(son[p][0],x,l,son[p][0],t); } up(p); } int merge(int l,int r,int t){ if(!l||!r)return l+r; if(dat[l]>dat[r]){ if(t)l=newnode(l); son[l][1]=merge(son[l][1],r,t); up(l); return l; } else{ if(t)r=newnode(r); son[r][0]=merge(l,son[r][0],t); up(r); return r; } } int main(){ cin>>n>>m>>Q; for(int i=1;i<=n;i++)rt[0]=merge(rt[0],new_node(1ll*i*m),0); for(int i=1;i<m;i++)rt[1]=merge(rt[1],new_node(i),0); for(int i=2;i<=n;i++)rt[i]=rt[1]; while(Q--){ int x,y; cin>>x>>y; if(y==m){ int l,p,r; split(rt[0],x,l,r,0); split(l,x-1,l,p,0); cout<<val[p]<<"\n"; rt[0]=merge(merge(l,r,0),p,0); } else{ int l,p,r; split(rt[x],y,l,r,1); split(l,y-1,l,p,1); cout<<val[p]+1ll*(x-1)*m<<"\n"; int L,P,R; split(rt[0],x,L,R,0); split(L,x-1,L,P,0); val[P]-=1ll*(x-1)*m; val[p]+=1ll*(x-1)*m; rt[x]=merge(merge(l,r,1),P,1); rt[0]=merge(merge(L,R,0),p,0); } } return 0; }
|