zoukankan      html  css  js  c++  java
  • 用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)

  • 相关阅读:
    菜鸟浅谈软件开发项目管理
    中国准货币体系的概要简析
    使用dockercompose安装wordpress
    货币乘数
    安全测试的相关内容
    TCP三次握手和四次挥手
    HTTP协议相关
    描述浏览器登录的过程
    AJAX相关知识
    什么是热钱
  • 原文地址:https://www.cnblogs.com/szdytom/p/stl-as-tree.html
Copyright © 2011-2022 走看看