3224: Tyvj 1728 普通平衡树

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 7390  Solved: 3122
[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的模板题,涉及了treap的各种操作
操作函数中运用了传引用的方式改变上一层结点的子节点值,很方便
 
 /*by SilverN*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
struct treap{
int l,r;//左右子树
int val,ct;//值 重复次数
int size,rand;//管控数量,随机权值
}t[];
int root=,cnt=;
int ans;
void update(int k){
t[k].size=t[t[k].l].size+t[t[k].r].size+t[k].ct;
}
void lt(int &k){//左旋
int now=t[k].r;
t[k].r=t[now].l;
t[now].l=k;
t[now].size=t[k].size;
update(k);
k=now;
return;
}
void rt(int &k){
int now=t[k].l;
t[k].l=t[now].r;
t[now].r=k;
t[now].size=t[k].size;
update(k);
k=now;
return;
}
void insert(int &k,int x){//&k是为了建新节点 x为插入值
if(k==){//建新节点
t[++cnt].val=x;
t[cnt].size=;
t[cnt].ct=;
t[cnt].rand=rand();
k=cnt;
return;
}
t[k].size++;
if(t[k].val==x) t[k].ct++;//与节点等值
else if(x>t[k].val){
insert(t[k].r,x);
if(t[t[k].r].rand<t[k].rand) lt(k);
}else{//x<t[k].val
insert(t[k].l,x);
if(t[t[k].l].rand<t[k].rand) rt(k);
}
return;
}
void del(int &k,int x){
if(k==)return;
if(t[k].val==x){
if(t[k].ct>){t[k].ct--;t[k].size--;return;}
if(t[k].l*t[k].r==)k=t[k].l+t[k].r;//如果k是链结点(只有一个子节点),由其子节点补位
else{
if(t[t[k].l].rand<t[t[k].r].rand){
rt(k);
del(k,x);
}
else{
lt(k);
del(k,x);
}
}
return; }
t[k].size--;
if(x>t[k].val)del(t[k].r,x);
if(x<t[k].val)del(t[k].l,x);
return;
}
void ask_p(int k,int x,int mode){//前驱 //mode==1 ->前驱 mode==2 ->后驱
if(mode==){
if(!k)return;
if(t[k].val<x){//依照二叉树的性质,要找小的往左查,要找大的往右查
ans=t[k].val;
ask_p(t[k].r,x,);
}else ask_p(t[k].l,x,);
return;
}
if(mode==){
if(!k)return;
if(t[k].val>x){
ans=t[k].val;
ask_p(t[k].l,x,);
}else ask_p(t[k].r,x,);
}
return;
}
int ask_rank(int k,int x){//已知数字问排名
if(!k)return ;
if(t[k].val==x)return t[t[k].l].size+;
if(t[k].val<x)return t[k].ct+t[t[k].l].size+ask_rank(t[k].r,x);
else return ask_rank(t[k].l,x);
}
int ask_num(int k,int x){// 已知排名问数字
if(!k)return ;
if(x<=t[t[k].l].size)return ask_num(t[k].l,x);//排名小于左子树包含结点数量,则往左查
if(x>t[t[k].l].size+t[k].ct)return ask_num(t[k].r,x-t[t[k].l].size-t[k].ct);
//排名大于“左子树结点数加父结点重复数”,则往右查
else return t[k].val;//否则返回父结点值
}
int main(){
int opt,x;
scanf("%d",&n);
while(n--){
scanf("%d%d",&opt,&x);
switch(opt){
case : insert(root,x); break;
case : del(root,x); break;
case : printf("%d\n",ask_rank(root,x)); break;
case : printf("%d\n",ask_num(root,x)); break;
case : ask_p(root,x,);printf("%d\n",ans);break;//前驱
case : ask_p(root,x,);printf("%d\n",ans);break;//前驱
}
// for(int i=1;i<=cnt;i++)printf("%d ",t[i].val);
// cout<<endl;
}
return ;
}

BZOJ 3224 TYVJ 1728 普通平衡树 [Treap树模板]的更多相关文章

  1. BZOJ - 3224 Tyvj 1728 普通平衡树 (treap/树状数组)

    题目链接 treap及树状数组模板题. treap版: #include<bits/stdc++.h> using namespace std; typedef long long ll; ...

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

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

  3. bzoj 3224: Tyvj 1728 普通平衡树 替罪羊树

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

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

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

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

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

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

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

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

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

  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 普通平衡树【非旋treap】

    就是非旋treap的板子 #include<iostream> #include<cstdio> #include<cstdlib> using namespace ...

随机推荐

  1. scp: command not found如何解决

    今天给一台新的服务器,准备源码安装一些软件,需要使用scp复制文件时报错如下:-bash: scp: command not found 解决办法如下:安装scp的软件包:# yum install ...

  2. 看ImplicitBackwardEulerSparse关于static solve的代码

    当选择static solve的时候,求解的流程如下: 1.获得内力 2.qresidual = 外力-内力,qdelta = qresidual, qdelta的非约束元素赋给bufferConst ...

  3. jenkins配置记录(2)--代码发布流程

    在我们的日常运维工作中,使用jenkins来完成业务代码发版上线是至关重要的一环.前面已经提到在jenkins上添加用户权限的操作,今天重点说下如何在jenkins下构建项目工程进行代码发布? 在此简 ...

  4. poj 3352

    Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11215 Accepted: 5575 De ...

  5. HashMap 中的 entrySet()使用方法 2016.12.28

    package map; import java.util.HashMap; import java.util.Iterator; import java.util.Map.Entry; import ...

  6. flask 使用 SQLAlchemy 的两种方式

    1. 使用 flask-SQLAlchemy 扩展 # flask-ext-sqlalchemy.py from flask import Flask from flask.ext.sqlalchem ...

  7. MVC3迁移MVC4相关问题

    mvc3迁移到mvc4后,发布到服务器,出现了如下错误: [A]System.Web.WebPages.Razor.Configuration.RazorPagesSection cannot be ...

  8. SQL Server优化50法

    查询速度慢的原因很多,常见如下几种:    1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷)    2.I/O吞吐量小,形成了瓶颈效应.    3.没有创建计算列导致查询不优化 ...

  9. 面试准备(四)Java基本数据类型

    Java语言是静态类型的(statical typed),也就是说所有变量和表达式的类型再编译时就已经完全确定.由于是statical typed,导致Java语言也是强类型(Strong typed ...

  10. LeetCode:Best Time to Buy and Sell Stock I II III

    LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...