poj 3481 Double Queue(平衡树,SBT)
2014-10-11 14:05
387 查看
poj 3481 Double Queue
平衡树模板题,练手
#include<stdio.h>
#include<string.h>
#define MAXN 100005
struct node
{
int l,r;
int key,id;
int size;
}t[MAXN];
int top,root;
void lRotate(int &p)
{
int rch=t[p].r;
t[p].r=t[rch].l;
t[rch].l=p;
t[rch].size=t[p].size;
t[p].size=t[t[p].l].size+t[t[p].r].size+1;
p=rch;
}
void rRotate(int &p)
{
int lch=t[p].l;
t[p].l=t[lch].r;
t[lch].r=p;
t[lch].size=t[p].size;
t[p].size=t[t[p].l].size+t[t[p].r].size+1;
p=lch;
}
void maintain(int &p,bool flag)
{
if(!flag)
{
if(t[t[t[p].l].l].size>t[t[p].r].size)//LL
rRotate(p);
else if(t[t[t[p].l].r].size>t[t[p].r].size)//LR
lRotate(t[p].l),
rRotate(p);
else return ;
}
else
{
if(t[t[t[p].r].r].size>t[t[p].l].size)
lRotate(p);
else if(t[t[t[p].r].l].size>t[t[p].l].size)
rRotate(t[p].r),
lRotate(p);
else return ;
}
maintain(t[p].l, false);
maintain(t[p].r, true);
maintain(p, false);
maintain(p, true);
}
void insert(int &p,int key,int id)
{
if(p==0)
{
p=++top;
t[p].l=t[p].r=0;
t[p].size=1;
t[p].key=key,t[p].id=id;
}
else
{
t[p].size++;
if(key<t[p].key) insert(t[p].l,key,id);
else insert(t[p].r,key,id);
maintain(p, key>=t[p].key);
}
}
int remove(int &p,int key)
{
t[p].size--;
if(t[p].key==key||
(key<t[p].key&&t[p].l==0)||
(key>t[p].key&&t[p].r==0))
{
int retkey=t[p].key;
if(t[p].l&&t[p].r)
t[p].key=remove(t[p].l,t[p].key+1);
else
p=t[p].l+t[p].r;
return retkey;
}
else if(key<t[p].key) return remove(t[p].l,key);
else return remove(t[p].r,key);
}
int getmin()
{
int p=root;
while(t[p].l) p=t[p].l;
printf("%d\n",t[p].id);
return t[p].key;
}
int getmax()
{
int p=root;
while(t[p].r) p=t[p].r;
printf("%d\n",t[p].id);
return t[p].key;
}
int main()
{
top=root=0;
int op,id,key;
while(scanf("%d",&op)!=EOF&&op)
{
if(op==1)
{
scanf("%d%d",&id,&key);
insert(root,key,id);
}
else if(op==2)
{
int temp=getmax();
remove(root,temp);
}
else
{
int temp=getmin();
remove(root,temp);
}
}
return 0;
}
平衡树模板题,练手
#include<stdio.h>
#include<string.h>
#define MAXN 100005
struct node
{
int l,r;
int key,id;
int size;
}t[MAXN];
int top,root;
void lRotate(int &p)
{
int rch=t[p].r;
t[p].r=t[rch].l;
t[rch].l=p;
t[rch].size=t[p].size;
t[p].size=t[t[p].l].size+t[t[p].r].size+1;
p=rch;
}
void rRotate(int &p)
{
int lch=t[p].l;
t[p].l=t[lch].r;
t[lch].r=p;
t[lch].size=t[p].size;
t[p].size=t[t[p].l].size+t[t[p].r].size+1;
p=lch;
}
void maintain(int &p,bool flag)
{
if(!flag)
{
if(t[t[t[p].l].l].size>t[t[p].r].size)//LL
rRotate(p);
else if(t[t[t[p].l].r].size>t[t[p].r].size)//LR
lRotate(t[p].l),
rRotate(p);
else return ;
}
else
{
if(t[t[t[p].r].r].size>t[t[p].l].size)
lRotate(p);
else if(t[t[t[p].r].l].size>t[t[p].l].size)
rRotate(t[p].r),
lRotate(p);
else return ;
}
maintain(t[p].l, false);
maintain(t[p].r, true);
maintain(p, false);
maintain(p, true);
}
void insert(int &p,int key,int id)
{
if(p==0)
{
p=++top;
t[p].l=t[p].r=0;
t[p].size=1;
t[p].key=key,t[p].id=id;
}
else
{
t[p].size++;
if(key<t[p].key) insert(t[p].l,key,id);
else insert(t[p].r,key,id);
maintain(p, key>=t[p].key);
}
}
int remove(int &p,int key)
{
t[p].size--;
if(t[p].key==key||
(key<t[p].key&&t[p].l==0)||
(key>t[p].key&&t[p].r==0))
{
int retkey=t[p].key;
if(t[p].l&&t[p].r)
t[p].key=remove(t[p].l,t[p].key+1);
else
p=t[p].l+t[p].r;
return retkey;
}
else if(key<t[p].key) return remove(t[p].l,key);
else return remove(t[p].r,key);
}
int getmin()
{
int p=root;
while(t[p].l) p=t[p].l;
printf("%d\n",t[p].id);
return t[p].key;
}
int getmax()
{
int p=root;
while(t[p].r) p=t[p].r;
printf("%d\n",t[p].id);
return t[p].key;
}
int main()
{
top=root=0;
int op,id,key;
while(scanf("%d",&op)!=EOF&&op)
{
if(op==1)
{
scanf("%d%d",&id,&key);
insert(root,key,id);
}
else if(op==2)
{
int temp=getmax();
remove(root,temp);
}
else
{
int temp=getmin();
remove(root,temp);
}
}
return 0;
}
相关文章推荐
- poj 3481 Double Queue(SBT解法)
- POJ 3481 Double Queue SBT || treap
- 【SBT】 POJ 3481 Double Queue
- POJ 3481 Double Queue【SBT】
- poj 3481 Double Queue (SBT)
- POJ-3481 Double Queue (平衡树 入门题 splay模板题)
- [BST-SBT]POJ_3481_double queue
- POJ 3481 Double Queue
- POJ 3481 Double Queue
- poj 3481 Double Queue
- POJ 3481 multimap / 平衡树 / splay
- poj 3481 sbt 版本
- POJ 3481 Double Queue
- POJ 3481 Double Queue
- POJ 3481 & HDU 1908 Double Queue (map运用)
- 跳跃表基础——POJ 3481 Double Queue
- POJ--3481 Double Queue
- poj-3481-Double Queue-splay树的水题
- POJ 3481 Double Queue
- poj 3481 Double Queue