码迷,mamicode.com
首页 > 其他好文 > 详细

3391 模板Splay

时间:2018-01-13 17:02:49      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:清零   using   mes   没有   画图   bit   bsp   目标   getc   

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 #define maxn 200000
  4 int read()
  5 {
  6     char ch=getchar();int f=1,w=0;
  7     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
  8     while(ch<=9&&ch>=0){w=w*10+ch-0;ch=getchar();}
  9     return f*w;
 10 }
 11 
 12 int n,root,m,tot;
 13 
 14 struct sj
 15 {
 16     int ch[2];            //左儿子和右儿子
 17     int ff,v;            //ff是父亲 v
 18     int size;            //size是儿子节点个数
 19     int mark;            //打上的标记
 20     void init(int x,int fa)                                    //初始化一个节点 左儿子和右儿子以及父亲都清零
 21         {
 22             ff=ch[0]=ch[1]=0;                                                    
 23             size=1;v=x;ff=fa;                                //含有子节点个数为1 父亲节点为参数fa  
 24         }
 25 }t[maxn];
 26 
 27 inline void pushup(int x)
 28 {
 29     t[x].size=t[t[x].ch[0]].size+t[t[x].ch[1]].size+1;
 30 }
 31 
 32 inline void pushdown(int x)
 33 {
 34     if(t[x].mark)
 35     {
 36         t[t[x].ch[0]].mark^=1;
 37         t[t[x].ch[1]].mark^=1;
 38         t[x].mark=0;
 39         swap(t[x].ch[0],t[x].ch[1]);
 40     }
 41 }
 42 
 43 inline void rotate(int x)                //旋转函数
 44 {
 45     int y=t[x].ff;        
 46     int z=t[y].ff;                        //这是爷爷节点                
 47     int k=t[y].ch[1]==x;                //看x是左儿子还是右儿子 k取0,1
 48     t[z].ch[t[z].ch[1]==y]=x;            //这是把爷爷节点的左/右儿子变成x
 49     t[x].ff=z;                            
 50     t[y].ch[k]=t[x].ch[k^1];            
 51     t[t[x].ch[k^1]].ff=y;
 52     t[x].ch[k^1]=y;
 53     t[y].ff=x;                            //此段画图理解
 54     pushup(y);pushup(x);                //再继续进行修改 push_up();
 55 }
 56 
 57 inline void splay(int x,int goal)
 58 {
 59     while(t[x].ff!=goal)                //当我的这个节点还没有达到目标位置
 60     {    
 61         int y=t[x].ff;int z=t[y].ff;
 62         if(z!=goal)                        //如果还没有跳到目标节点 继续旋转
 63         (t[z].ch[1]==y)^(t[y].ch[1]==x)?rotate(x):rotate(y);        //Warning:: !!!!同边现象时要先翻爸爸再翻儿子 否则不满足搜索树的性质!!!!
 64         rotate(x);
 65     }
 66     if(goal==0)root=x;                    //如果目标节点是0 那么就把root 根节点变成x
 67 }
 68 
 69 inline void insert(int x)
 70 {
 71     int u=root,ff=0;                    //从根节点开始插入 一开始父亲变成0
 72     while(t[u].size!=0)ff=u,u=t[u].ch[x>t[u].v];    //有根的时候 ff父亲变成根节点 u再继续下去向下操作
 73     u=++tot;                            //tot是实时增加的一个记录子节点个数的全局变量
 74     if(ff)t[ff].ch[x>t[ff].v]=u;        //如果父亲不为零 他的父亲的左/右儿子就是u
 75     t[u].init(x,ff);                    //初始化这个节点 并且它的父亲就是ff
 76     splay(u,0);                            //把这个点旋转到根节点
 77 }
 78 
 79 inline int get_k(int k)
 80 {
 81     int u=root;
 82     while(1)                                        
 83     {
 84         pushdown(u);
 85         if(t[t[u].ch[0]].size>=k)u=t[u].ch[0];
 86         else if(t[t[u].ch[0]].size+1==k)return u;
 87         else k-=t[t[u].ch[0]].size+1,u=t[u].ch[1];
 88     }
 89 }
 90 
 91 void write(int u)
 92 {
 93     pushdown(u);                                //最后输出之前先push_down一遍
 94     if(t[u].ch[0])write(t[u].ch[0]);            //先读左儿子
 95     if(t[u].v>1&&t[u].v<n+2)printf("%d ",t[u].v-1);        
 96     if(t[u].ch[1])write(t[u].ch[1]);
 97 }
 98 
 99 inline void work(int l,int r)            //工作函数
100 {
101     l=get_k(l);
102     r=get_k(r+2);
103     splay(l,0);
104     splay(r,l);
105     t[t[t[root].ch[1]].ch[0]].mark^=1;
106 }
107 
108 int main()
109 {
110     n=read();m=read();                    //n个点 m次翻转
111     for(int i=1;i<=n+2;++i)insert(i);    //跳过建树 直接插入即可 再继续看insert()
112     for(int i=1;i<=m;i++)
113     {
114         int l=read(),r=read();
115         work(l,r);                        //工作函数
116     }
117     write(root);                        //输出;                        
118     return 0;                            //大工告吉
119 }

 

3391 模板Splay

标签:清零   using   mes   没有   画图   bit   bsp   目标   getc   

原文地址:https://www.cnblogs.com/Kv-Stalin/p/8279239.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!