zoukankan      html  css  js  c++  java
  • 洛谷P3369 【模板】普通平衡树

    题目描述

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

    1. 插入x数
    2. 删除x数(若有多个相同的数,因只删除一个)
    3. 查询x数的排名(排名定义为比当前数小的数的个数+1。若有多个相同的数,因输出最小的排名)
    4. 查询排名为x的数
    5. x的前驱(前驱定义为小于x,且最大的数)
    6. x的后继(后继定义为大于x,且最小的数)

    输入输出格式

    输入格式:

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

    输出格式:

    对于操作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的数据范围: n100000

    2.每个数的数据范围: [-10^7, 10^7]

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

    在此鸣谢

     1 #include<cstdio>
     2 #include<vector>
     3 #include<algorithm>
     4 using namespace std;
     5 long long read()
     6 {
     7     long long x=0,f=1;
     8     char ch=getchar();
     9     while(ch>'9'||ch<'0')
    10     {
    11         if(ch=='-')
    12             f=-1;
    13         ch=getchar();
    14     }
    15     while(ch>='0'&&ch<='9')
    16     {
    17         x=x*10+ch-'0';
    18         ch=getchar();
    19     }
    20     return x*f;
    21 }
    22 vector<int>v;
    23 int n,opt,x;
    24 int main()
    25 {
    26     v.reserve(100001);
    27     n=read();
    28     while(n--)
    29     {
    30         opt=read(),x=read();
    31         if(opt==1)
    32             v.insert(lower_bound(v.begin(),v.end(),x),x);
    33         if(opt==2)
    34             v.erase (lower_bound(v.begin(),v.end(),x));
    35         if(opt==3)
    36             printf("%d\n",lower_bound(v.begin(),v.end(),x)-v.begin()+1);
    37         if(opt==4)
    38             printf("%d\n",v[x-1]);
    39         if(opt==5)
    40             printf("%d\n",v[lower_bound(v.begin(),v.end(),x)-v.begin()-1]);
    41         if(opt==6)
    42             printf("%d\n",v[upper_bound(v.begin(),v.end(),x)-v.begin()]);
    43     }
    44     return 0;
    45 }
    View Code
  • 相关阅读:
    面试
    二叉树- 二叉树直径
    排序算法
    JAVA编程
    JAVA编程
    JAVA中break和continue的区别
    HTTP的序列化和反序列化
    PL/SQL基础
    G. Game Design
    hdu 6703 array
  • 原文地址:https://www.cnblogs.com/liweilin/p/10187882.html
Copyright © 2011-2022 走看看