poj1442 treap版本&&priority_queue

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include <cstdlib>

using namespace std;

int n,i,m,h[30009];

struct wbysr
{
	wbysr *l,*r;
	int dui;
	int num;
	int size;
	wbysr (int x)
	  {
	  	l=NULL;
	  	r=NULL;
	  	dui=rand()%10000+4;
	  	num=x;
	  }
}*root;

int size_l(wbysr *t)
{
	if(t->l)
	  return t->l->size;
	else
	 return 0;
}

int size_r(wbysr *t)
{
	if(t->r)
	  return t->r->size;
	else
	 return 0;
}

void rotate_left(wbysr *&t)//左旋,右孩子变成根,根变成左孩子  
{
	wbysr *p=t->r;
	t->r=p->l;
	p->l=t;
	p->size=t->size;
	t->size=size_l(t)+size_r(t)+1;
	t=p;
}

void rotate_right(wbysr *&t)
{
	wbysr *p=t->l;
	t->l = p->r;
    p->r = t;
    p->size = t->size;
    t->size=size_l(t)+size_r(t)+1;
    t=p;
}

int ask(int d,wbysr *t)
{
	int p=size_l(t);
	if(d==p+1)
	  return t->num;
	else
	if(d<=p)
	  return ask(d,t->l);
	else
	  return ask(d-1-p,t->r);
}

void insert(wbysr *&t,int d)
{
	if(!t)
	  {
	  	t=new wbysr(d);
	  	t->size=1;
	  }
	else
	if(d<=t->num)
	 {
	 	t->size++;
	 	insert(t->l,d);
	 	if(t->l->dui < t->dui)
	 	  rotate_right(t);
	 }
	else
	//if(d>t->num)
	  {
	  	t->size++;
	  	insert(t->r,d);	  	  
	  	if(t->r->dui < t->dui)
	  	  rotate_left(t);
	  }
}

int main()
{
	cin>>n>>m;
	int now=1;
	for(i=1;i<=n;i++)
	   scanf("%d",&h[i]);
	for(i=1;i<=m;i++)
	 {
	 	int x;
	 	scanf("%d",&x);
	 	while(now<=x)
	 	 {
	 	 	insert(root,h[now]);
	 	 	now++;
	 	 }
	 	cout<<ask(i,root)<<endl;
	 }
}

很简单的题目。之前就用priority_queue水过,现在由于要学treap又没有很水的题入门所以就他了。。

但是写完真的觉得treap不算很难啊,按的标程学的,基本没有什么障碍;从明天开始多做一些treap的题

最后能写下fhq的函数版treap

下面是代码


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值