[Tyvj 1728] 普通平衡树
大名鼎鼎的板子题w
照例先贴题面
Describtion
您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
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 493598Sample Output
106465
84185
492737Hint
1.n的数据范围:n<=1000002.每个数的数据范围:[-1e7,1e7]
#include <ctime>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std; const int INF=0x7FFFFFFF;
#define lch chd[0]
#define rch chd[1]
#define kch chd[k]
#define xch chd[k^1] class Treap{
private:
int v;
int k;
int s;
public:
Treap* chd[];
Treap(int v=,Treap* l=NULL,Treap* r=NULL,int s=,int k=rand()){
this->v=v;
this->s=s;
this->k=k;
this->chd[]=l;
this->chd[]=r;
}
inline void Maintain(){
if(this->exist())
this->s=this->lch->size()+this->rch->size()+;
}
inline int value(){
return this==NULL?:v;
}
inline int key(){
return this==NULL?INF:k;
}
inline int size(){
return this==NULL?:s;
}
inline bool empty(){
return this==NULL;
}
inline bool exist(){
return this!=NULL;
}
}*Root=NULL; void Insert(Treap*&,int);
void Delete(Treap*&,int);
void Rotate(Treap*&,int);
int Rank(int);
int Kth(int);
int Predecessor(int);
int Successor(int); inline void PrintTree(){
queue<Treap*> q;
q.push(Root);
while(!q.empty()){
if(q.front()->empty()){
puts("(v=@@@,s=@@@,k=@@@)");
}
else{
printf("(v=%d,s=%d,k=%d)\n",q.front()->value(),q.front()->size(),q.front()->key());
q.push(q.front()->lch);
q.push(q.front()->rch);
}
q.pop();
}
printf("\n\n\n\n");
} int main(){
freopen("phs.in","r",stdin);
freopen("phs.out","w",stdout);
srand(time(NULL));
int n,type,num;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",&type,&num);
if(type==)
Insert(Root,num);
else if(type==)
Delete(Root,num);
else if(type==)
printf("%d\n",Rank(num));
else if(type==)
printf("%d\n",Kth(num));
else if(type==)
printf("%d\n",Predecessor(num));
else if(type==)
printf("%d\n",Successor(num));
// PrintTree();
}
return ;
} void Insert(Treap* &root,int x){
if(root->empty()){
root=new Treap(x);
return;
}
else{
int k=x<root->value();
Insert(root->xch,x);
root->Maintain();
if(root->xch->key()>root->key())
Rotate(root,k);
}
} void Delete(Treap* &root,int x){
if(root->empty())
return;
else{
if(root->value()==x){
if(root->lch->exist()&&root->rch->exist()){
int k=root->lch->key()>root->rch->key();
Rotate(root,k);
Delete(root->kch,x);
}
else{
Treap* tmp=root;
if(root->lch->exist())
root=root->lch;
else
root=root->rch;
delete tmp;
}
}
else
Delete(root->chd[x>=root->value()],x);
root->Maintain();
}
} inline int Rank(int x){
Treap* root=Root;
int ans=;
while(root->exist()){
if(x<=root->value())
root=root->lch;
else{
ans+=root->lch->size()+;
root=root->rch;
}
}
return ans;
} inline int Kth(int x){
Treap* root=Root;
int k;
while(root->exist()){
k=root->lch->size()+;
if(x<k)
root=root->lch;
else if(x==k)
return root->value();
else{
x-=k;
root=root->rch;
}
}
return ;
} inline int Predecessor(int x){
return Kth(Rank(x)-);
} inline int Successor(int x){
return Kth(Rank(x+));
} inline void Rotate(Treap* &root,int k){
Treap* tmp=root->xch;
root->xch=tmp->kch;
root->Maintain();
tmp->kch=root;
tmp->Maintain();
root=tmp;
}
Treap
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #define lch chd[0]
#define rch chd[1]
#define kch chd[k]
#define xch chd[k^1] const int INF=0x7FFFFFFF; class SplayTree{
private:
struct Node{
int k;
int s;
Node* prt;
Node* chd[];
Node(const int& key){
this->k=key;
this->s=;
this->prt=NULL;
this->lch=NULL;
this->rch=NULL;
}
inline int size(){
return this==NULL?:this->s;
}
inline int key(){
return this==NULL?:this->k;
}
inline void Maintain(){
if(this!=NULL)
this->s=this->lch->size()+this->rch->size()+;
}
inline int Pos(){
return this==this->prt->lch;
}
}*root; void Rotate(Node* root,int k){
Node* tmp=root->xch;
if(root->prt==NULL)
this->root=tmp;
else if(root->prt->lch==root)
root->prt->lch=tmp;
else
root->prt->rch=tmp;
tmp->prt=root->prt;
root->xch=tmp->kch;
if(root->xch!=NULL)
root->xch->prt=root;
tmp->kch=root;
root->prt=tmp;
root->Maintain();
tmp->Maintain();
} void Splay(Node* root,Node* prt=NULL){
while(root->prt!=prt){
int k=root->Pos();
if(root->prt->prt==prt){
this->Rotate(root->prt,k);
}
else{
int d=root->prt->Pos();
this->Rotate(k==d?root->prt->prt:root->prt,k);
this->Rotate(root->prt,d);
}
}
}
public:
Node* Kth(int pos){
Node* root=this->root;
while(root!=NULL){
int k=root->lch->size()+;
if(pos<k)
root=root->lch;
else if(pos==k)
return root;
else{
pos-=k;
root=root->rch;
}
}
return NULL;
} int Rank(const int& key){
Node* root=this->root;
int rank=;
while(root!=NULL){
if(root->key()<key){
rank+=root->lch->size()+;
root=root->rch;
}
else
root=root->lch;
}
return rank;
} void Insert(const int& key){
int pos=this->Rank(key)-;
this->Splay(this->Kth(pos));
this->Splay(this->Kth(pos+),this->root);
Node* tmp=new Node(key);
this->root->rch->lch=tmp;
tmp->prt=this->root->rch;
this->root->rch->Maintain();
this->root->Maintain();
} void Delete(const int& key){
int pos=this->Rank(key);
this->Splay(this->Kth(pos-));
this->Splay(this->Kth(pos+),root);
delete this->root->rch->lch;
this->root->rch->lch=NULL;
this->root->rch->Maintain();
this->root->Maintain();
} inline int Predecessor(const int& key){
return this->Kth(this->Rank(key)-)->key();
} inline int Successor(const int& key){
return this->Kth(this->Rank(key+))->key();
} SplayTree(){
this->root=new Node(-INF);
this->root->rch=new Node(INF);
this->root->rch->prt=this->root;
this->root->rch->Maintain();
this->root->Maintain();
}
}; int main(){
#ifndef ASC_LOCAL
freopen("phs.in","r",stdin);
freopen("phs.out","w",stdout);
#endif
SplayTree* T=new SplayTree();
int t,opt,key;
scanf("%d",&t);
while(t--){
scanf("%d%d",&opt,&key);
if(opt==)
T->Insert(key);
else if(opt==)
T->Delete(key);
else if(opt==)
printf("%d\n",T->Rank(key)-);
else if(opt==)
printf("%d\n",T->Kth(key+)->key());
else if(opt==)
printf("%d\n",T->Predecessor(key));
else
printf("%d\n",T->Successor(key));
}
return ;
}
Splay
[Tyvj 1728] 普通平衡树的更多相关文章
- [BZOJ3224]Tyvj 1728 普通平衡树
[BZOJ3224]Tyvj 1728 普通平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个) ...
- BZOJ 3224: Tyvj 1728 普通平衡树
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 9629 Solved: 4091[Submit][Sta ...
- 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数(若有多个相同的数,因只删除 ...
- bzoj3224 Tyvj 1728 普通平衡树(名次树+处理相同)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5354 Solved: 2196[Submit][Sta ...
- BZOJ 3224: Tyvj 1728 普通平衡树(BST)
treap,算是模板题了...我中间还一次交错题... -------------------------------------------------------------------- #in ...
- [补档][Tyvj 1728]普通平衡树
[Tyvj 1728]普通平衡树 题目 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 1. 插入x数 2. 删除x数(若有多个相同的数,因只删除一个) 3. 查询x数的 ...
- 【bzoj】3224: Tyvj 1728 普通平衡树
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 10097 Solved: 4302[Submit][St ...
- BZOJ_3224 Tyvj 1728 普通平衡树 【离散化+权值线段树】
一 题面 Tyvj 1728 普通平衡树 二 分析 比较明显是可以用平衡二叉搜索树(splay)做的. 用权值线段树做,前提就是要先离散化,因为权值线段树维护的值域信息. 板子. 三 AC代码 #in ...
随机推荐
- 快速压缩PNG文件在线工具
https://tinypng.com/ 直接拖移要压缩文件即可
- Bash中的特殊变量和位置参量
位置参量:向脚本或函数传递的参数,可以被set命令设置.重置和清空. 1.$$ 当前Shell的PID 2.$- 当前Shell的选项,如果是交互式shell,应该包含字符i,例如$ echo $-h ...
- Here文档
Here文档为需要输入的程序,例如,mail.sort和cat等接收在线文本,直到遇到用户定义的结束符号为止.最常用的用户是在Shell脚本中和case命令一起创建菜单.自动登录等等. 1.建立菜单 ...
- Winform中Chart图表的简单使用
在常见的一些数据采集的系统中, 都少不了一个就是, 数据分析, 无论是报表的形式, 还是图形的形式. 他都是可以迅速的展现一个数据趋势的实现方法, 而今天, 就是简单介绍一下, 微软的工具库自带的 C ...
- Linux命令 查看文件内容
cat [功能说明] 查看文件的内容 #cat本身是一个串接命令,把指定一个或多个源文件的内容,利用>符号重定向到目标文件中,如果不指定重定向文件,则默认在标准输出设备上显示.此时,可以利用c ...
- react-native 学习 ----- React Navigation
很久没有的登陆博客园了,密码都是找回的,从当年的大学生已经正常的走上了程序员的道路,看到之前发的博客还是写的android,现在自己已经在使用了react-native了. 大学毕业了,做了java后 ...
- ⑨的完美冻青蛙(frog)
⑨的完美冻青蛙(frog) 时间限制: 1 Sec 内存限制: 128 MB 题目描述 输入 第一行是一个正整数n,表示上式中的p的个数. 接下来有n行,每一行两个正整数pi 和ei . 输出 ...
- 使用zabbix_agent监控第一台linux服务器
添加zabbix用户和组 groupadd zabbix useradd -g zabbix zabbix 解压并编译安装 tar xf zabbix-3.2.1.tar.gz cd zabbix-3 ...
- 简单轻量级的一套JS 类库(RapidDevelopmentFramework.JS)
1.最近好久没有更新自己的博客了,一直在考虑自己应该写一些什么.4.2日从苏州回到南京的路上感觉自己的内心些崩溃和失落,我就不多说了? 猛然之间我认为自己需要找一下内心的平衡.决定开发属于自己一套快速 ...
- win7老是弹出“Windows Media PIayer网络共享服务配置应用程序 已停止工作”
应是优化软件的时候把服务禁止启动了. 我的电脑 > 管理 > 服务和应用程序 > 服务 Windows Media Player Network Sharing Service 启动 ...