POJ 3481 Double Queue [splay]
来源:互联网 发布:linux服务器端口开放 编辑:程序博客网 时间:2024/05/22 04:47
题意:有三种操作:1.将权值为p名称为k的顾客加入队列 2.从队列中取出权值较高的顾客,并输出 3.从队列中取出权值较低的顾客并输出
题解:用splay维护权值,再询问
AC代码:
#include<stdio.h>#include<string.h>#include<map>using namespace std;#define MAXN 1000005 //节点数 int ch[MAXN][2],f[MAXN],cnt[MAXN],key[MAXN],size[MAXN];int sz=0,root=0; int newnode(int fa,int v){sz++;ch[sz][0]=ch[sz][1]=0;f[sz]=fa;key[sz]=v;size[sz]=cnt[sz]=1;return sz;}void clear(int x){ch[x][0]=ch[x][1]=f[x]=cnt[x]=key[x]=size[x]=0;}int get(int x)//得到x是其父亲的左儿子还是右儿子 { return ch[f[x]][1]==x;}void up(int x){ if(x) { size[x]=cnt[x]; if(ch[x][0])size[x]+=size[ch[x][0]]; if(ch[x][1])size[x]+=size[ch[x][1]]; }}void rotate(int x){int old=f[x],oldf=f[old],which=get(x);ch[old][which]=ch[x][which^1];f[ch[old][which]]=old;f[old]=x;ch[x][which^1]=old;f[x]=oldf;if(oldf)ch[oldf][ch[oldf][1]==old]=x;up(old);up(x);}void splay(int x,int goal)//伸展{ for(int fa;(fa=f[x]);rotate(x)) if(f[fa]!=goal)rotate((get(x)==get(fa)?fa:x)); if(goal==0)root=x;}void insert(int v){if(root==0){root=newnode(0,v);return;}int now=root,fa=0;while(1){if (key[now]==v) { cnt[now]++;up(now);up(fa);splay(now,0);break; }fa=now;now=ch[now][key[now]<v];if(now==0){int newroot=newnode(fa,v);ch[fa][key[fa]<v]=newroot;up(fa);splay(sz,0);break;}}}int find(int v)//查询v的排名{ int ans=0,now=root; while(1) {if(v<key[now])now=ch[now][0];else{ans+=(ch[now][0]?size[ch[now][0]]:0);if(v==key[now]){splay(now,0);return ans+1;}ans+=cnt[now];now=ch[now][1];}}}int findx(int x)//查询第x小权值是多少 { int now=root;while(1){if(ch[now][0]&&x<=size[ch[now][0]])now=ch[now][0];else{int temp=(ch[now][0]?size[ch[now][0]]:0)+cnt[now];if(x<=temp)return key[now]; x-=temp;now=ch[now][1];} }}int pre(){int now=ch[root][0];while(ch[now][1])now=ch[now][1];return now;}int next(){int now=ch[root][1];while(ch[now][0])now=ch[now][0];return now;}void del(int x)//删除某权值的点 {int whatever=find(x);if (cnt[root]>1) {cnt[root]--;return;} //自由一个点if (!ch[root][0]&&!ch[root][1]) {clear(root);root=0;return;}//只有一个儿子if (!ch[root][0]){ int oldroot=root;root=ch[root][1];f[root]=0;clear(oldroot);return;} else if (!ch[root][1]){int oldroot=root;root=ch[root][0];f[root]=0;clear(oldroot);return; } //两个儿子 int leftbig=pre(),oldroot=root; splay(leftbig,0); f[ch[oldroot][1]]=root; ch[root][1]=ch[oldroot][1]; clear(oldroot); up(root); return;}map<int,int>mp;int main(){int op;while(~scanf("%d",&op)){if(op==0)break;if(op==1){int k,v;scanf("%d%d",&k,&v);insert(v);mp[v]=k;}if(op==2){if(size[root]==0){printf("0\n");continue;}int v=findx(size[root]);printf("%d\n",mp[v]);del(v); }if(op==3){if(size[root]==0){printf("0\n");continue;}int v=findx(1);printf("%d\n",mp[v]);del(v);}}}
splay模板:
#define MAXN 100005 //节点数 int ch[MAXN][2],f[MAXN],cnt[MAXN],key[MAXN],size[MAXN];int sz=0,root=0; int newnode(int fa,int v){sz++;ch[sz][0]=ch[sz][1]=0;f[sz]=fa;key[sz]=v;size[sz]=cnt[sz]=1;return sz;}void clear(int x){ch[x][0]=ch[x][1]=f[x]=cnt[x]=key[x]=size[x]=0;}int get(int x)//得到x是其父亲的左儿子还是右儿子 { return ch[f[x]][1]==x;}void up(int x){ if(x) { size[x]=cnt[x]; if(ch[x][0])size[x]+=size[ch[x][0]]; if(ch[x][1])size[x]+=size[ch[x][1]]; }}void rotate(int x){int old=f[x],oldf=f[old],which=get(x);ch[old][which]=ch[x][which^1];f[ch[old][which]]=old;f[old]=x;ch[x][which^1]=old;f[x]=oldf;if(oldf)ch[oldf][ch[oldf][1]==old]=x;up(old);up(x);}void splay(int x,int goal)//伸展{ for(int fa;(fa=f[x]);rotate(x)) if(f[fa]!=goal)rotate((get(x)==get(fa)?fa:x)); if(goal==0)root=x;}void insert(int v){if(root==0){root=newnode(0,v);return;}int now=root,fa=0;while(1){if (key[now]==v) { cnt[now]++;up(now);up(fa);splay(now,0);break; }fa=now;now=ch[now][key[now]<v];if(now==0){int newroot=newnode(fa,v);ch[fa][key[fa]<v]=newroot;up(fa);splay(sz,0);break;}}}int find(int v)//查询v的排名{ int ans=0,now=root; while(1) {if(v<key[now])now=ch[now][0];else{ans+=(ch[now][0]?size[ch[now][0]]:0);if(v==key[now]){splay(now,0);return ans+1;}ans+=cnt[now];now=ch[now][1];}}}int findx(int x)//查询第x小权值是多少 { int now=root;while(1){if(ch[now][0]&&x<=size[ch[now][0]])now=ch[now][0];else{int temp=(ch[now][0]?size[ch[now][0]]:0)+cnt[now];if(x<=temp)return key[now]; x-=temp;now=ch[now][1];} }}int pre(){int now=ch[root][0];while(ch[now][1])now=ch[now][1];return now;}int next(){int now=ch[root][1];while(ch[now][0])now=ch[now][0];return now;}void del(int x)//删除某权值的点 {int whatever=find(x);if (cnt[root]>1) {cnt[root]--;return;} //自由一个点if (!ch[root][0]&&!ch[root][1]) {clear(root);root=0;return;}//只有一个儿子if (!ch[root][0]){ int oldroot=root;root=ch[root][1];f[root]=0;clear(oldroot);return;} else if (!ch[root][1]){int oldroot=root;root=ch[root][0];f[root]=0;clear(oldroot);return; } //两个儿子 int leftbig=pre(),oldroot=root; splay(leftbig,0); f[ch[oldroot][1]]=root; ch[root][1]=ch[oldroot][1]; clear(oldroot); up(root); return;}
阅读全文
0 0
- poj 3481 Double Queue splay
- POJ 3481 Double Queue [splay]
- POJ 3481 Double Queue(Splay)
- POJ 3481 Double Queue (Splay || 水题)
- poj-3481-Double Queue-splay树的水题
- POJ 3481 Double Queue 伸展树splay + 删除节点
- poj 3481 double Queue(Splay树 模板解读)
- poj 3481 Double Queue
- Double Queue POJ 3481
- POJ 3481 Double Queue
- POJ 3481 Double Queue
- POJ 3481 Double Queue
- POJ 3481 Double Queue
- POJ 3481 Double Queue
- poj 3481 Double Queue
- poj 3481 Double Queue
- POJ - 3481 Double Queue
- POJ 3481 Double Queue
- 02-JS特效-匀速动画
- WAS中通过集群的方式打开项目,报500的错误
- Android KK上对ALC5616调试笔记——音量调节
- fgm实例练习笔记-3.9倒计时
- 尾数前移问题
- POJ 3481 Double Queue [splay]
- 矩阵乘法
- 接口测试软件简要介绍
- java 对象锁
- 机器学习:线性回归和逻辑回归的区别
- DevExpress之WaitDialogForm
- 中文版iTween帮助文档、参数解析
- 获取百度地图
- centos 下 sphinx(coreseek)安装及常见问题