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. 前端js如何生成一个对象,并转化为json字符串

    https://www.cnblogs.com/May-day/p/6841958.html 一,直接上代码 <script src="../../Content/jquery-2.0 ...

  2. testng入门教程15数据驱动

    testng在类 里面的数据驱动 package driver_test; import org.testng.annotations.DataProvider; import org.testng. ...

  3. http 之 HTTP_X_FORWARDED_FOR

    原文   http://www.imququ.com/post/x-forwarded-for-header-in-http.html 主题 HTTP Nginx 我一直认为,对于从事 Web 前端开 ...

  4. HDU 1432 Lining Up(几何)

    http://acm.hdu.edu.cn/showproblem.php?pid=1432 题目大意: 2维平面上给定n个点,求一条直线能够穿过点数最多是多少. 解题思路: 因为题目给定的n(1~7 ...

  5. sift 与 surf 算法

    http://blog.csdn.net/cy513/article/details/4414352 SURF算法是SIFT算法的加速版,OpenCV的SURF算法在适中的条件下完成两幅图像中物体的匹 ...

  6. 20155334 2016-2017-2 《Java程序设计》第八周学习总结

    20155334 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 第十四章:NIO与NIO2 NIO的定义: InputStream.OutputStream ...

  7. python+Django框架运用(一)

    Django 介绍: django是一个采用Python语言开发的开源框架,2005年发布.早期是做新闻以及内容管理的网站的,提供了非常强大的后管理系统. django官网:https://www.d ...

  8. Python: yield, python 实现tail -f

    def CreateGenerator(file): with open(file,'r') as t: t.seek(0,2) while True: line=t.readline() if no ...

  9. Python tricks(4) -- with statement

    简介 with是从2.5版本引入的一个语法. 这个语法本身是为了解决try..finally繁琐的释放各类资源(文件句柄, Lock等)的问题. 如果想在旧版本中使用这个功能, 直接引入future模 ...

  10. C/C++之内存分配

    一.编译时与运行时的内存情况1.编译时不分配内存编译时是不分配内存的.此时只是根据声明时的类型进行占位,到以后程序执行时分配内存才会正确.所以声明是给编译器看的,聪明的编译器能根据声明帮你识别错误.2 ...