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

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

时间: 2024-08-25 08:50:17

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

bzoj 3224: Tyvj 1728 普通平衡树.

3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 15114  Solved: 6570[Submit][Status][Discuss] Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数,因输出最小的排名)4. 查询排名为x的数5. 求x的前驱(前驱定义为

BZOJ 3224: Tyvj 1728 普通平衡树 treap

3224: Tyvj 1728 普通平衡树 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数,因输出最小的排名)4. 查询排名为x的数5. 求x的前驱(前驱定义为小于x,且最大的数)6. 求x的后继(后继定义为大于x,且最小的数) Input 第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号(1<=opt<=6)

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

题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 思路: splay树模板题: 推荐博客:https://blog.csdn.net/clove_unique/article/details/50630280 b站上splay树的讲解视频也可以看下,讲的很好,推荐看完视频了解了splay的原理再写 实现代码: #include<bits/stdc++.h> using namespace std; #define ll lo

BZOJ 3224 Tyvj 1728 普通平衡树 | Splay 板子

下面给出Splay的实现方法(复杂度证明什么的知道是 nlogn 就可以啦) 首先对于一颗可爱的二叉查找树,是不能保证最坏nlogn的复杂度(可以想象把一个升序序列插入) 所以我们需要一些非常巧妙的旋转操作 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<cmath> 5 #define N 100010 6 #define which(x) (ls[fa[(x)

[bzoj] 3224 Tyvj 1728 普通平衡树 || 平衡树板子题

#!/bin/bash g++ make.cpp -o make -Wall g++ 1.cpp -o ac -Wall g++ sb.cpp -o sb -Wall while true; do ./make > 1.in ./ac < 1.in > 1.out ./sb < 1.in > sb.out if diff 1.out sb.out; then printf "AC!" else exit fi done 原文地址:https://www.c

BZOJ 题目3224: Tyvj 1728 普通平衡树(SBT)

3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 4350  Solved: 1769 [Submit][Status][Discuss] Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 1. 插入x数 2. 删除x数(若有多个相同的数,因只删除一个) 3. 查询x数的排名(若有多个相同的数,因输出最小的排名) 4. 查询排名为x的数 5. 求x的前驱

3224: Tyvj 1728 普通平衡树

3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2566  Solved: 1031[Submit][Status] Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数,因输出最小的排名)4. 查询排名为x的数5. 求x的前驱(前驱定义为小于x,且最大的数)

3224: Tyvj 1728 普通平衡树(新板子)

3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 17048  Solved: 7429[Submit][Status][Discuss] Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数,因输出最小的排名)4. 查询排名为x的数5. 求x的前驱(前驱定义为

HYSBZ 3224 Tyvj 1728 普通平衡树 splay模版

先学了splay写的 以后有空再学treap和sbt版 参考: http://blog.csdn.net/clove_unique/article/details/50630280 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<vector>