[bzoj3223]文艺平衡树

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

Source

平衡树

 

做完了维修数列之后,这就是个大水题

按题意反转序列,最后更新所有节点,输出中序遍历即可。。。。。。

 

 1 // It is made by XZZ
 2 #include<cstdio>
 3 #include<algorithm>
 4 #define Fname "sph"
 5 using namespace std;
 6 #define rep(a,b,c) for(rg int a=b;a<=c;a++)
 7 #define drep(a,b,c) for(rg int a=b;a>=c;a--)
 8 #define erep(a,b) for(rg int a=fir[b];a;a=nxt[a])
 9 #define il inline
10 #define rg register
11 #define vd void
12 #define Now t[now]
13 #define pr pair<int,int>
14 #define mp make_pair
15 typedef long long ll;
16 il int gi(){
17     rg int x=0,f=1;rg char ch=getchar();
18     while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
19     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
20     return x*f;
21 }
22 ll seed=19260817;
23 int root;
24 const int maxn=101010;
25 il int Rand(){return (int)(seed=seed*48271%2147483647);}
26 struct node{int data,size,ls,rs,rand;bool rev;}t[maxn];
27 int newnode(int i){t[i]=(node){i,1,0,0,Rand(),0};return i;}
28 il vd down(int now){
29     if(now&&Now.rev)swap(Now.ls,Now.rs),Now.rev=0,t[Now.ls].rev^=1,t[Now.rs].rev^=1;
30 }
31 il vd reset(int now){
32     down(Now.ls),down(Now.rs);
33     Now.size=t[Now.ls].size+t[Now.rs].size+1;
34 }
35 il int build(int n){
36     int stack[maxn],top=0,last;
37     rep(i,1,n){
38     int now=newnode(i);last=0;
39     while(top&&t[stack[top]].rand>Now.rand)reset(stack[top]),last=stack[top--];
40     if(top)t[stack[top]].rs=now;Now.ls=last,stack[++top]=now;
41     }
42     while(top)reset(stack[top--]);
43     return stack[1];
44 }
45 il int merge(int a,int b){
46     if(!a||!b)return a|b;
47     if(t[a].rand<t[b].rand){down(a),t[a].rs=merge(t[a].rs,b),reset(a);return a;}
48     else{down(b),t[b].ls=merge(a,t[b].ls),reset(b);return b;}
49 }
50 il pr split(int now,int num){
51     if(!now)return mp(0,0);
52     down(now);
53     int ls=Now.ls,rs=Now.rs;
54     if(num==t[Now.ls].size){Now.ls=0,reset(now);return mp(ls,now);}
55     if(num==t[Now.ls].size+1){Now.rs=0,reset(now);return mp(now,rs);}
56     if(num<t[Now.ls].size){
57     pr T=split(Now.ls,num);
58     Now.ls=T.second,reset(now);
59     return mp(T.first,now);
60     }else{
61     pr T=split(Now.rs,num-t[Now.ls].size-1);
62     Now.rs=T.first,reset(now);
63     return mp(now,T.second);
64     }
65 }
66 il vd dfs(int now){
67     if(now)down(Now.ls),down(Now.rs),dfs(Now.ls),printf("%d ",Now.data),dfs(Now.rs);
68 }
69 int main(){
70     freopen(Fname".in","r",stdin);
71     freopen(Fname".out","w",stdout);
72     int n=gi(),m=gi();
73     root=build(n);
74     while(m--){
75     int pos=gi(),tot=gi()-pos+1;
76     pr T=split(root,pos-1),TT=split(T.second,tot);
77     t[TT.first].rev^=1;
78     root=merge(T.first,merge(TT.first,TT.second));
79     }
80     down(root),dfs(root);
81     return 0;
82 }
View Code

 

posted @ 2017-08-04 17:12  菜狗xzz  阅读(140)  评论(0编辑  收藏  举报