洛谷 P3369 BZOJ 3224 【模板】普通平衡树(Treap/SBT)

题目描述

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:

  1. 插入x数

  2. 删除x数(若有多个相同的数,因只删除一个)

  3. 查询x数的排名(若有多个相同的数,因输出最小的排名)

  4. 查询排名为x的数

  5. 求x的前驱(前驱定义为小于x,且最大的数)

  6. 求x的后继(后继定义为大于x,且最小的数)

输入输出格式

输入格式:

 

第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号(1<=opt<=6)

 

输出格式:

 

对于操作3,4,5,6每行输出一个数,表示对应答案

 

输入输出样例

输入样例#1:
10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
输出样例#1:
106465
84185
492737

说明

时空限制:1000ms,128M

1.n的数据范围:n<=100000

2.每个数的数据范围:[-1e7,1e7]

来源:Tyvj1728 原名:普通平衡树

在此鸣谢

解题思路

  1、直接上平衡树的板子,手敲也好,pb_ds也罢,这里先留坑……

  2、黄学长博客上看到的二分查找,好神奇……Orz

源代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<set>
#include<vector>
#include<algorithm>
#define inf 1000000000
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n;
vector<int> a;
void insert(int x)
{
    a.insert(upper_bound(a.begin(),a.end(),x),x);
    return;
}
void del(int x)
{
    a.erase(lower_bound(a.begin(),a.end(),x));
    return;
}
int find(int x)
{
    return lower_bound(a.begin(),a.end(),x)-a.begin()+1;
}
int main()
{
    n=read();
    a.reserve(200000);
    int f,x;
    for(int i=1;i<=n;i++)
    {
        f=read();x=read();
        switch(f)
        {
        case 1:insert(x);break;
        case 2:del(x);break;
        case 3:printf("%d\n",find(x));break;
        case 4:printf("%d\n",a[x-1]);break;
        case 5:printf("%d\n",*--lower_bound(a.begin(),a.end(),x));break;
        case 6:printf("%d\n",*upper_bound(a.begin(),a.end(),x));break;
        }
    }
    return 0;
}

 

posted @ 2017-06-30 22:05  wawcac  阅读(241)  评论(0编辑  收藏  举报