[Tvyj 1729]文艺平衡树
题目
您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1INPUT
第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n) m表示翻转操作次数接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=nOUTPUT
输出一行n个数字,表示原始序列经过m次变换后的结果SAMPLE
INPUT
5 31 31 31 4OUTPUT
4 3 2 1 5
解题报告
板子题,Splay,fhq-Treap什么的,我半个都不会呢= =
上板子= =
1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 using namespace std; 6 inline int read(){ 7 int sum(0); 8 char ch(getchar()); 9 while(ch<'0'||ch>'9') 10 ch=getchar(); 11 while(ch>='0'&&ch<='9'){ 12 sum=sum*10+ch-'0'; 13 ch=getchar(); 14 } 15 return sum; 16 } 17 int n,m,top; 18 struct node{ 19 int v,key,size,mark; 20 node *ch[2]; 21 node(int x=0):size(1),key(rand()),mark(0),v(x){ 22 ch[0]=ch[1]=NULL; 23 } 24 inline void revs(); 25 inline void pushup(); 26 inline void pushdown(){ 27 if(mark){ 28 if(ch[0]) 29 ch[0]->revs(); 30 if(ch[1]) 31 ch[1]->revs(); 32 mark=0; 33 } 34 } 35 }*root,*st[100005]; 36 typedef pair<node*,node*>p; 37 inline void swp(node *x,node *y){ 38 node *tmp(x); 39 x=y; 40 y=tmp; 41 } 42 inline int get_size(node *x){ 43 if(x==NULL) 44 return 0; 45 return x->size; 46 } 47 inline void node::revs(){ 48 mark^=1; 49 swap(ch[0],ch[1]); 50 } 51 inline void node::pushup(){ 52 size=1; 53 size+=get_size(ch[0])+get_size(ch[1]); 54 } 55 inline node* build(){ 56 node *x,*las; 57 for(int i=1;i<=n;i++){ 58 x=new node(i); 59 las=NULL; 60 while(top&&st[top]->key>x->key){ 61 st[top]->pushup(); 62 las=st[top]; 63 st[top--]=NULL; 64 } 65 if(top) 66 st[top]->ch[1]=x; 67 x->ch[0]=las; 68 st[++top]=x; 69 } 70 while(top) 71 st[top--]->pushup(); 72 return st[1]; 73 } 74 inline node* merge(node *x,node *y){ 75 if(x==NULL) 76 return y; 77 if(y==NULL) 78 return x; 79 if(x->key<y->key){ 80 x->pushdown(); 81 x->ch[1]=merge(x->ch[1],y); 82 x->pushup(); 83 return x; 84 } 85 else{ 86 y->pushdown(); 87 y->ch[0]=merge(x,y->ch[0]); 88 y->pushup(); 89 return y; 90 } 91 } 92 inline p split(node *x,int k){ 93 if(!x) 94 return p(NULL,NULL); 95 p y; 96 x->pushdown(); 97 if(get_size(x->ch[0])>=k){ 98 y=split(x->ch[0],k); 99 x->ch[0]=y.second; 100 x->pushup(); 101 y.second=x; 102 } 103 else{ 104 y=split(x->ch[1],k-get_size(x->ch[0])-1); 105 x->ch[1]=y.first; 106 x->pushup(); 107 y.first=x; 108 } 109 return y; 110 } 111 inline int rk(node *rt,int x){ 112 if(!rt) 113 return 0; 114 return x<rt->v?rk(rt->ch[0],x):rk(rt->ch[1],x)+get_size(rt->ch[0])+1; 115 } 116 inline int kth(int k){ 117 p x(split(root,k-1)),y(split(x.second,1)); 118 node *tmp(y.first); 119 root=merge(merge(x.first,tmp),y.second); 120 return tmp->v; 121 } 122 inline void insert(int x){ 123 int k(rk(root,x)); 124 p tp(split(root,k)); 125 node *tmp(new node(x)); 126 root=merge(merge(tp.first,tmp),tp.second); 127 } 128 inline void print(node *x){ 129 if(!x) 130 return; 131 x->pushdown(); 132 print(x->ch[0]); 133 printf("%d ",x->v); 134 print(x->ch[1]); 135 } 136 inline int gg(){ 137 srand(time(NULL)); 138 n=read(),m=read(); 139 root=build(); 140 for(int i=1;i<=m;i++){ 141 int x(read()),y(read()); 142 if(x==y) 143 continue; 144 p tmp1(split(root,x-1)),tmp2(split(tmp1.second,y-x+1)); 145 tmp2.first->revs(); 146 root=merge(tmp1.first,merge(tmp2.first,tmp2.second)); 147 } 148 print(root); 149 } 150 int k(gg()); 151 int main(){;}
代码极其漂(chou)亮(lou),请慢(gan)慢(jin)欣(tui)赏(chu)