bzoj3223 Tyvj 1729 文艺平衡树

题目链接

平衡树系列第二题

轩神的split还是这么好写%%%

还是要注意虚拟结点

  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],w[100010],s[100010],rev[100010],fa[100010],ed,root;
 34 void maintain(int k){s[k]=s[ch[k][0]]+s[ch[k][1]]+1;}
 35 int newnode(int v,int f)
 36 {
 37     ed++;
 38     w[ed]=v,fa[ed]=f;
 39     ch[ed][0]=ch[ed][1]=0;
 40     s[ed]=1;rev[ed]=0;
 41     return ed;
 42 }
 43 void down(int k)
 44 {
 45     if(rev[k])
 46     {
 47         swap(ch[k][0],ch[k][1]);
 48         rev[ch[k][0]]^=1,rev[ch[k][1]]^=1;
 49         rev[k]=0;
 50     }
 51 }
 52 void rotate(int x)
 53 {
 54     int y=fa[x],z=fa[y];
 55     if(z)ch[z][ch[z][1]==y]=x;
 56     int l=ch[y][1]==x,r=l^1;
 57     fa[y]=x,fa[x]=z;
 58     if(ch[x][r])fa[ch[x][r]]=y;
 59     ch[y][l]=ch[x][r],ch[x][r]=y;
 60     maintain(y);
 61 }
 62 int sta[100010],top;
 63 void splay(int k,int f)
 64 {
 65     top=0;
 66     for(int i=k;i!=f;i=fa[i])sta[++top]=i;
 67     while(top)down(sta[top--]);
 68     while(fa[k]!=f)
 69     {
 70         int y=fa[k],z=fa[y];
 71         if(z!=f)
 72             if((ch[z][1]==y)^(ch[y][1]==k))rotate(k);
 73             else rotate(y);else ;
 74         rotate(k);
 75     }
 76     maintain(k);
 77     if(!f)root=k;
 78 }
 79 int tot,a[100010];
 80 int kth(int k)
 81 {
 82     int x=root;
 83     while(x)
 84     {
 85         down(x);
 86         int pp=s[ch[x][0]]+1;
 87         if(pp==k)return x;
 88         if(pp<k)k-=pp,x=ch[x][1];
 89         else x=ch[x][0];
 90     }return 0;
 91 }
 92 int build(int f,int l,int r)
 93 {
 94     if(l>r)return 0;
 95     int mid=(l+r)>>1;
 96     int k=newnode(a[mid],f);
 97     ch[k][0]=build(k,l,mid-1);
 98     ch[k][1]=build(k,mid+1,r);
 99     maintain(k);return k;
100 }
101 int split(int l,int r)
102 {
103     int x=kth(l),y=kth(r+2);
104     splay(x,0);splay(y,x);
105     return ch[y][0];
106 }
107 int n,m;
108 void print(int k)
109 {
110     if(!k)return ;
111     down(k);
112     print(ch[k][0]);
113     printf("%d ",w[k]);
114     print(ch[k][1]);
115 }
116 int CSC()
117 {
118     inin(n),inin(m);
119     re(i,1,n+2)a[i]=i-1;
120     root=build(0,1,n+2);
121     re(i,1,m)
122     {
123         int L,R;
124         inin(L),inin(R);
125         int hh=split(L,R);
126         rev[hh]^=1;
127     }
128     int hh=split(1,n);
129     print(hh);
130     return 0;
131 }

 

posted @ 2016-01-24 08:19  HugeGun  阅读(211)  评论(1编辑  收藏  举报