hdu-2475-Box-splay
来源:互联网 发布:老司机测试 知乎 编辑:程序博客网 时间:2024/05/05 16:45
伸展树是那么的迷人,总是让你在希望的时候WA,在绝望的时候AC。。。。。
做这个题没有之前,首先要了解树形结构转线性的做法。
推荐连接:http://blog.csdn.net/lyhypacm/article/details/6734748
本题做法:
对于本题的每一个最外层的盒子建立一颗伸展树。
对于move x y操作:
(1)把x为根的子树切下来。
(2)然后把切下来的子树放入y的后面。
对于query x操作:
(1)直接查询x为根的子树的最小点就可以了。
#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<vector>using namespace std;#define maxn 110000#define mem(a,b) memset(a,b,sizeof(a))#define root10 ch[ch[root][1]][0]#define root1 ch[root][1]#define root11 ch[ch[root][1]][1]#define lson ch[x][0]#define rson ch[x][1]int ch[maxn][2];int pre[maxn];int root,tot;int size[maxn];int val[maxn];//----------------------int l[maxn];int r[maxn];vector<int>vec[maxn];vector<int>ans;vector<int>tree;void Treaval(int x){ if(x) { Treaval(ch[x][0]); printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d ,key = %2d \n",x,ch[x][0],ch[x][1],pre[x],size[x],val[x]); Treaval(ch[x][1]); }}void debug(){ printf("root:%d\n",root); Treaval(root);}void debug2(){ puts("start:"); for(int x=1;x<=tot;x++) { printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d ,key = %2d \n",x,ch[x][0],ch[x][1],pre[x],size[x],val[x]); }}//以上Debugvoid push_down(int x){}void push_up(int x){}void rot(int x,int kind){ int y=pre[x]; push_down(y); push_down(x); ch[y][!kind]=ch[x][kind]; pre[ch[x][kind]]=y; if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x; pre[x]=pre[y]; ch[x][kind]=y; pre[y]=x; push_up(y); push_up(x);}void splay(int x,int goal){ push_down(x); while(pre[x]!=goal) { if(pre[pre[x]]==goal) { push_down(pre[x]); push_down(x); rot(x,ch[pre[x]][0]==x); } else { int y=pre[x]; push_down(pre[y]); push_down(y); push_down(x); int kind=ch[pre[y]][0]==y; if(ch[y][kind]==x) { rot(x,!kind); rot(x,kind); } else { rot(y,kind); rot(x,kind); } } } push_up(x); if(goal==0)root=x;}void init(){ root=tot=0; memset(ch,0,sizeof(ch)); memset(pre,0,sizeof(pre));}void newnode(int &x,int k,int father){ x=++tot; pre[x]=father; size[x]=1; ch[x][0]=ch[x][1]=0; val[x]=k; if(k<0)r[-k]=x; else l[k]=x;}void buildtree(int &x,int l,int r,int father){ if(l>r)return; int mid=(l+r)/2; newnode(x,ans[mid],father); buildtree(ch[x][0],l,mid-1,x); buildtree(ch[x][1],mid+1,r,x); push_up(x);}int get_max(int r){ push_down(r); while(ch[r][1]) { r=ch[r][1]; push_down(r); } return r;}int get_min(int r){ push_down(r); while(ch[r][0]) { r=ch[r][0]; push_down(r); } return r;}int query(int x){ int ll=l[x]; splay(ll,0); int y=get_min(ll); return val[y];}void dos(int a,int b){ int x,y,z; x=l[a];y=r[a];z=y; splay(x,0); splay(y,0); int ll,rr,conn; ll=ch[x][0]; rr=ch[y][1]; conn=get_max(ll); pre[ll]=pre[rr]=0; ch[x][0]=ch[y][1]=0; if(conn!=0)ch[conn][1]=rr; pre[rr]=conn; if(b==0)return; if(query(b)==a) { ch[conn][1]=0;pre[rr]=0; ch[x][0]=ll;ch[y][1]=rr; pre[ll]=x; pre[rr]=y; pre[0]=0; return; } if(rr!=0)splay(rr,0); x=l[b]; splay(x,0); y=get_min(ch[x][1]); splay(y,x); ch[y][0]=z;pre[z]=y;}void dfs(int x){ ans.push_back(x); for(int i=0; i<vec[x].size(); i++) { dfs(vec[x][i]); } ans.push_back(-x);}int main(){ int n,x; int cas=0; while(~scanf("%d",&n)) { if(cas)puts(""); cas++; init(); for(int i=0; i<=n; i++)vec[i].clear(); ans.clear(); for(int i=1; i<=n; i++) { scanf("%d",&x); vec[x].push_back(i); } dfs(0); int len=0; int st=1; for(int i=1; i<ans.size()-1; i++) { if(ans[i]>0)len++; else len--; if(len==0) { buildtree(root,st,i,0); st=i+1; } } int m,x,y; char str[1110]; scanf("%d",&m); while(m--) { cas++; scanf("%s",str); if(str[0]=='M') { scanf("%d%d",&x,&y); dos(x,y); } if(str[0]=='Q') { scanf("%d",&x); printf("%d\n",query(x)); } } } return 0;}
0 0
- hdu-2475-Box-splay
- hdu 2475 Box (splay tree)
- hdu 2475 Box splay, lct
- HDU 2475 Box [dfs序+splay]
- HDU 2475Box
- hdu 2475 box LCT
- hdu2475 Box splay || 动态树
- hdu2475 BOX(splay+dfs序)
- hdu 2475 Box(伸展树)
- HDU 2475 Box 树型转线型 + 伸展树
- 【splay树】hdu 3487
- HDU 3487 Splay
- hdu 4680(splay tree)
- hdu 4699 Editor(Splay)
- hdu-4453-Looploop-splay
- 【HDU】4453 Looploop 【splay】
- Hdu 4699 Editor(Splay)
- HDU 1890(Splay树)
- 解决js eval() 缺少';'
- SSH学习笔记1
- 防止SQL注入和XSS攻击Filter
- 基于百度推送的第三方服务器android即时聊天系统
- Apache和Tomcat的区别与联系
- hdu-2475-Box-splay
- 初探排序学习笔记
- linux命令su与su-的区别
- 财经作品推荐
- 使用NameCtrl为网页集成Office Communicator状态信息
- 根据SQLServer数据表生成C#实体类
- dbms_spm之baseline FIXED=YES使用陷阱(执行计划不通用),严重影响系统;
- C#基础知识
- Http协议学习总结