您的位置:首页 > 其它

P3369 【模板】普通平衡树 Treap

2017-12-09 19:41 369 查看

 P3369 【模板】普通平衡树(Treap/SBT)

题目描述

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:

  1. 插入x数

  2. 删除x数(若有多个相同的数,因只删除一个)

  3. 查询x数的排名(排名定义为比当前数小的数的个数+1。若有多个相同的数,因输出最小的排名)

  4. 查询排名为x的数

  5. 求x的前驱(前驱定义为小于x,且最大的数)

  6. 求x的后继(后继定义为大于x,且最小的数)

输入输出格式

输入格式:

 

第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号( 1 \leq opt \leq 61≤opt≤6 )

 

输出格式:

 

对于操作3,4,5,6每行输出一个数,表示对应答案

 

输入输出样例

输入样例#1: 复制
10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
输出样例#1: 复制
106465
84185
492737

说明

时空限制:1000ms,128M

1.n的数据范围: n \leq 100000n≤100000

2.每个数的数据范围: [-{10}^7, {10}^7][−107,107]

来源:Tyvj1728 原名:普通平衡树

在此鸣谢

 

code

treap 真是个好东西。。

 

#include<cstdio>
#include<algorithm>
#include<ctime>
using namespace std;

const int N = 200010;

struct Data{
int l,r,val,key,siz,cnt;
}t
;
int Root,tn,ans;

inline char nc() {
static char buf[100000],*p1 = buf,*p2 = buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2) ? EOF :*p1++;
}
inline int read() {
int x = 0,f = 1;char ch=nc();
for (; ch<'0'||ch>'9'; ch = nc())
if (ch == '-') f = -1;
for (; ch>='0'&&ch<='9'; ch = nc())
x = x*10+ch-'0';
return x * f;
}

inline void pushup(int x) {
t[x].siz = t[t[x].l].siz + t[t[x].r].siz + t[x].cnt;
}
inline void leftturn(int &k) {
int a = t[k].r;
t[k].r = t[a].l;
t[a].l = k;
t[a].siz = t[k].siz;
pushup(k);
k = a;
}
inline void rightturn(int &k) {
int a = t[k].l;
t[k].l = t[a].r;
t[a].r = k;
t[a].siz = t[k].siz;
pushup(k);
k = a;
}
void Insert(int &k,int x) {
if (k==0) {
tn++;k = tn;
t[k].siz = t[k].cnt = 1;
t[k].val = x;t[k].key = rand();
return ;
}
t[k].siz++;
if (t[k].val==x) t[k].cnt ++;
else if (x > t[k].val) {
Insert(t[k].r,x);
if (t[t[k].r].key < t[k].key) leftturn(k);
}
else {
Insert(t[k].l,x);
if (t[t[k].l].key < t[k].key) rightturn(k);
}
}
void Delete(int &k,int x) {
if (k==0) return ;
if (t[k].val==x) {
if (t[k].cnt > 1) {
t[k].cnt--;t[k].siz--;return ;
}
if (t[k].l * t[k].r == 0) k = t[k].l + t[k].r;
else if (t[t[k].l].key < t[t[k].r].key) {
rightturn(k);Delete(k,x);
}
else {
leftturn(k);Delete(k,x);
}
}
else if (x > t[k].val) {
t[k].siz--;Delete(t[k].r,x);
}
else {
t[k].siz--;Delete(t[k].l,x);
}
}
int getk(int k,int x) {
if (k==0) return 0;
if (t[k].val==x) return t[t[k].l].siz + 1;
else if (x > t[k].val)
return t[t[k].l].siz + t[k].cnt + getk(t[k].r,x);
else return getk(t[k].l,x);
}
int getkth(int k,int x) {
if (k==0) return 0;
if (x <= t[t[k].l].siz) return getkth(t[k].l,x);
else if (x > t[t[k].l].siz + t[k].cnt)
return getkth(t[k].r,x-t[t[k].l].siz-t[k].cnt);
else return t[k].val;
}
void getpre(int k,int x) {
if (k==0) return ;
if (t[k].val < x) ans = k,getpre(t[k].r,x);
else getpre(t[k].l,x);
}
void getsuc(int k,int x) {
if (k==0) return ;
if (t[k].val > x) ans = k,getsuc(t[k].l,x);
else getsuc(t[k].r,x);
}

int main() {
int n = read();
while (n--){
int opt = read(),x = read();
if (opt==1) Insert(Root,x);
else if (opt==2) Delete(Root,x);
else if (opt==3) printf("%d\n",getk(Root,x));
else if (opt==4) printf("%d\n",getkth(Root,x));
else if (opt==5) ans = 0,getpre(Root,x),printf("%d\n",t[ans].val);
else ans = 0,getsuc(Root,x),printf("%d\n",t[ans].val);
}
return 0;
}

 

更新后的treap

#include<cstdio>
#include<algorithm>
#include<ctime>

using namespace std;

#define lson t[k].l
#define rson t[k].r
const int N = 200010;

struct Data{
int l,r,val,key,siz,cnt;
}t
;
int Root,tn,ans;

inline char nc() {
static char buf[100000],*p1 = buf,*p2 = buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2) ? EOF :*p1++;
}
inline int read() {
int x = 0,f = 1;char ch=nc();
for (; ch<'0'||ch>'9'; ch = nc())
if (ch == '-') f = -1;
for (; ch>='0'&&ch<='9'; ch = nc())
x = x*10+ch-'0';
return x * f;
}

inline void pushup(int k) {
t[k].siz = t[lson].siz + t[rson].siz + t[k].cnt;
}
inline void leftturn(int &k) {
int a = rson;
rson = t[a].l;
t[a].l = k;
t[a].siz = t[k].siz;
pushup(k);
k = a;
}
inline void rightturn(int &k) {
int a = lson;
lson = t[a].r;
t[a].r = k;
t[a].siz = t[k].siz;
pushup(k);
k = a;
}
void Insert(int &k,int x) {
if (k==0) {
tn++;k = tn;
t[k].siz = t[k].cnt = 1;
t[k].val = x;t[k].key = rand();
return ;
}
t[k].siz++;
if (t[k].val==x) t[k].cnt ++;
else if (x > t[k].val) {
Insert(rson,x);
if (t[rson].key < t[k].key) leftturn(k);
}
else {
Insert(lson,x);
if (t[lson].key < t[k].key) rightturn(k);
}
}
void Delete(int &k,int x) {
if (k==0) return ;
if (t[k].val==x) {
if (t[k].cnt > 1) {
t[k].cnt--;t[k].siz--;return ;
}
if (lson * rson == 0) k = lson + rson;
else if (t[lson].key < t[rson].key) {
rightturn(k);Delete(k,x);
}
else {
leftturn(k);Delete(k,x);
}
}
else if (x > t[k].val) {
t[k].siz--;Delete(rson,x);
}
else {
t[k].siz--;Delete(lson,x);
}
}
int getk(int k,int x) {
if (k==0) return 0;
if (t[k].val==x) return t[lson].siz + 1;
else if (x > t[k].val)
return t[lson].siz + t[k].cnt + getk(rson,x);
else return getk(lson,x);
}
int getkth(int k,int x) {
if (k==0) return 0;
if (x <= t[lson].siz) return getkth(lson,x);
else if (x > t[lson].siz + t[k].cnt)
return getkth(rson,x-t[lson].siz-t[k].cnt);
else return t[k].val;
}
void getpre(int k,int x) {
if (k==0) return ;
if (t[k].val < x) ans = k,getpre(rson,x);
else getpre(lson,x);
}
void getsuc(int k,int x) {
if (k==0) return ;
if (t[k].val > x) ans = k,getsuc(lson,x);
else getsuc(rson,x);
}

int main() {
int n = read();
while (n--){
int opt = read(),x = read();
if (opt==1) Insert(Root,x);
else if (opt==2) Delete(Root,x);
else if (opt==3) printf("%d\n",getk(Root,x));
else if (opt==4) printf("%d\n",getkth(Root,x));
else if (opt==5) ans = 0,getpre(Root,x),printf("%d\n",t[ans].val);
else ans = 0,getsuc(Root,x),printf("%d\n",t[ans].val);
}
return 0;
}
View Code

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: