hdu 3436 Queue-jumpers
来源:互联网 发布:mac 文件权限 去掉加号 编辑:程序博客网 时间:2024/04/30 04:52
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3436
题目思路:splay,也可以用线段树或树状数组,主要难点在离散化,这个离散化是把点和点这间的区间都离散化,离散化后的点要保存原来区间的长度和起始点等信息,这样就可用splay模拟了,top操作可以先删点再插入,也可以将要删的点伸展到根结点,然后将它的左子树接到它后继的左边,这样它便成为整棵树最左边的结点了。由于人的编号和结点的index是一一对应的,不会改变,所以可以把这个关系存起来,这样访问的时候就可以直接通过人的编号找到结点了。这个题我tle了很久,结果才发现最前面的那段区间没存,真是悲剧啊!
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<string>#include<queue>#include<algorithm>#include<vector>#include<stack>#include<list>#include<iostream>#include<map>using namespace std;#define inf 0x3f3f3f3f#define M 300000int max(int a,int b){return a>b?a:b;}int min(int a,int b){return a<b?a:b;}char op[M][10];int d[M],pos[M],nodenum[M],p[M],ch[M][2],s[M],v[M],begin[M],end[M],num[M];int root,top1,N;int change;struct node{ int d,id; bool operator<(const node a)const { return d<a.d; }}li[M];void vist(int x){ if(!x) return; printf("x %d p %d ch0 %d ch1 %d s %d v %d\n",x,p[x],ch[x][0],ch[x][1],s[x],v[x]); vist(ch[x][0]); vist(ch[x][1]);}void debug(){ puts("debug"); vist(root);}void up(int x){ int l=ch[x][0],r=ch[x][1]; s[x]=s[l]+s[r]+num[x];}void newnode(int &x,int val,int pre){ x=++top1; ch[x][0]=ch[x][1]=0; p[x]=pre; v[x]=val;}void build(int &x,int l,int r,int pre){ if(l>r) return; int mid=(l+r)>>1; newnode(x,mid,pre); nodenum[mid]=x; num[x]=end[mid]-begin[mid]+1; s[x]=end[mid]-begin[mid]+1; build(ch[x][0],l,mid-1,x); build(ch[x][1],mid+1,r,x); up(x);}void init(){ top1=0; p[0]=ch[0][0]=ch[0][1]=s[0]=num[0]=v[0]=0; newnode(root,inf,0); s[root]=1; num[root]=1; build(ch[root][0],1,N,root); up(root);}void rot(int x,int f){ int y=p[x]; p[ch[x][f]]=y; ch[y][!f]=ch[x][f]; p[x]=p[y]; if(p[y]) ch[p[y]][ch[p[y]][1]==y]=x; p[y]=x; ch[x][f]=y; up(y);}void splay(int x,int goal){ while(p[x]!=goal) { if(p[p[x]]==goal) rot(x,ch[p[x]][0]==x); else { int y=p[x],f=ch[p[y]][0]==y; if(ch[y][f]==x) { rot(x,!f); } else rot(y,f); rot(x,f); } } up(x); if(!goal) root=x;}void top(int val){ int x=nodenum[pos[val]]; splay(x,0); int t1=ch[root][1]; while(ch[t1][0]) t1=ch[t1][0]; splay(t1,root); p[ch[root][0]]=t1; ch[t1][0]=ch[root][0]; ch[root][0]=0; up(t1); up(root);}void query(int val){ int x=nodenum[pos[val]]; splay(x,0); printf("%d\n",s[ch[root][0]]+num[x]);}void rank(int id){ int x=root; while(1) { if(s[ch[x][0]]>=id) x=ch[x][0]; else { if(s[ch[x][0]]+num[x]>=id) break; id-=s[ch[x][0]]+num[x]; x=ch[x][1]; } } int tmp=id-(s[ch[x][0]]); printf("%d\n",begin[v[x]]+tmp-1); splay(x,0);}int main(){ int t,n,q,i,count=1; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&q); int cnt=0; int ma=0; for(i=0;i<q;i++) { scanf("%s%d",op[i],&d[i]); if(op[i][0]=='T'||op[i][0]=='Q') { li[cnt].d=d[i]; li[cnt++].id=i; if(op[i][0]=='T') ma=max(ma,d[i]); } } sort(li,li+cnt); N=0; if(li[0].d>1) { ++N; begin[N]=1; end[N]=li[0].d-1; } ++N; begin[N]=end[N]=li[0].d; pos[li[0].id]=N; for(i=1;i<cnt&&li[i].d<=ma;i++) { if(li[i].d==li[i-1].d) { pos[li[i].id]=N; continue; } if(li[i].d-li[i-1].d>1) { ++N; begin[N]=li[i-1].d+1; end[N]=li[i].d-1; } ++N; begin[N]=end[N]=li[i].d; pos[li[i].id]=N; } init(); printf("Case %d:\n",count++); for(i=0;i<q;i++) { if(op[i][0]=='T') top(i); else if(op[i][0]=='Q') { if(d[i]>ma) { printf("%d\n",d[i]); continue; } query(i); } else { if(d[i]>ma) { printf("%d\n",d[i]); continue; } rank(d[i]); } } }}
- hdu 3436 Queue-jumpers
- HDU 3436 Queue-jumpers
- HDU 3436 Queue-jumpers
- HDU 3436 Queue-jumpers
- HDU 3436 Queue-jumpers
- hdu 3436 Queue-jumpers(Splay)
- HDU 3436 Queue-jumpers (Splaytree)
- HDU 3436 Queue-jumpers(Splay)
- HDU 3436 Queue-jumpers splay
- HDU 3436 Queue-jumpers (Splay tree)
- HDU 3436 Queue-jumpers(splay tree)
- hdu 3436 Queue-jumpers(splay tree)
- hdu 3436 Queue-jumpers 树状数组
- HDU 3436 Queue-jumpers(SplayTree+离散化)
- hdu-3436-Queue-jumpers-伸展树
- 【HDU 3436】Queue-jumpers(Splay)
- hdu-3436 Queue-jumpers(伸展树)
- HDU 3436 Queue-jumpers Splay+离散化
- 我在写烂文章
- 推荐几本对创业者很有用的书籍
- 第16章 贪心算法
- 清十二帝疑案(阎崇年)37集全集 百家讲坛 DVD高清晰版
- Extjs学习 事件处理
- hdu 3436 Queue-jumpers
- 基础总结篇之五:BroadcastReceiver应用详解
- 原语组装和光栅化
- 一些原子操作函数
- Extjs学习 表单验证validate
- java 的反射学习
- 漂亮的jQuery页面向导插件 - Pageguide.js
- 升级 VMM 2008 到 2012
- C++中const在不同对象不同位置中的作用