zoukankan      html  css  js  c++  java
  • P5076 普通二叉树(简化版)

    别的方法我不知道,我知道这题用multiset很简单.

    multiset(以及set)可以执行插入,二分查找,删除等操作,与set的区别在于它不会自动去重.multiset在任意时刻可以保持内部元素的有序性.

    并且提到的这三种操作的复杂度都是O(logN).

    一种常见的做法(不限于此题)是插入两个"哨兵"初始化multiset(及set),使得查找操作无论如何都不会返回不存在的迭代器位置.

    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <set>
    using namespace std;
    
    multiset<int> s;
    
    int main(){
        int q;
        scanf("%d", &q);
        s.insert(2147483647);
        s.insert(-2147483647);
    
        while(q--){
            int x, opr;
            scanf("%d%d", &opr, &x);
    
            if(opr == 1){
                int ans = 0;
                auto pos = s.lower_bound(x);
                for(auto i = s.begin(); i != pos; i++) ans++;
                printf("%d
    ", ans);
            }else if(opr == 2){
                auto pos = s.begin();
                while(x--) pos++;
                // pos--;
                printf("%d
    ", *pos);
            }else if(opr == 3){
                auto pos = s.lower_bound(x);
                pos--;
                printf("%d
    ", *pos);
            }else if(opr == 4){
                 printf("%d
    ", *s.upper_bound(x));
            }else
                s.insert(x);
        }
    
        return 0;
    }
    P5076
  • 相关阅读:
    centos崩溃后如何修复
    乘法是啥
    接上篇—用spring注入DBbean,并使用maven管理
    技术产生价值
    技术?
    世界的本质是啥呢
    java-web 登陆功能
    对java的理解
    数学的历史
    使用jmeter测试接口
  • 原文地址:https://www.cnblogs.com/Gaomez/p/14365439.html
Copyright © 2011-2022 走看看