ACM常用模板——数据结构——treap树

hdu 4585 新和尚和老和尚比武
   
   
using namespace std;
struct Node
{
Node* son[2];
int rank;
int size;
int key;
bool operator<(const Node &a)const
{
return rank<a.rank; //重载小于好,用于维护堆的性质
}
int cmp(int x)const
{
if(x==key) return -1; //比较函数,用于确定寻找的值的位置,0表示左儿子,1表示右儿子,-1代表当前节点
return x<key?0:1;
}
void maintain()//这里仅仅需要维护size
{
size = 1 + son[0]->size + son[1]->size;
}
};
struct Treap
{
Node * root;
Node *null = new Node();//定义空结点,可以有效避免NULL带来的问题
//初始化Treap
void initial()
{
srand(time(NULL));
root = null;
}
//旋转操作,d=0时代表左旋,d=1时代表右旋
void rotate(Node* &o,int d)
{
Node* k = o->son[d^1];
o->son[d^1] = k -> son[d];
k->son[d] = o;
o->maintain();//注意,必须先维护o再维护k,因为o是子节点,不优先维护,会导致父节点出错
k->maintain();
o = k;
}
void ins(Node* &o,int x)
{
if(o==null)
{
o = new Node();
o->son[0] = o->son[1] = null;
o->rank = rand();
o->key = x;
o->size = 1;
}
else
{
int d = o->cmp(x);
ins(o->son[d],x);
o->maintain();
if(o < o->son[d])
rotate(o,d^1);//当位于左儿子时,需要进行的是右旋,当位于右儿子时应当进行左旋,因此通项为rotate(o,d^1)
}
}
void del(Node* &o,int x)
{
int d = o->cmp(x);
if(d==-1)//已经找到待删除节点,将会有两种情况
{
if(o->son[0] == null) o = o->son[1];//左儿子为空,直接连接父节点和右儿子
else if(o->son[1] == null) o= o->son[0];//右儿子为空,同上
else
{
d = o->son[0] < o->son[1] ? 0:1;//在子节点中找到一个rank大的节点,将其旋转到当前节点,那么当前节点应当位于相反的子节点
rotate(o,d);
del(o->son[d],x);
}
}
else
del(o->son[d],x);
if(o!=null) o->maintain();//删除节点后,需要对size进行维护。需要注意的是,如果节点为空时,就不需要维护了
}
int kth(Node* o,int k)//寻找第k大数
{
if(o == null || k<=0 || k>o->size) return -1;//k过大或者过小都不能找到,Treap中没有节点也无需找
int s = (o->son[1] == null?0:o->son[1]->size);//获得右儿子的size
if(k == s+1) return o->key;//当k为 s + 1时,说明当前节点就是答案
else
if(k<=s) return kth(o->son[1],k);//当k<s时,说明第k大数在右子树,k值无需更改
else
return kth(o->son[0],k-s-1);//当k>s+1时,第k大数在左子树,需要减去右子树的值和节点本身
}
int find(Node* o,int k)//返回该数是第几大
{
if(o == null) return -1;
int d = o->cmp(k);
if(d == -1) return 1 + o->son[1]->size;
else if(d == 1) return find(o->son[d],k);
else
{
int tmp = find(o->son[d],k);
if(tmp == -1) return -1;
else
return tmp + 1 + o->son[1]->size;
}
}
} treap;
int id[5000005];
int main()
{
int n;
while(scanf("%d",&n)&&n)
{
treap.initial();//树堆初始化
int x,y;
scanf("%d%d",&x,&y);
treap.ins(treap.root,y);
id[y] = x;
printf("%d %d\n",x,1);
for(int i =2;i<=n;i++)
{
scanf("%d%d",&x,&y);
id[y] = x;
treap.ins(treap.root,y);
int t = treap.find(treap.root,y);
int ans1,ans2,ans;
ans1 = treap.kth(treap.root,t-1);//寻找第K大数的前驱
ans2 = treap.kth(treap.root,t+1);//寻找第K大数的后继
if(ans1 != -1)
{
if(ans2==-1) ans = ans1;
else
if(ans1 - y >= y-ans2) ans = ans2; else ans = ans1;
}
else
ans =ans2;
printf("%d %d\n",x,id[ans]);
}
}
return 0;
}
map解法:
   
   
#include <stdio.h>
#include <string.h>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
int i,n,id,f;
while(~scanf("%d",&n),n)
{
map<int,int> a;
a.clear();
a[1000000000] = 1;
for(i = 0; i<n; i++)
{
scanf("%d%d",&id,&f);
printf("%d ",id);
map<int,int>::iterator it = a.lower_bound(f);
if(it == a.end())
{
printf("%d\n",it->second);
}
else
{
int t1 = it->first;
int t2 = it->second;
if(it!=a.begin())
{
it--;
if(f-it->first<=t1-f)
printf("%d\n",it->second);
else
printf("%d\n",t2);
}
else
printf("%d\n",t2);
}
a[f] = id;
}
}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值