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

--------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
 
#define rep(i,n) for(int i=0;i<n;++i)
#define clr(x,c) memset(x,c,sizeof(x))
 
using namespace std;
 
const int inf=1e9;
const int maxn=100000+5;
 
struct Node {
Node *ch[2];
int r;
int v;
int s;
Node(int v=0):v(v) {
   ch[0]=ch[1]=NULL;
   r=rand();
   s=1;
}
int cmp(int x) const {
if(x==v) return -1;
return x<v ? 0 : 1;
}
void maintain() {
s=1;
if(ch[0]!=NULL) s+=ch[0]->s;
if(ch[1]!=NULL) s+=ch[1]->s;
}
};
 
void rotate(Node* &o,int d) {
Node* k=o->ch[d^1];
o->ch[d^1]=k->ch[d];
k->ch[d]=o;
o->maintain(); k->maintain();
o=k;
}
 
void insert(Node* &o,int x) {
if(o==NULL) o=new Node(x);
else {
int d=(x<o->v ? 0 : 1);
insert(o->ch[d],x);
if(o->ch[d]->r > o->r) rotate(o,d^1);
}
o->maintain();
}
 
Node* find(Node* o,int x) {
if(o==NULL) return NULL;
if(x==o->v) return o;
return x<o->v ? find(o->ch[0],x) : find(o->ch[1],x);
}
 
void remove(Node* &o,int x) {
int d=o->cmp(x);
if(d==-1) {
Node* k=o;
if(o->ch[0]!=NULL && o->ch[1]!=NULL) {
int d2=(o->ch[0]->r > o->ch[1]->r ? 1 : 0);
rotate(o,d2); remove(o->ch[d2],x);
} else {
if(o->ch[0]==NULL) o=o->ch[1];
else o=o->ch[0];
delete k;
   }
} else remove(o->ch[d],x);
if(o!=NULL) o->maintain();
}
 
int rank(Node* o,int x) {
if(o==NULL) return 1;
if(x<=o->v) return rank(o->ch[0],x);
return rank(o->ch[1],x)+(o->ch[0]==NULL ? 0 : o->ch[0]->s)+1;
}
 
int kth(Node* o,int k) {
if(o==NULL || k<=0 || k>o->s) return 0;
int s=(o->ch[0]==NULL ? 0 : o->ch[0]->s);
if(k==s+1) return o->v;
else if(k<=s) return kth(o->ch[0],k);
    else return kth(o->ch[1],k-s-1);
}
 
int pred(Node* o,int x) {
int ans=-inf;
while(o!=NULL) {
int d=(x>=o->v ? 1 : 0);
if(d) ans=max(ans,o->v);
o=o->ch[d];
}
return ans;
}
 
int succ(Node* o,int x) {
int ans=inf;
while(o!=NULL) {
int d=(x<=o->v ? 0 : 1);
if(!d) ans=min(ans,o->v);
o=o->ch[d];
}
return ans;
}
int main()
{
// freopen("test.in","r",stdin);
Node* root=new Node(inf);
int n,t;
scanf("%d",&n);
while(n--) {
int a,b;
scanf("%d%d",&a,&b);
switch(a) {
case 1 : insert(root,b); break;
case 2 : if(find(root,b)!=NULL) remove(root,b); break;
case 3 : printf("%d\n",rank(root,b)); break;
case 4 : printf("%d\n",kth(root,b)); break;
case 5 : printf("%d\n",pred(root,b-1)); break;
case 6 : printf("%d\n",succ(root,b+1)); break;
default: break;
}
}
return 0;
}

--------------------------------------------------------------------

3224: Tyvj 1728 普通平衡树

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 3328  Solved: 1349
[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

BZOJ 3224: Tyvj 1728 普通平衡树(BST)的更多相关文章

  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 普通平衡树 or 洛谷 P3369 【模板】普通平衡树-Splay树模板题

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

  6. BZOJ 3224 Tyvj 1728 普通平衡树模板

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 题目大意: 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以 ...

  7. bzoj 3224: Tyvj 1728 普通平衡树 && loj 104 普通平衡树 (splay树)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 思路: splay树模板题: 推荐博客:https://blog.csdn.ne ...

  8. bzoj 3224/Tyvj 1728 普通平衡树(splay)

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

  9. fhq_treap || BZOJ 3224: Tyvj 1728 普通平衡树 || Luogu P3369 【模板】普通平衡树

    题面:[模板]普通平衡树 代码: #include<cstdio> #include<cstring> #include<iostream> #include< ...

随机推荐

  1. iOS动态运行时方法

    在某些时候,程序可能需要根据获取的参数来决定调用的方法. 要实现这样的功能,就需要使用到动态运行时方法了. 首先需要定义好接口,以便调用. 然后就是动态调用定义好的方法. 这里有两种方法, 第一种: ...

  2. 仅仅需手动添加一行代码就可以让Laravel4执行在SAE (v. 1.0.0)

    Github:https://github.com/chariothy/laravel4-sae (已更新至v1.1.0) laravel4-sae (v. 1.0.0) 仅仅需手动添加一行代码就可以 ...

  3. HDU 4715 Difference Between Primes (打表)

    Difference Between Primes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...

  4. 通过案例掌握Spring 管理事务的步骤及配置

    案例描述  通过完成生成订单业务,掌握事务处理.  需要d_order表和d_item表  订单生成时的业务逻辑:向d_order插入1条数据的同时,向t_item中插入若干条数据  这就是一个独立的 ...

  5. oracle 快照(snapshot) 管理

     ----手工创建oracle 快照 BEGIN DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT (); END; / ---删除快照 详细快照信息能够查看视图 ...

  6. java调试工具

    jps当前用户已启动的java进程信息,信息包括进程号和简短的进程command. jstat输出指定 jvm 实例的特定统计量:统计量:-class-compiler-gc-gccapacity-g ...

  7. hive 学习笔记精简

    创建表: drop table t create table if not exists t (t string) partitioned by (log_date string) row forma ...

  8. 红豆带你从零学C#系列之:使用集合组织相关数据

    ArrayList(数组列表) Why:如果一个公司有5名员工,一般我们会用长度为5的对象数组来存储信息,但要是有新员工来了,5个长度的数组就不够用了,因此我们需要一种能够根据需要自动分配容量的动态数 ...

  9. Java随机输出验证码包含数字、字母、汉字

    //随机验证码,有数字.字符 //生成随机数,然后再截取,还要限定随机数的范围 String zimu = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn ...

  10. 搭建Hadoop集群 (三)

    通过 搭建Hadoop集群 (二), 我们已经可以顺利运行自带的wordcount程序. 下面学习如何创建自己的Java应用, 放到Hadoop集群上运行, 并且可以通过debug来调试. 有多少种D ...