# bzoj3223 Tyvj 1729 文艺平衡树

5 3

1 3

1 3

1 4

4 3 2 1 5

## HINT

N,M<=100000

 1 //It is made by wfj_2048~
2 #include <algorithm>
3 #include <iostream>
4 #include <complex>
5 #include <cstring>
6 #include <cstdlib>
7 #include <cstdio>
8 #include <vector>
9 #include <cmath>
10 #include <queue>
11 #include <stack>
12 #include <map>
13 #include <set>
14 #define inf (1<<30)
15 #define N (100010)
16 #define il inline
17 #define RG register
18 #define ll long long
19 #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
20
21 using namespace std;
22
23 int ch[N][2],fa[N],sz[N],key[N],lazy[N],st[N],n,m,rt,tot;
24
25 il int gi(){
26     RG int x=0,q=1; RG char ch=getchar(); while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
27     if (ch=='-') q=-1,ch=getchar(); while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q*x;
28 }
29
30 il void newnode(RG int &x,RG int k,RG int f){ x=++tot,key[x]=k,sz[x]=1,fa[x]=f; return; }
31
32 il void pushdown(RG int x){ lazy[x]=0,lazy[ch[x][0]]^=1,lazy[ch[x][1]]^=1,swap(ch[x][0],ch[x][1]); return; }
33
34 il void pushup(RG int x){ sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1; return; }
35
36 il void rotate(RG int x){
37     RG int y=fa[x],z=fa[y],k=ch[y][0]==x;
38     ch[z][ch[z][1]==y]=x,fa[x]=z;
39     ch[y][k^1]=ch[x][k],fa[ch[x][k]]=y;
40     ch[x][k]=y,fa[y]=x,pushup(y),pushup(x); return;
41 }
42
43 il void splay(RG int x,RG int goal){
44     RG int top=0; st[++top]=x;
45     for (RG int i=x;fa[i]!=goal;i=fa[i]) st[++top]=fa[i];
46     for (RG int i=top;i;--i) if (lazy[st[i]]) pushdown(st[i]);
47     while (fa[x]!=goal){
48     RG int y=fa[x],z=fa[y];
49     if (fa[y]!=goal){
50         ((ch[z][0]==y)^(ch[y][0]==x)) ? rotate(x) : rotate(y);
51     }
52     rotate(x);
53     }
54     if (!goal) rt=x; return;
55 }
56
57 il void insert(RG int &x,RG int k){
58     while (ch[x][key[x]<k]) x=ch[x][key[x]<k];
59     newnode(ch[x][key[x]<k],k,x),splay(ch[x][key[x]<k],0); return;
60 }
61
62 il int find(RG int x,RG int key){
63     if (lazy[x]) pushdown(x); if (key==sz[ch[x][0]]+1) return x;
64     if (key<sz[ch[x][0]]+1) return find(ch[x][0],key);
65     else return find(ch[x][1],key-sz[ch[x][0]]-1);
66 }
67
68 il void print(RG int x){
69     if (lazy[x]) pushdown(x);
70     if (ch[x][0]) print(ch[x][0]);
71     if (key[x]>=1 && key[x]<=n) printf("%d ",key[x]);
72     if (ch[x][1]) print(ch[x][1]); return;
73 }
74
75 il void work(){
76     n=gi(),m=gi(),newnode(rt,1,0);
77     for (RG int i=2;i<=n;++i) insert(rt,i);
78     insert(rt,0),insert(rt,n+1);
79     for (RG int i=1;i<=m;++i){
80     RG int l=gi(),r=gi(),x;
81     x=find(rt,l),splay(x,0);
82     x=find(rt,r+2),splay(x,rt);
83     lazy[ch[x][0]]^=1;
84     }
85     print(rt); return;
86 }
87
88 int main(){
89     File("artist");
90     work();
91     return 0;
92 }

posted @ 2017-03-01 10:13  wfj_2048  阅读(138)  评论(0编辑  收藏  举报