• 用STL水平衡树的题


    vector

    预备动作

    #include <vector>
    #include <algorithm>
    using namespace std;
    
    vector<int> tree;
    

    插入数 (x)

    tree.insert(lower_bound(tree.begin(), tree.end(), x), x);
    

    删除数 (x) (若有多个相同的数,只删除一个)

    tree.erase(lower_bound(tree.begin(), tree.end(), x));
    

    查询 (x) 数的排名

    res = lower_bound(tree.begin(), tree.end(), x) - tree.begin() + 1;
    

    查询排名为 (x) 的数

    res = tree[x - 1];
    

    (x) 的前驱

    res = *--lower_bound(tree.begin(), tree.end(), x);
    

    (x) 的后继

    res = *upper_bound(tree.begin(), tree.end(), x);
    

    模板题完整代码

    P3369 普通平衡树

    #include <cstdio>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main() {
        int n;
        scanf("%d", &n);
    
        vector<int> tree;
        while (n--) {
            int op, x;
            scanf("%d %d", &op, &x);
    
            if (op == 1) tree.insert(lower_bound(tree.begin(), tree.end(), x), x);
            else if (op == 2) tree.erase(lower_bound(tree.begin(), tree.end(), x));
            else if (op == 3) printf("%d
    ", lower_bound(tree.begin(), tree.end(), x) - tree.begin() + 1);
            else if (op == 4) printf("%d
    ", tree[x - 1]);
            else if (op == 5) printf("%d
    ", *--lower_bound(tree.begin(), tree.end(), x));
            else printf("%d
    ", *upper_bound(tree.begin(), tree.end(), x));  
        }
        return 0;
    }
    

    洛谷测评(C++17 O2)总用时305ms,最大点80ms。

    multiset

    (To be continued)

  • 相关阅读:
    jQuery 选择器 与 事件
    JavaScript 语法
    Java 包(package)
    Java 接口
    java封装继承多态---继承篇
    Java数组的学习
    java基础周总结
    JAVA基础接口集合框架
    周总结
    Java基础
  • 原文地址:https://www.cnblogs.com/szdytom/p/stl-as-tree.html
Copyright © 2020-2023  润新知