3224: Tyvj 1728 普通平衡树

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 10097  Solved: 4302
[Submit][Status][Discuss]

Description

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)

Input

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

Output

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

Sample Input

10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598

Sample Output

106465
84185
492737

HINT

1.n的数据范围:n<=100000
2.每个数的数据范围:[-1e7,1e7]
数据如下http://pan.baidu.com/s/1jHMJwO2

一万年没写数据结构了,来发treap模板练练手/
 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 110010
#define llg int
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
int n,m,ans; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} struct MYTREAP
{
int root,size;
struct
{
int l,r,rnd,val,w,size;
}po[maxn]; void init(){ memset(po,,sizeof(po)); root=size=;} void update(llg now) {po[now].size=po[po[now].l].size+po[po[now].r].size+po[now].w;} void right_(llg &now) {llg t=po[now].l; po[now].l=po[t].r; po[t].r=now; po[t].size=po[now].size; update(now); now=t;}
void left_(llg &now) {llg t=po[now].r; po[now].r=po[t].l; po[t].l=now; po[t].size=po[now].size; update(now); now=t;} void in(llg val)
{
insert(root,val);
} void insert(llg &now,llg val)
{
if (now==)
{
now=++size;
po[now].size=po[now].w=; po[now].val=val; po[now].rnd=rand();
return ;
}
po[now].size++;
if (po[now].val==val)
{
po[now].w++;
}
else
if (po[now].val<val)
{
insert(po[now].r,val);
if (po[po[now].r].rnd<po[now].rnd) left_(now);
}
else
{
insert(po[now].l,val);
if (po[po[now].l].rnd<po[now].rnd) right_(now);
}
} void del(llg &now,llg val)
{
if (now==) return ;
if (po[now].val==val)
{
if (po[now].w>)
{
po[now].w--; po[now].size--;
return ;
}
if (po[now].l*po[now].r==) now=po[now].l+po[now].r;
else
{
if (po[po[now].l].rnd<po[po[now].r].rnd) right_(now); else left_(now);
del(now,val);
}
}
else
{
po[now].size--;
if (val>po[now].val) del(po[now].r,val);
else del(po[now].l,val);
}
} int ask_rank(llg now,llg val)
{
if (now==) return ;
if (po[now].val==val) return po[po[now].l].size+;
else
if (val>po[now].val) return po[po[now].l].size+po[now].w+ask_rank(po[now].r,val);
else return ask_rank(po[now].l,val);
} int ask_num(llg now,int val)
{
if (now==) return ;
if (val<=po[po[now].l].size)
return ask_num(po[now].l,val);
else if (val>po[po[now].l].size+po[now].w)
return ask_num(po[now].r,val-po[po[now].l].size-po[now].w);
else
return po[now].val;
} void pre(llg now,llg val)
{
if (now==) return ;
if (po[now].val<val)
{
ans=now;
pre(po[now].r,val);
}
else pre(po[now].l,val);
} void nex(llg now,llg val)
{
if (now==) return ;
if (po[now].val>val)
{
ans=now;
nex(po[now].l,val);
}
else nex(po[now].r,val);
} }tree; int main()
{
yyj("");
srand();
tree.init();
cin>>n;
llg ty,x;
for (llg i=;i<=n;i++)
{
ty=getint(),x=getint();
switch(ty)
{
case :tree.in(x);break;
case :tree.del(tree.root,x);break;
case :printf("%d\n",tree.ask_rank(tree.root,x));break;
case :printf("%d\n",tree.ask_num(tree.root,x));break;
case :ans=;tree.pre(tree.root,x);printf("%d\n",tree.po[ans].val);break;
case :ans=;tree.nex(tree.root,x);printf("%d\n",tree.po[ans].val);break;
}
}
return ;
}
 
 

【bzoj】3224: Tyvj 1728 普通平衡树的更多相关文章

  1. BZOJ 3224: Tyvj 1728 普通平衡树

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 9629  Solved: 4091[Submit][Sta ...

  2. BZOJ 3224 TYVJ 1728 普通平衡树 [Treap树模板]

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 7390  Solved: 3122 [Submit][S ...

  3. BZOJ 3224: Tyvj 1728 普通平衡树 treap

    3224: Tyvj 1728 普通平衡树 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除 ...

  4. BZOJ 3224: Tyvj 1728 普通平衡树 vector

    3224: Tyvj 1728 普通平衡树 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除 ...

  5. BZOJ 3224: Tyvj 1728 普通平衡树(BST)

    treap,算是模板题了...我中间还一次交错题... -------------------------------------------------------------------- #in ...

  6. BZOJ 3224: Tyvj 1728 普通平衡树 or 洛谷 P3369 【模板】普通平衡树-Splay树模板题

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 22483  Solved: 10130[Submit][S ...

  7. BZOJ 3224 Tyvj 1728 普通平衡树模板

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 题目大意: 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以 ...

  8. bzoj 3224: Tyvj 1728 普通平衡树 && loj 104 普通平衡树 (splay树)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 思路: splay树模板题: 推荐博客:https://blog.csdn.ne ...

  9. bzoj 3224/Tyvj 1728 普通平衡树(splay)

    Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...

  10. fhq_treap || BZOJ 3224: Tyvj 1728 普通平衡树 || Luogu P3369 【模板】普通平衡树

    题面:[模板]普通平衡树 代码: #include<cstdio> #include<cstring> #include<iostream> #include< ...

随机推荐

  1. Python自动发邮件-yagmail库

    之前写过用标准库使用Python Smtplib和email发送邮件,感觉很繁琐,久了不用之后便忘记了.前几天看知乎哪些Python库让你相见恨晚?,看到了yagmail第三方库,学习过程中遇到一些问 ...

  2. Ubuntu16.04 安装 “宋体,微软雅黑,Consolas雅黑混合版编程字体” 等 Windows 7 下的字体

    Windows平台下,“宋体”.“微软雅黑”.“Courier New(编程字体)”用的比较多,看的也习惯了.那如何在 Ubuntu下也安装这些字体呢? 操作步骤如下: 第一步:从 Windows 7 ...

  3. Outlier Detection

    1)正态分布数据,飘出95%的可能是异常值.变量var正态标准化,|var|<=1.96的可能是异常值,further chk needed!large sample better. 对于偏态分 ...

  4. [LeetCode] All questions numbers conclusion 所有题目题号

    Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...

  5. 从游戏开发到web前端——仅仅只是开始

    文章开头,请允许我随便扯扯. 一来,开头从来都是最难写的,二来,描述我现在的心情和状态以及工作背景啥的,对于大家理解后面的内容也许会有所帮助~ 2012年211大学毕业,工作4年了,一直都是做游戏前端 ...

  6. CTC(Connectionist Temporal Classification)介绍

    CTC解决什么问题 CTC,Connectionist Temporal Classification,用来解决输入序列和输出序列难以一一对应的问题. 举例来说,在语音识别中,我们希望音频中的音素和翻 ...

  7. Git本地仓库与远程github同步的时候提示fatal: remote origin already exists 错误解决办法

    Git本地仓库与远程github同步的时候提示fatal: remote origin already exists 错误解决办法 1.git在本地的电脑创建了仓库,要远程同步github的仓库.使用 ...

  8. Linux服务器配置---tftpserver

    安装tftp-server 1.安装tftp-server [root@localhost weijie]# yum install -y tftp-server Loaded plugins: fa ...

  9. HttpClient配置SSL绕过https证书

    https://blog.csdn.net/irokay/article/details/78801307 HttpClient简介 HTTP 协议可能是现在 Internet 上使用得最多.最重要的 ...

  10. DBMS_OUTPUT.PUT_LINE()方法的简单介绍

    1.最基本的DBMS_OUTPUT.PUT_LINE()方法. 随便在什么地方,只要是BEGIN和END之间,就可以使用DBMS_OUTPUT.PUT_LINE(output);然而这会有一个问题,就 ...