标签:ase void splay int bool char sizeof turn case
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int beg[200010],en[200010],num[200010],fa[200010],ch[200010][2],root,N; inline void flow(int n){ num[n]=num[ch[n][0]]+num[ch[n][1]]+en[n]-beg[n]+1; } inline void rotate(int n){ int t=fa[n]; bool isr=(ch[t][1]==n); ch[t][isr]=ch[n][!isr],fa[ch[n][!isr]]=t; fa[n]=fa[t],ch[fa[t]][ch[fa[t]][1]==t]=n; fa[t]=n,ch[n][!isr]=t; flow(t),flow(n); } inline void splay(int n,int goal){ int f,ff; while(fa[n]-goal) { f=fa[n],ff=fa[f]; if(goal==ff) rotate(n); else if((ch[ff][1]==f)==(ch[f][1]==n)) rotate(f),rotate(n); else rotate(n),rotate(n); } if(!goal) root=n; } void Top(int n){ splay(n,0); if(!ch[n][1]) { ch[n][1]=ch[n][0]; ch[n][0]=0; return; } int t=ch[n][1]; while(ch[t][0]) t=ch[t][0]; splay(t,n); ch[t][0]=ch[n][0],fa[ch[n][0]]=t; ch[n][0]=0; flow(ch[n][1]),flow(n); } int Query(int n){ splay(n,0); return num[ch[n][0]]+1; } int Rank(int n){ int t=root; while(1){ if(num[ch[t][0]]<n&&num[ch[t][0]]+en[t]-beg[t]+1>=n) break; if(n<=num[ch[t][0]]) t=ch[t][0]; else n-=num[ch[t][0]]+en[t]-beg[t]+1,t=ch[t][1]; } int ans=beg[t]+n-1-num[ch[t][0]]; splay(t,0); return ans; } int Bsearch(int n){ int l=1,r=N,mid; while(l<=r) { mid=(l+r)>>1; if(beg[mid]>n) r=mid-1; else if(en[mid]<n) l=mid+1; else return mid; } return l; } int req[100010],a[200010]; char re[100010]; int main(){ int t,n,q,i,j,k,ii=0; char s[10]; for(scanf("%d",&t);t--;){ scanf("%d%d",&n,&q); ++ii; for(i=0,k=0;i<q;++i) { scanf("%s%d",s,&j); req[i]=j,re[i]=s[0]; if(s[0]==‘T‘) a[k++]=j; } sort(a,a+k); for(i=1,j=1;i<k;++i) if(a[i]-a[i-1]) a[j++]=a[i]; k=1; if(a[0]>1) beg[k]=1,en[k++]=a[0]-1; for(i=0;i<j;++i) { beg[k]=en[k]=a[i],++k; if(i<j-1) { if(a[i]<a[i+1]-1) beg[k]=a[i]+1,en[k++]=a[i+1]-1; } else if(a[i]<n) beg[k]=a[i]+1,en[k++]=n; } beg[k]=++n,en[k++]=n; memset(ch,0,sizeof(int)*2*(k+1)); N=k-1,num[0]=0; for(i=1;i<=N;++i) { ch[i][0]=i-1,num[i]=num[i-1]+en[i]-beg[i]+1; fa[i-1]=i; } root=N,fa[N]=0; printf("Case %d:\n",ii); for(i=0;i<q;++i) { if(re[i]==‘T‘) Top(Bsearch(req[i])); else if(re[i]==‘Q‘) { j=Bsearch(req[i]); printf("%d\n",Query(j)+req[i]-beg[j]); } else printf("%d\n",Rank(req[i])); } } }
标签:ase void splay int bool char sizeof turn case
原文地址:http://www.cnblogs.com/mfys/p/8013760.html