Bzoj3223 Tyvj 1729 文艺平衡树

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 4419  Solved: 2561

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

树 平衡树 Splay

迷,博客里突然找不到这篇了……

直接Splay

  1 #include<algorithm>
  2 #include<cstring>
  3 #include<cmath>
  4 #include<cstdio>
  5 using namespace std;
  6 const int mxn=100010;
  7 int read(){
  8     int x=0,f=1;char ch=getchar();
  9     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 10     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
 11     return x*f;
 12 }
 13 struct node{
 14     int ch[2];
 15     int fa,size;
 16     bool rev;
 17 }t[mxn];
 18 int sz,rt;
 19 void pushdown(int x){
 20     if(t[x].rev){
 21         t[x].rev=0;
 22         t[t[x].ch[0]].rev^=1;
 23         t[t[x].ch[1]].rev^=1;
 24         swap(t[x].ch[0],t[x].ch[1]);
 25     }
 26     return;
 27 }
 28 void pushup(int x){
 29     t[x].size=t[t[x].ch[0]].size+t[t[x].ch[1]].size+1;
 30     return;
 31 }
 32 void rotate(int x,int &k){
 33     int y=t[x].fa;int z=t[y].fa;int lc,rc;
 34     if(t[y].ch[0]==x)lc=0;else lc=1;
 35     rc=lc^1;
 36     if(y==k)k=x;
 37     else t[z].ch[t[z].ch[1]==y]=x;
 38     t[x].fa=z;t[y].fa=x;
 39     t[t[x].ch[rc]].fa=y;
 40     t[y].ch[lc]=t[x].ch[rc]; 
 41     t[x].ch[rc]=y;
 42     pushup(y);
 43     pushup(x);
 44     return;
 45 }
 46 void Splay(int x,int &k){
 47     while(x!=k){
 48         int y=t[x].fa;int z=t[y].fa;
 49         if(y!=k){
 50             if((t[y].ch[0]==x)^(t[z].ch[0]==y))rotate(x,k);
 51             else rotate(y,k);
 52         }
 53         rotate(x,k);
 54     }
 55     return;
 56 }
 57 int find(int x,int rank){
 58     if(t[x].rev)pushdown(x);
 59     int lc=t[x].ch[0];int rc=t[x].ch[1];
 60     if(t[lc].size+1==rank)return x;
 61     else if(t[lc].size>=rank)return find(lc,rank);
 62         else return find(rc,rank-t[lc].size-1);
 63 }
 64 void split(int L,int R){
 65     int x=find(rt,L);
 66     int y=find(rt,R);
 67     Splay(x,rt);
 68     Splay(y,t[x].ch[1]);
 69     return;
 70 }
 71 void rev(int L,int R){
 72     split(L,R+2);
 73     int x=t[t[rt].ch[1]].ch[0];
 74     t[x].rev^=1;
 75     return;
 76 }
 77 void Build(int L,int R,int fa){
 78     if(L>R)return;
 79     int now=L;int last=fa;
 80     if(L==R){
 81         t[L].fa=fa;t[L].size=1;
 82         if(L<fa) t[fa].ch[0]=L;
 83         else t[fa].ch[1]=L;
 84         return;
 85     }
 86     int mid=(L+R)>>1;
 87     Build(L,mid-1,mid);Build(mid+1,R,mid);
 88     t[mid].fa=fa;
 89     if(mid<fa) t[fa].ch[0]=mid;
 90         else t[fa].ch[1]=mid;
 91     pushup(mid);
 92     return;
 93 }
 94 int n,m;
 95 int main()
 96 {
 97     n=read();
 98     Build(1,n+2,0);
 99     rt=(n+3)>>1;
100     m=read();
101     int i,j,l,r;
102     while(m--){
103         l=read();r=read();
104         rev(l,r);
105 //      for(i=2;i<=n+1;i++)printf("%d ",find(rt,i)-1);
106 //      printf("
");
107     }
108     for(i=2;i<=n+1;i++)printf("%d ",find(rt,i)-1);
109     return 0;
110 }
111 
原文地址:https://www.cnblogs.com/SilverNebula/p/6677361.html