bzoj3223 Tyvj 1729 文艺平衡树

Description

您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 

Input

第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n)  m表示翻转操作次数
接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n 

Output

输出一行n个数字,表示原始序列经过m次变换后的结果 

Sample Input

5 3

1 3

1 3

1 4

Sample Output

4 3 2 1 5

HINT

N,M<=100000

正解:splay。

其实我原来就做过,但是这回又调了很久。。find本来是非递归的,但是跑不出来,最后没办法改成递归版才过。。

把l-1旋到根,r+1旋到根的右儿子,然后r+1的左儿子打标记。记得随时下放。

 

 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  阅读(162)  评论(0编辑  收藏  举报