## BZOJ3223 Tyvj 1729 文艺平衡树

  1 /**************************************************************
2     Problem: 3223
3     User: round_0
4     Language: C++
5     Result: Accepted
6     Time:3268 ms
7     Memory:3148 kb
8 ****************************************************************/
9
10 #include <cstdio>
11 #include <cstring>
12 #include <algorithm>
13 using namespace std;
14 const int maxn = 100005;
15 int fa[maxn],son[2][maxn],val[maxn],lz[maxn],siz[maxn];
16 int root,tot,cnt;
17 void pushdown(int x){
18     if(lz[x]){
19             swap(son[0][x],son[1][x]);
20         lz[son[0][x]]^=1;
21         lz[son[1][x]]^=1;
22         lz[x] = 0;
23
24     }
25 }
26 void rota(int w,int x){
27
28     pushdown(x);
29     int y = fa[x];
30     son[!w][y] = son[w][x];
31     if(son[w][x])fa[son[w][x]] = y;
32     fa[x] = fa[y];
33     if(fa[y])son[y==son[1][fa[y]]][fa[y]] = x;
34     fa[y] = x;
35     son[w][x] = y;
36     siz[y] = siz[y]-siz[x]+siz[son[!w][y]];
37     siz[x] = siz[x]-siz[son[!w][y]]+siz[y];
38 }
39 void splay(int x,int y){
40     while(fa[x]!=y){
41         if(fa[fa[x]]==y)rota(x==son[0][fa[x]],x);
42         else {
43             int w = fa[x]==son[0][fa[fa[x]]];///here
44             if(x==son[w][fa[x]]){
45                 rota(!w,x);rota(w,x);
46             }
47             else {
48                 rota(w,fa[x]); rota(w,x);
49             }
50         }
51
52     }
53     if(y==0)root = x;
54 }
55 void Ins(int v){
56     int x = root;
57     while(1){
58         siz[x]++;
59         if(v<val[x]){
60             if(son[0][x])x = son[0][x];
61             else break;
62         }
63         else {
64             if(son[1][x])x = son[1][x];
65             else break;
66         }
67     }
68     fa[++tot] = x;
69     val[tot] = v;
70     siz[tot] = 1;
71     if(v<val[x])son[0][x] = tot;
72     else son[1][x] = tot;
73     splay(tot,0);
74 }
75 int getK(int k){
76     int x = root;
77     if(k<1||k>tot)return 0;
78     while(1){
79         pushdown(x);
80         if(k==siz[son[0][x]]+1)return x;
81         if(k<=siz[son[0][x]])x = son[0][x];
82         else {
83             k-=(siz[son[0][x]]+1);
84             x = son[1][x];
85         }
86     }
87
88     return x;
89 }
90 int main()
91 {
92    // freopen("in.txt","r",stdin);
93     int n,m;scanf("%d%d",&n,&m);
94     root = ++tot;
95     val[tot] = 1;
96     siz[tot] = 1;
97     for(int i = 2;i<=n;++i)Ins(i);
98     while(m--){
99             cnt++;
100         int l,r;scanf("%d%d",&l,&r);
101         int x = getK(l-1),y = getK(r+1);
102         if(x)splay(x,0);
103         if(y)splay(y,x);
104         if(x==0&&y==0){lz[root] ^= 1;continue;}
105         if(x==0)lz[son[0][y]]^=1;
106         else if(y==0)lz[son[1][x]]^=1;
107         else lz[son[0][y]]^=1;
108     }
109     for(int i = 1;i<=n;++i)printf("%d ",val[getK(i)]);
110         puts("");
111     return 0;
112 }

posted on 2015-08-08 10:12  round_0  阅读(114)  评论(0编辑  收藏  举报