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. java把函数作为参数传递

    public class Tool { public void a()// /方法a { System.out.print("tool.a()..."); } public voi ...

  2. [Leetcode][Python]34: Search for a Range

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...

  3. configure mount nfs

    qemu-img convert -f raw -O qcow2 nix.img ruiynix.qcow2 1,yum createrepo

  4. nodejs的url模块中的resolve()的用法总结

    var url = require('url'); var a = url.resolve('/one/two/three', 'four') , b = url.resolve('http://ex ...

  5. OpenGL绘制简单场景,实现旋转缩放平移和灯光效果

    本项目实现了用OpenGL绘制一个简单场景,包括正方体.球体和网格,实现了物体的旋转.缩放.平移和灯光效果.附有项目完整代码.有具体凝视.适合刚開始学习的人熟悉opengl使用. 开发情况 开发环境V ...

  6. iOS 使用Method Swizzling隐藏Status Bar

    在iOS 6中,隐藏Status Bar很的简单. // iOS 6及曾经,隐藏状态栏 [[UIApplication sharedApplication] setStatusBarHidden:YE ...

  7. GridView隔行样式

    <AlternatingRowStyle BorderColor="#FF99CC" BorderStyle="Solid" />

  8. 【WorkTile赞助】jQuery编程挑战#009:生成两个div元素互相追逐的动画

    HTML页面: <!-- HTML代码片段中请勿添加<body>标签 //--> <div id="container"> <div id ...

  9. GWT工程 —— HostedMode(宿主模式下调试) 所有的运行命令

    Unknown argument: -helpGoogle Web Toolkit 1.7.0HostedMode [-noserver] [-port port-number | "aut ...

  10. UISearchBar--改变内部输入框的背景颜色

    思路是获取UISearchBar的子视图,判断他是否是输入框(注意不要先入为主地认为是UITextField),最后修改背景色.至于UISearchBar的子视图结构,在不同的iOS版本可能会不一样, ...