https://www.oj.swust.edu.cn/problem/show/2268
pb_ds库
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<tr1/unordered_map>
using namespace std;
using namespace std::tr1;
using namespace __gnu_pbds;
typedef long long LL;
tree<LL, null_type, less<LL>, rb_tree_tag, tree_order_statistics_node_update>::iterator iter;
const int offset = 1e8;
char op1[100];
int n;
int main()
{
tree<LL, null_type, less<LL>, rb_tree_tag, tree_order_statistics_node_update>t;
map<LL, int>m;
while(scanf("%s",op1)!=EOF)
{
string op(op1);
if(op == "-1")
break;
if(op == "insert")
{
LL x;
scanf("%lld", &x);
if(m[x] == 0)
t.insert(x), m[x] = 1;
}
else if(op == "delete")
{
LL x;
scanf("%d", &x);
if(m[x] == 1)
t.erase(x), m[x] = 0;
else
puts("Input Error");
}
else if(op == "predecessor")
{
LL x;
scanf("%lld", &x);
if(m[x] == 0)
puts("Input Error");
else
{
iter = t.begin();
if(*iter == x)
printf("%lld is the minimum\n", x);
else
{
iter = t.lower_bound(x);
iter--;
printf("The predecessor of %lld is %lld\n", x, *iter);
}
}
}
else if(op == "successor")
{
LL x;
scanf("%lld", &x);
if(m[x] == 0)
puts("Input Error");
else
{
iter = t.end();
iter--;
if(*iter == x)
printf("%lld is the maximum\n", x);
else
{
iter = t.lower_bound(x);
iter++;
printf("The successor of %lld is %lld\n", x, *iter);
}
}
}
else if(op == "Kth")
{
LL x;
scanf("%lld", &x);
if(x < 1 || x > t.size())
puts("Input Error");
else
printf("The %lld_th element is %lld\n", x, *t.find_by_order(x - 1));
}
else if(op == "rank")
{
LL x;
scanf("%lld", &x);
if(m[x] == 0)
puts("Input Error");
else
printf("The rank of %lld is %lld_th\n", x,t.order_of_key(x) + 1);
}
else if(op == "print")
{
for(iter = t.begin(); iter != t.end(); iter++)
{
printf("%lld,",*iter);
}
puts("end of print");
}
else
{
puts("end of this test");
t.clear();
m.clear();
}
}
return 0;
}