[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 ...
随机推荐
- DOM事件类型总结大全
unload:事件在用户退出页面时发生 window.onload = function(){ return "页面关闭!"; }; onblur:失去焦点发生变化 window. ...
- Java 并发 – 线程安全?
线程安全的定义常常让人迷惑,搜索引擎会发现无数定义,比如: 多个线程同时执行也能正确工作就是线程安全的代码 多个线程同时执行能以正确的方式操纵共享数据就是线程安全的代码. 而且还有很多类似的定义 你是 ...
- JAVA web.xml中引用多个XML
web.xml里加<context-param><param-name>contextConfigLocation</param-name><param-va ...
- CoolBlog开发笔记第3课:创建Django应用
教程目录 1.1 CoolBlog开发笔记第1课:项目分析 1.2 CoolBlog开发笔记第2课:搭建开发环境 前言 经过上一节我们已经创建了CoolBlog工程,但是关于CoolBlog的功能代码 ...
- 进程cookie与硬盘cookie
内存cookie,是指没有设在cookie的Expires(过期时间)的属性硬盘cookie,是指在你设置了cookie的Expires(过期时间)属性 关于session的几点理解与测试 同一个浏览 ...
- Java 9 揭秘(6. 封装模块)
Tips 做一个终身学习的人. 在这章节中, 主要介绍以下内容: 封装Java模块的不同格式 JAR格式增强 什么是多版本JAR 如何创建和使用多版本JAR JMOD是什么格式 如何使用jmod工具来 ...
- AngularJS模块
方式一: <body ng-app="myApp"> <div ng-controller="myCtrl1"> <h1>{ ...
- linq 批量修改更新
批量修改:var values = Context.Request["values"].JsonDeserialize<Dictionary<string, objec ...
- python自动化运维学习第一天--day1
学习python自动化运维第一天自己总结的作业 所使用到知识:json模块,用于数据转化sys.exit 用于中断循环退出程序字符串格式化.format字典.文件打开读写with open(file, ...
- Linux之定时任务补充
定时任务两实例 例1: 每分钟打印一次自己的名字拼音全拼到“/server/log/自己的名字命名的文件”中. [root@chengliang log]# mkdir -p /server/log/ ...