2268: SB_cyh and his BST two pb_ds

 

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值