vector的骚操作

链接:https://www.nowcoder.com/acm/contest/214/F
来源:牛客网

题目描述

clccle是个蒟蒻,她经常会在学校机房里刷题,也会被同校的dalao们虐,有一次,她想出了一个毒瘤数据结构,便兴冲冲的把题面打了出来,她觉得自己能5s内切掉就很棒了,结果evildoer过来一看,说:"这思博题不是1s就能切掉嘛",clccle觉得自己的信心得到了打击,你能帮她在1s中切掉这道水题嘛?

你需要写一个毒瘤(划掉)简单的数据结构,满足以下操作
1.插入一个数x(insert)
2.删除一个数x(delete)(如果有多个相同的数,则只删除一个)
3.查询一个数x的排名(若有多个相同的数,就输出最小的排名)
4.查询排名为x的数
5.求一个数x的前驱
6.求一个数x的后继

输入描述:

第一行,输入一个整数n,表示接下来需要输入n行

接下来n行,输入 一个整数num和一个整数x

输出描述:

当num为3,4,5,6时,输出对应的答案

示例1

输入

复制

8
1 10
1 20
1 30
3 20
4 2
2 10
5 25
6 -1

输出

复制

2
20
20
20

说明

大家自己手玩样例算了QWQ

备注:

对于全部数据n<=1e5,且3,4,5,6的操作数少于60000

输入数据可能很多,推荐使用快读
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=tree[rt<<1]+tree[rt<<1|1]
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int M=1e3+5;
const int N=1e5+5;

int main(){
    int n,id,x;
    scanf("%d",&n);
    vector<int>vv;
    for(int i=0;i<n;i++){
        id=read();  x=read();
        if(id==1){
           vector<int>::iterator it=lower_bound(vv.begin(),vv.end(),x);
           //vv.push_back(x);
           vv.insert(it,x);
           //sort(vv.begin(),vv.end());
        }
        else if(id==2){
            vector<int>::iterator it=lower_bound(vv.begin(),vv.end(),x);
            vv.erase(it);
        }
        else if(id==3){
            printf("%d\n",lower_bound(vv.begin(),vv.end(),x)-vv.begin()+1);
        }
        else if(id==4){
            printf("%d\n",vv[x-1]);
        }
        else if(id==5){
            int h=lower_bound(vv.begin(),vv.end(),x)-vv.begin();
            printf("%d\n",vv[h-1]);
        }
        else if(id==6){
            int h=upper_bound(vv.begin(),vv.end(),x)-vv.begin();
            printf("%d\n",vv[h]);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/black_horse2018/article/details/83240453
今日推荐