【题目描述】

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

【输入格式】

第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n)  m表示翻转操作次数

接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n

【输出格式】

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

【样例输入】

5 3
1 3
1 3
1 4

【样例输出】

4 3 2 1 5

【数据范围】

N,M<=100000

【来源】

Tyvj 1729

solution

板子题,但是从这个板子题里,我真是学会了很多东西

在调的很长时间里,我甚至思考过人生

体会了绝处逢生的感觉

有两个地方:

1.在kth和rot里都要pushdown

2.INF哨兵节点的父亲一定要=-INF的  (一开始) (就因为这个,我调了很长时间)

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<iostream>
  4 #define ls(x) ((x)->ch[0])
  5 #define rs(x) ((x)->ch[1])
  6 #define sz(x) ((x)->size)
  7 #define p(x) ((x)->p)
  8 using namespace std;
  9 const int INF=0x7fffffff;
 10 struct son
 11 {
 12     int v,size,sw;
 13     son *ch[2],*p;
 14     son(int val,son *fa)
 15     {
 16         v=val;size=1;sw=0;p=fa;
 17     }
 18 }*root,*null;
 19 int n,m;
 20 void pushup(son *x){sz(x)=sz(ls(x))+sz(rs(x))+1;}
 21 void swap(son *&a,son *&b){son *temp=a;a=b;b=temp;}
 22 void change(son *x)
 23 {
 24     if(x==null)return ;
 25     x->sw^=1;
 26     //printf("1  %d %d\n",ls(x)->v,rs(x)->v);
 27     swap(ls(x),rs(x));
 28     //printf("2  %d %d\n",ls(x)->v,rs(x)->v);
 29 }
 30 void pushdown(son *x)
 31 {
 32     if(!x->sw||x==null)return ;
 33     change(ls(x));change(rs(x));x->sw=0;
 34 }
 35 int islc(son *x){return ls(p(x))==x;}
 36 // is left child????
 37
 38 void rot(son *y)//x:父亲 y:儿子 z:祖父
 39 {
 40     //printf("yrot=%d\n",y->v);
 41     son *x=p(y);int d=islc(y);
 42     pushdown(x);pushdown(y);//******
 43     if(p(x)==null)root=y;
 44     else p(x)->ch[islc(x)^1]=y;//这个地方 warnnig
 45     p(y)=p(x);//
 46     x->ch[d^1]=y->ch[d];
 47     if(y->ch[d])p(y->ch[d])=x;//
 48     y->ch[d]=x;p(x)=y;//
 49     pushup(x);pushup(y);
 50 }
 51
 52 /*void rot(son *x,int d)//x:父亲 y:儿子 z:祖父
 53 {
 54     son *y=x->ch[d^1];
 55     pushdown(x);pushdown(y);//******
 56     if(p(x)!=null)p(x)->ch[islc(x)^1]=y;
 57     else root=y;
 58     p(y)=p(x);//
 59     x->ch[d^1]=y->ch[d];
 60     if(y->ch[d])p(y->ch[d])=x;//
 61     y->ch[d]=x;p(x)=y;//
 62     pushup(x);pushup(y);
 63 }*/
 64
 65 void bianli(son *x);
 66
 67 void splay(son *x,son *t)//x:儿子 y:父亲
 68 {
 69     //if(t==root)printf("YES\n");
 70     //printf("beginsplay %d\n",x->v);
 71     while(p(x)!=t)
 72     {
 73         //printf("ci\n");
 74         son *y=p(x);
 75         if(p(y)!=t)
 76         {
 77             if(islc(x)==islc(y))rot(y);
 78             else rot(x);
 79         }
 80         //bianli(root);
 81         //printf("x=%d y=%d\n",x->v,y->v);
 82         //printf("p(x)->val=%d\n",p(x)->v);
 83         //printf("p(p(x))=%d\n",p(p(x))->v);
 84         rot(x);
 85         //bianli(root);
 86         //printf("\n");
 87     }
 88 }
 89
 90 /*void splay(son *x,son *t)//x:儿子 y:父亲
 91 {
 92     if(t==root)printf("YES\n");
 93     //printf("beginsplay\n");
 94     while(p(x)!=t)
 95     {
 96         printf("ci\n");
 97         son *y=p(x);
 98         if(p(y)!=t)islc(x)==islc(y)?rot(y):rot(x);
 99         rot(x);
100     }
101 }*/
102 son* kth(int k)
103 {
104     //printf("beginkth\n");
105     son *x=root;
106     while(x!=null)
107     {//printf("endkth\n");
108         //printf("k=%d\n",k);
109         pushdown(x);//******
110         if(sz(ls(x))+1==k){return x;}
111         if(sz(ls(x))+1>k)x=ls(x);
112         else {k-=(sz(ls(x))+1);x=rs(x);}
113     }
114 }
115
116
117 void Splay(int l,int r)
118 {
119     //printf("l=%d r=%d\n",l,r);
120     son *zuo=kth(l);
121
122     //printf("zuo=%d\n",zuo->v);
123     splay(zuo,null);
124     //bianli(root);
125     //printf("2\n");
126     son *you=kth(r+2);
127     //printf("you=%d\n",you->v);
128     splay(you,root);
129     //bianli(root);
130     //cout<<0;
131     change(root->ch[1]->ch[0]);
132 }
133 son* build(int l,int r,son *fa)
134 {
135     if(l>r)return null;
136     int mid=(l+r)>>1;
137     son *x=new son(mid,fa);
138     x->ch[0]=build(l,mid-1,x);
139     x->ch[1]=build(mid+1,r,x);
140     pushup(x);
141     return x;
142 }
143
144 void bianli(son *x)
145 {
146     if(x==null)return ;
147     pushdown(x);
148     //printf("%d ",x->size);
149     //printf("%d ",x->v);
150     bianli(ls(x));
151     if(x->v!=INF&&x->v!=-INF)
152       printf("%d ",x->v);
153     bianli(rs(x));
154 }
155
156 int main(){
157     //freopen("sph.in","r",stdin);
158     //freopen("sph.out","w",stdout);
159     null=new son(0,0);null->size=0;
160     root=new son(-INF,null);
161     root->ch[1]=new son(INF,root);//son(INF,root) 我竟然写成了son(INF,null) 调了一下午
162     root->ch[0]=null;
163     scanf("%d%d",&n,&m);
164     //printf("n=%d m=%d\n",n,m);
165     root->ch[1]->ch[0]=build(1,n,root->ch[1]);
166     root->ch[1]->ch[1]=null;
167     pushup(root->ch[1]);
168     pushup(root);
169
170     //if(root==null)
171     //  printf("what the fuck!!!\n");
172
173     //bianli(root);
174
175     for(int i=1;i<=m;++i)
176     {
177         int qq,ww;
178         scanf("%d%d",&qq,&ww);
179         Splay(qq,ww);
180         //printf("bianli=  ");
181         //bianli(root);
182         //printf("\n");
183         //bianli(root);
184     }
185     //printf("\n");
186     bianli(root);
187     //while(1);
188     return 0;
189 }

调了一个下午,未删注释

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<iostream>
  4 #define ls(x) ((x)->ch[0])
  5 #define rs(x) ((x)->ch[1])
  6 #define sz(x) ((x)->size)
  7 #define p(x) ((x)->p)
  8 using namespace std;
  9 const int INF=0x7fffffff;
 10 struct son
 11 {
 12     int v,size,sw;
 13     son *ch[2],*p;
 14     son(int val,son *fa)
 15     {
 16         v=val;size=1;sw=0;p=fa;
 17     }
 18 }*root,*null;
 19 int n,m;
 20 void pushup(son *x){sz(x)=sz(ls(x))+sz(rs(x))+1;}
 21 void swap(son *&a,son *&b){son *temp=a;a=b;b=temp;}
 22 void change(son *x)
 23 {
 24     if(x==null)return ;
 25     x->sw^=1;
 26     swap(ls(x),rs(x));
 27 }
 28 void pushdown(son *x)
 29 {
 30     if(!x->sw||x==null)return ;
 31     change(ls(x));change(rs(x));x->sw=0;
 32 }
 33 int islc(son *x){return ls(p(x))==x;}
 34 // is left child????
 35
 36 void rot(son *y)//x:父亲 y:儿子 z:祖父
 37 {
 38     son *x=p(y);int d=islc(y);
 39     pushdown(x);pushdown(y);//******
 40     if(p(x)==null)root=y;
 41     else p(x)->ch[islc(x)^1]=y;//这个地方 warnnig
 42     p(y)=p(x);//
 43     x->ch[d^1]=y->ch[d];
 44     if(y->ch[d])p(y->ch[d])=x;//
 45     y->ch[d]=x;p(x)=y;//
 46     pushup(x);pushup(y);
 47 }
 48 void bianli(son *x);
 49
 50 void splay(son *x,son *t)//x:儿子 y:父亲
 51 {
 52     while(p(x)!=t)
 53     {
 54         son *y=p(x);
 55         if(p(y)!=t)islc(x)==islc(y)?rot(y):rot(x);
 56         rot(x);
 57     }
 58 }
 59 son* kth(int k)
 60 {
 61     son *x=root;
 62     while(x!=null)
 63     {
 64         pushdown(x);//******
 65         if(sz(ls(x))+1==k){return x;}
 66         if(sz(ls(x))+1>k)x=ls(x);
 67         else {k-=(sz(ls(x))+1);x=rs(x);}
 68     }
 69 }
 70 void Splay(int l,int r)
 71 {
 72     splay(kth(l),null);
 73     splay(kth(r+2),root);
 74     change(root->ch[1]->ch[0]);
 75 }
 76 son* build(int l,int r,son *fa)
 77 {
 78     if(l>r)return null;
 79     int mid=(l+r)>>1;
 80     son *x=new son(mid,fa);
 81     x->ch[0]=build(l,mid-1,x);
 82     x->ch[1]=build(mid+1,r,x);
 83     pushup(x);
 84     return x;
 85 }
 86
 87 void bianli(son *x)
 88 {
 89     if(x==null)return ;
 90     pushdown(x);
 91     bianli(ls(x));
 92     if(x->v!=INF&&x->v!=-INF)
 93       printf("%d ",x->v);
 94     bianli(rs(x));
 95 }
 96
 97 int main(){
 98     //freopen("sph.in","r",stdin);
 99     //freopen("sph.out","w",stdout);
100     null=new son(0,0);null->size=0;
101     root=new son(-INF,null);
102     root->ch[1]=new son(INF,root);//son(INF,root) 我竟然写成了son(INF,null) 调了一下午
103     root->ch[0]=null;
104     scanf("%d%d",&n,&m);
105     root->ch[1]->ch[0]=build(1,n,root->ch[1]);
106     root->ch[1]->ch[1]=null;
107     pushup(root->ch[1]);
108     pushup(root);
109     for(int i=1;i<=m;++i)
110     {
111         int qq,ww;
112         scanf("%d%d",&qq,&ww);
113         Splay(qq,ww);
114     }
115     bianli(root);
116     //while(1);
117     return 0;
118 }

删注释

转载于:https://www.cnblogs.com/A-LEAF/p/7281681.html

[HZOI 2016][Tyvj 1729]文艺平衡树 这道题我真是哭了,调了一下午,一晚上相关推荐

  1. BZOJ 3223: Tyvj 1729 文艺平衡树(splay)

    速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...

  2. [Tyvj 1729] 文艺平衡树

    题面如下: Tyvj 1729 文艺平衡树 Time Limit: 1 Sec  Memory Limit: 128 MB Description 您需要写一种数据结构(可参考题目标题),来维护一个有 ...

  3. BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 6881  Solved: 4213 [Submit][S ...

  4. splay区间翻转(bzoj 3223: Tyvj 1729 文艺平衡树)

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 4854  Solved: 2844 [Submit][S ...

  5. fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)...

    题面: [模板]文艺平衡树(Splay) 题解:无 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<iostr ...

  6. bzoj 3223: Tyvj 1729 文艺平衡树

    Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 2853  Solved: 1602 [Submit][Status][Discuss] Descri ...

  7. BZOJ Tyvj 1729 文艺平衡树

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

  8. 【Splay】【块状链表】bzoj3223 Tyvj 1729 文艺平衡树

    让蒟蒻见识到了常数大+滥用STL的危害. <法一>很久之前的Splay #include<cstdio> #include<algorithm> using nam ...

  9. BZOJ3223-Tyvj 1729 文艺平衡树

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

最新文章

  1. 机器学习必读TOP 100论文清单:高引用、分类全、覆盖面广丨GitHub 21.4k星
  2. 【PP操作手册】运行MRP产生计划订单
  3. 我的MYSQL学习心得(一)
  4. 一道非常简单的签到题
  5. 阿里DNS:用LibFuzzer照亮DNS代码的死角
  6. mysql创建非聚集索引_一文让你明白聚集索引和非聚集索引?
  7. c++ java setobjectarrayelement_java中jni的使用:C/C++操作java中的数组
  8. MySQL 8小时空闲后连接失效的解决
  9. JavaScript数据类型之Undefined、Null、Boolean
  10. 《第一本docker书》第4章 使用docker镜像和仓库 读书笔记
  11. EditPlus 3 实现将JSON字符串格式化、排版成JSON格式数据
  12. 数据库sql脚本--省市县生成
  13. 2017年北京邮电大学计算机考研机试试题及答案
  14. 读文献——《ImageNet classification with deep convolutional neural networks》
  15. 组织架构图怎么画?思维导图创作教程分享
  16. 企业提高客户保持率基本方法
  17. ES6(ECMAScript6)-ES11 从入门到修仙
  18. mysql中的临时表怎么用的?
  19. 【观察】华为IoT首席架构师王启军:云计算时代全栈工程师的养成
  20. 初识mysql实验小结_初识mysql学习笔记

热门文章

  1. 有关计算机基础的论文,有关计算机基础论文范文.doc
  2. hikaridatasource连接池_细数springboot中的那些连接池
  3. php面试hr要看你的项目,昨晚hr给了我一个面试题,说过了就安排我面试
  4. java代码的运行顺序_java中的代码块执行顺序
  5. 底层实现_Redis有序集合zset的底层实现
  6. java.lang.NoClassDefFoundError comfasterxmljacksonannotationJsonView
  7. webspere php,Project Zero、WebSphere sMash、PHP和JAVA的整合
  8. 浏览器的referer是服务器修改的吗,利用浏览器调整http的referer
  9. nexus 6p Android SDK,Flutter没有检测到Android SDK
  10. pgsql数据库默认配置事务类型_PostgreSQL数据库事务出现未知状态的处理方法