bzoj1552 [Cerc2007]robotic sort

题目链接

splay模板题

只是多维护一个子树内最小值所在结点编号即可

  1 #include<algorithm>
  2 #include<iostream>
  3 #include<cstdlib>
  4 #include<cstring>
  5 #include<cstdio>
  6 #include<string>
  7 #include<cmath>
  8 #include<ctime>
  9 #include<queue>
 10 #include<stack>
 11 #include<map>
 12 #include<set>
 13 #define rre(i,r,l) for(int i=(r);i>=(l);i--)
 14 #define re(i,l,r) for(int i=(l);i<=(r);i++)
 15 #define Clear(a,b) memset(a,b,sizeof(a))
 16 #define inout(x) printf("%d",(x))
 17 #define douin(x) scanf("%lf",&x)
 18 #define strin(x) scanf("%s",(x))
 19 #define LLin(x) scanf("%lld",&x)
 20 #define op operator
 21 #define CSC main
 22 typedef unsigned long long ULL;
 23 typedef const int cint;
 24 typedef long long LL;
 25 using namespace std;
 26 void inin(int &ret)
 27 {
 28     ret=0;int f=0;char ch=getchar();
 29     while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}
 30     while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar();
 31     ret=f?-ret:ret;
 32 }
 33 int ch[100010][2],fa[100010],s[100010],rev[100010],root;
 34 void maintain(int k){if(k)s[k]=s[ch[k][0]]+s[ch[k][1]]+1;}
 35 void down(int k)
 36 {
 37     if(rev[k])
 38     {
 39         swap(ch[k][0],ch[k][1]);
 40         rev[ch[k][0]]^=1;
 41         rev[ch[k][1]]^=1;
 42         rev[k]=0;
 43     }
 44 }
 45 void rotate(int x)
 46 {
 47     int y=fa[x],z=fa[y];
 48     if(z)ch[z][ch[z][1]==y]=x;
 49     int l=ch[y][1]==x,r=l^1;
 50     fa[y]=x,fa[x]=z;
 51     fa[ch[x][r]]=y;
 52     ch[y][l]=ch[x][r];
 53     ch[x][r]=y;
 54     maintain(y),maintain(x);
 55 }
 56 int sta[100010],top;
 57 void splay(int x,int f)
 58 {
 59     top=0;
 60     for(int i=x;i;i=fa[i])sta[++top]=i;
 61     while(top)down(sta[top--]);
 62     while(fa[x]!=f)
 63     {
 64         int y=fa[x],z=fa[y];
 65         if(z!=f)
 66             if((ch[z][1]==y)^(ch[y][1]==x))rotate(y);
 67             else rotate(x);else ;
 68         rotate(x);
 69     }
 70     if(!f)root=x;
 71 }
 72 int kth(int k)
 73 {
 74     int x=root;
 75     while(x)
 76     {
 77         down(x);
 78         int pp=s[ch[x][0]]+1;
 79         if(pp==k)return x;
 80         if(k<pp)x=ch[x][0];
 81         else x=ch[x][1],k-=pp;
 82     }return 0;
 83 }
 84 void reverse(int l,int r)
 85 {
 86     int x=kth(l),y=kth(r+2);splay(x,0),splay(y,x);
 87     rev[ch[y][0]]^=1;
 88 }
 89 int aa[100010],tot;
 90 pair<int,int> a[100010];
 91 int build(int xx)
 92 {
 93     if(xx<=0)return 0;
 94     int l=build(xx>>1);
 95     int x=aa[tot++]+1;
 96     int r=build(xx-(xx>>1)-1);
 97     if(l)fa[l]=x;
 98     if(r)fa[r]=x;
 99     ch[x][0]=l,ch[x][1]=r;
100     maintain(x);return x;
101 }
102 int n;
103 int CSC()
104 {
105     inin(n);
106     re(i,1,n)inin(a[i].first),a[i].second=i;
107     a[n+1].second=n+1;
108     sort(a+1,a+n+1);
109     re(i,0,n+1)aa[a[i].second]=i;
110     root=build(n+2);
111     re(i,1,n)
112     {
113         int u=i+1;
114         splay(u,0);
115         int rank=s[ch[u][0]];
116         printf("%d",rank);
117         if(i!=n)printf(" ");
118         if(i==rank)continue;
119         int l=min(i,rank),r=max(i,rank);
120         reverse(l,r);
121     }
122     return 0;
123 }

 

你可能感兴趣的:(bzoj1552 [Cerc2007]robotic sort)