bzoj 3223 Tyvj 1729 文艺平衡树

区间翻转233

 1 #include<bits/stdc++.h>
 2 #define N 100005
 3 #define LL long long
 4 #define inf 0x3f3f3f3f
 5 #define ls tr[x][0]
 6 #define rs tr[x][1]
 7 using namespace std;
 8 inline int ra()
 9 {
10     int x=0,f=1; char ch=getchar();
11     while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
12     while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
13     return x*f;
14 }
15 struct SPLAY{
16     bool rev[N];
17     int fa[N],tr[N][2],root,v[N],size[N],w[N],cnt;
18     bool which(int x){return tr[fa[x]][1]==x;}
19     void update(int x){size[x]=size[ls]+size[rs]+1;}
20     void rotate(int x)
21     {
22         int y=fa[x],z=fa[y]; bool nx=which(x),ny=which(y);
23         tr[y][nx]=tr[x][!nx]; fa[tr[x][!nx]]=y;
24         fa[y]=x; tr[x][!nx]=y; fa[x]=z;
25         if (z) tr[z][ny]=x; update(y); update(x);
26     }
27     void splay(int x, int aim)
28     {
29         while (fa[x]!=aim)
30         {
31             int y=fa[x],z=fa[y];
32             if (z==aim) rotate(x);
33             else if (which(x)==which(y)) rotate(y),rotate(x);
34             else rotate(x),rotate(x);
35         }
36         if (!aim) root=x; update(x);
37     }
38     void insert(int val)
39     {
40         int x=root;
41         for (;;)
42         {
43             if (val<v[x]) 
44                 if (!ls) {ls=val,fa[ls]=x,size[ls]=1; splay(ls,0); break;} else x=ls;
45             else if (!rs) {rs=val,fa[rs]=x,size[rs]=1; splay(rs,0); break;} else x=rs;
46         }
47     }
48     void pushdown(int x)
49     {
50         if (rev[x])
51         {
52             swap(ls,rs);
53             rev[ls]^=1; rev[rs]^=1;
54             rev[x]=0;
55         }
56     }
57     int find(int x, int rank)
58     {
59         pushdown(x);
60         if (size[ls]+1==rank) return x;
61         else if (size[ls]>=rank) return find(ls,rank);
62         else return find(rs,rank-size[ls]-1);
63     }
64     void rever(int l, int r)
65     {
66         int x=find(root,l);int y=find(root,r+2);
67         splay(x,0); splay(y,x);
68         rev[tr[y][0]]^=1;
69     }
70 }T;
71 int n,m;
72 int main()
73 {
74     n=ra(); m=ra();
75     for (int i=1; i<=n+2; i++) T.insert(i);
76     for (int i=1; i<=m; i++)
77     {
78         int x=ra(),y=ra();
79         T.rever(x,y);
80     }
81     for (int i=2; i<=n+1; i++)
82         printf("%d ",T.find(T.root,i)-1);
83 }

 

posted @ 2017-02-12 22:32  ws_ccd  阅读(166)  评论(0编辑  收藏  举报