BZOJ 3224: Tyvj 1728 普通平衡树
3224: Tyvj 1728 普通平衡树
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 9629 Solved: 4091
[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
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
Sample Output
84185
492737
HINT
Source
FHQ Treap
#include <bits/stdc++.h> const int N = ; int ls[N], rs[N], vl[N], tg[N], sz[N]; inline int node(int v)
{
static int t = ;
sz[t] = ;
vl[t] = v;
tg[t] = rand();
return t++;
} int merge(int a, int b)
{
if (!a || !b)return a + b;
if (tg[a] > tg[b])
{
rs[a] = merge(rs[a], b);
sz[a] = + sz[ls[a]] + sz[rs[a]];
return a;
}
else
{
ls[b] = merge(a, ls[b]);
sz[b] = + sz[ls[b]] + sz[rs[b]];
return b;
}
} void split(int t, int k, int &a, int &b)
{
if (!t)a = b = ;
else
{
if (vl[t] <= k)
a = t, split(rs[t], k, rs[t], b);
else
b = t, split(ls[t], k, a, ls[t]);
sz[t] = + sz[ls[t]] + sz[rs[t]];
}
} int kth(int t, int k)
{
if (k <= sz[ls[t]])
return kth(ls[t], k);
else if (k == sz[ls[t]] + )
return t;
else
return kth(rs[t], k - sz[ls[t]] - );
} signed main(void)
{
srand(); int n, r = ; scanf("%d", &n); for (int a, b, x, y, z; n--; )
{
scanf("%d%d", &a, &b); if (a == )
{
split(r, b, x, y);
r = merge(x, node(b));
r = merge(r, y);
}
else if (a == )
{
split(r, b, x, z);
split(x, b - , x, y);
y = merge(ls[y], rs[y]);
r = merge(x, y);
r = merge(r, z);
}
else if (a == )
{
split(r, b - , x, y);
printf("%d\n", sz[x] + );
r = merge(x, y);
}
else if (a == )
printf("%d\n", vl[kth(r, b)]);
else if (a == )
{
split(r, b - , x, y);
printf("%d\n", vl[kth(x, sz[x])]);
r = merge(x, y);
}
else
{
split(r, b, x, y);
printf("%d\n", vl[kth(y, )]);
r = merge(x, y);
}
}
}
#include <bits/stdc++.h>
const int N = ;
int ls[N], rs[N], vl[N], tg[N], sz[N], tot = ;
int node(int v) {
return vl[tot] = v, sz[tot] = , tg[tot] = rand(), tot++;
}
int merge(int a, int b) {
if (!a || !b)return a + b;
if (tg[a] > tg[b]) {
rs[a] = merge(rs[a], b);
sz[a] = + sz[ls[a]] + sz[rs[a]];
return a;
}
else {
ls[b] = merge(a, ls[b]);
sz[b] = + sz[ls[b]] + sz[rs[b]];
return b;
}
}
int split(int t, int k, int &a, int &b) {
if (!t)return a = b = , ;
if (vl[t] <= k)
a = t, split(rs[t], k, rs[t], b);
else
b = t, split(ls[t], k, a, ls[t]);
return sz[t] = + sz[ls[t]] + sz[rs[t]];
}
int kth(int t, int k) {
return k <= sz[ls[t]] ? kth(ls[t], k) : ((k -= sz[ls[t]] + ) ? kth(rs[t], k) : vl[t]);
}
signed main(void) {
int n, r = , a, b, x, y, z;
for (scanf("%d", &n); n--; ) {
scanf("%d%d", &a, &b);
if (a == )
split(r, b, x, y), r = merge(merge(x, node(b)), y);
else if (a == )
split(r, b, x, z), split(x, b - , x, y), r = merge(merge(x, merge(ls[y], rs[y])), z);
else if (a == )
split(r, b - , x, y), printf("%d\n", sz[x] + ), r = merge(x, y);
else if (a == )
printf("%d\n", kth(r, b));
else if (a == )
split(r, b - , x, y), printf("%d\n", kth(x, sz[x])), r = merge(x, y);
else
split(r, b, x, y), printf("%d\n", kth(y, )), r = merge(x, y);
}
}
@Author: YouSiki
BZOJ 3224: Tyvj 1728 普通平衡树的更多相关文章
- BZOJ 3224 TYVJ 1728 普通平衡树 [Treap树模板]
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 7390 Solved: 3122 [Submit][S ...
- BZOJ 3224: Tyvj 1728 普通平衡树 treap
3224: Tyvj 1728 普通平衡树 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除 ...
- BZOJ 3224: Tyvj 1728 普通平衡树 vector
3224: Tyvj 1728 普通平衡树 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除 ...
- BZOJ 3224: Tyvj 1728 普通平衡树(BST)
treap,算是模板题了...我中间还一次交错题... -------------------------------------------------------------------- #in ...
- BZOJ 3224: Tyvj 1728 普通平衡树 or 洛谷 P3369 【模板】普通平衡树-Splay树模板题
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 22483 Solved: 10130[Submit][S ...
- BZOJ 3224 Tyvj 1728 普通平衡树模板
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 题目大意: 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以 ...
- bzoj 3224: Tyvj 1728 普通平衡树 && loj 104 普通平衡树 (splay树)
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 思路: splay树模板题: 推荐博客:https://blog.csdn.ne ...
- bzoj 3224/Tyvj 1728 普通平衡树(splay)
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...
- fhq_treap || BZOJ 3224: Tyvj 1728 普通平衡树 || Luogu P3369 【模板】普通平衡树
题面:[模板]普通平衡树 代码: #include<cstdio> #include<cstring> #include<iostream> #include< ...
随机推荐
- configure Git to accept a particular self-signed server certificate for a particular https remote
get the self signed certificate put it into some (e.g. ~/git-certs/cert.pem) file set git to trust t ...
- 使用 Async 和 Await 的异步编程(C# 和 Visual Basic)[msdn.microsoft.com]
看到Microsoft官方一篇关于异步编程的文章,感觉挺好,不敢独享,分享给大家. 原文地址:https://msdn.microsoft.com/zh-cn/library/hh191443.asp ...
- vimrc
我的vimrc https://github.com/juandx/vimrc 当然得装vundle git clone https://github.com/VundleVim/Vundle.vim ...
- IIS7如何部署asp.net网站
第一步:发布网站 右键asp.net web项目,选择发布, 然后新建配置文件名称并选择 "文件系统" 发布方法. 目标位置选择本地新建的文件夹如: IISWebSite 第二 ...
- The process could not execute 'sp_repldone/sp_replcounters' on 'ServerName'
昨天发现发布服务器S(SQL Server 2008 R2),出现大量如下错误 错误细节如下所示: Date :: PM :: PM) Source spid52 Message Replicatio ...
- ORA-00911: invalid character --- 字符集的问题
网上搜了一遍, 大多数是因为分号( ; ) 的问题. 而我的sql文件是没有分号的, 最后发现是sql文件编码和服务器字符集的差异造成 sql文件怎么都看不出问题,直到在UltraEdit里切换到1 ...
- java实现基于activeMQ的消息推送
一. 准备工作 1. 点击此下载相关的第三方jar包,并在工程中引用 二. spring配置文件:application.xml <?xml version="1.0" en ...
- android ListView 属性
android:divider="#fffff" 分割线颜色 android:dividerHeight="1px" 分割线高度 divider 分割线-去掉分 ...
- hdfs client access the hdfs cluster not in one domain
https://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-hdfs/HdfsMultihoming.html#Clients_u ...
- AngularJS笔记---作用域和控制器
什么是作用域. 什么是控制器, 作用域包含了渲染视图时所需的功能和数据,它是所有视图的唯一源头.可以将作用域理解成试图模型(ViewModel). 作用域之间可以是包含关系也可以是独立关系.可以通过设 ...