zoukankan      html  css  js  c++  java
  • [HZOI 2016][Tyvj 1729]文艺平衡树 这道题我真是哭了,调了一下午,一晚上

    【题目描述】

    您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是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
    ",ls(x)->v,rs(x)->v);
     27     swap(ls(x),rs(x));
     28     //printf("2  %d %d
    ",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
    ",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
    ");
     70     //printf("beginsplay %d
    ",x->v);
     71     while(p(x)!=t)
     72     {
     73         //printf("ci
    ");
     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
    ",x->v,y->v);
     82         //printf("p(x)->val=%d
    ",p(x)->v);
     83         //printf("p(p(x))=%d
    ",p(p(x))->v);
     84         rot(x);
     85         //bianli(root);
     86         //printf("
    ");
     87     }
     88 }
     89 
     90 /*void splay(son *x,son *t)//x:儿子 y:父亲 
     91 {
     92     if(t==root)printf("YES
    ");
     93     //printf("beginsplay
    ");
     94     while(p(x)!=t)
     95     {
     96         printf("ci
    ");
     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
    ");
    105     son *x=root;
    106     while(x!=null)
    107     {//printf("endkth
    ");
    108         //printf("k=%d
    ",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
    ",l,r);
    120     son *zuo=kth(l);
    121     
    122     //printf("zuo=%d
    ",zuo->v);
    123     splay(zuo,null);
    124     //bianli(root);
    125     //printf("2
    ");
    126     son *you=kth(r+2);
    127     //printf("you=%d
    ",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,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!!!
    ");
    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("
    ");
    183         //bianli(root);
    184     }
    185     //printf("
    ");
    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 }
    删注释
  • 相关阅读:
    lerna管理前端packages的最佳实践
    理解Python闭包,这应该是最好的例子
    Maven 初学+http://mvnrepository.com/
    逆向工程,调试Hello World !程序(更新中)
    github设置仓库可见性 私人仓库设置他人协作/可见
    flink 1.9.0 编译:flink-fs-hadoop-shaded 找不到
    中通消息平台 Kafka 顺序消费线程模型的实践与优化
    以友盟+U-Push为例,深度解读消息推送的筛选架构解决方案应用与实践
    idea新建spring boot项目使用maven引入依赖失败,pom.xml文件中project标签报错
    RabbitMQ (十二) 消息确认机制
  • 原文地址:https://www.cnblogs.com/A-LEAF/p/7281681.html
Copyright © 2011-2022 走看看