链接:https://www.nowcoder.com/acm/contest/214/F
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld
题目描述
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 输入数据可能很多,推荐使用快读
好像是个平衡树的板子题
但是平衡树还没写过啊....
刚开始想用multiset实现
但是mutilset的树结构关系 迭代器只重载了自加自减
无法随机查询
洗衣服的时候突然想到LIS的有序表插入
这道题用有序表加vector的各种操作应该可以实现
结果交了一发竟然过了
时间复杂度:
随机查询O(1)
按值查询O(log2n) //lower/upper_bound()
后插O(1) //arr.push_back()
中间插入平均O(n/2) //arr.insert(vector<int>::iterator it, val);
时间复杂度尚可接受
代码↓
//#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <sstream>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 10;
std::vector <int> arr;
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0); cout.tie(0);
//freopen("D://test.in", "r", stdin);
//freopen("D://test.out", "w", stdout);
int n, a, b;
scanf("%d", &n);
vector <int>::iterator it;
while(n--)
{
scanf("%d%d", &a, &b);
if(a == 1)
{
it = lower_bound(arr.begin(), arr.end(), b);
arr.insert(it, b);
}
else if(a == 2)
{
it = lower_bound(arr.begin(), arr.end(), b);
arr.erase(it);
}
else if(a == 3)
{
it = lower_bound(arr.begin(), arr.end(), b);
printf("%d
", it - arr.begin() + 1);
}
else if(a == 4)
{
printf("%d
", arr[b - 1]);
}
else if(a == 5)
{
it = lower_bound(arr.begin(), arr.end(), b);
it--;
printf("%d
", *it);
}
else if(a == 6)
{
it = upper_bound(arr.begin(), arr.end(), b);
printf("%d
", *it);
}
}
return 0;
}