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. 2014第16周三CSS布局再学习摘录

    今天尝试写了下前端页面,费了不少时间,做出的结果仍然惨不忍睹,感觉很简单的几个页面,在现有框架多个样式混杂下就是感觉很不自在随意,晚上回来又看了些div+css方面的基础知识. 1.CSS的class ...

  2. kvm中运行kvm

    如何在 KVM 虚拟机上运行 KVM 上次讨论了如何在 VMware ESXi 虚拟机上运行 KVM 问题,前不久有读者想 “在 kvm 上面创建个虚拟机安装 rackspace 的 openstac ...

  3. 起启航-华夏互联与杰华网络合体结盟打造本土IT利舰

    北京时间2013年9月9日消息: 领先的软件研发企业上海逐一软件科技有限公司与专业互联网推广运营机构南昌杰华网络开发有限公司达成协议,双方将建立紧密合作关系与集团运营体制,并在未来的10个月内进行相应 ...

  4. python 【第四篇】:面向对象(一)

    1.前言 提笔忘字,感慨良多!python自习前前后后有一年多了吧,貌似花了不少时间,其实没学到啥东西,都是在面向对象编程之前基础知识这块一直打转转,每次到了面向对象这块就感觉很蒙,看两天直接放弃,从 ...

  5. Codeforces Round #277.5 (Div. 2)-D

    题意:求该死的菱形数目.直接枚举两端的点.平均意义每一个点连接20条边,用邻接表暴力计算中间节点数目,那么中间节点任选两个与两端可组成的菱形数目有r*(r-1)/2. 代码: #include< ...

  6. CSS3六边形

    <!DOCTYPE html> <!-- saved from url=(0043)http://dbox.whosemind.net/demo/liufang.html --> ...

  7. apk混淆打包注意事项

    混淆打包搞了好几天才初步了解,其中碰到很多Debug正常,Release的apk不能用,基本都是第三方的jar的问题,所以要排除混淆. 1. Json解析对象出错 用到fastJson或者GJson的 ...

  8. C# 创建文件时,文件夹不存在,如何自动创建文件夹

    c# 创建文件时怎么创建文件夹?strhtml=......StreamWriter sw=new StreamWriter("D:/test/1.aspx",false);sw. ...

  9. sql 练习(3)

    1.打印九九乘法表 ) ,exp)) A, ,exp))B, ,exp))C, ,exp))D, ,exp))E, ,exp))F, ,exp))G, ,exp))H, ,exp))I from ( ...

  10. .Net平台-MVP模式再探(二)

    PS:     本文与  上一遍文章  没有什么必然的联系,可以说是对于MVP的一定的加深,或许在理解上比上一篇多有点难度. 正文   一.简单讲讲MVP是什么玩意儿 如果从层次关系来讲,MVP属于P ...