pb_ds 求助查找前驱!
查看原帖
pb_ds 求助查找前驱!
488827
Super_Supper楼主2022/5/8 22:52
#include <bits/extc++.h>

#include <iostream>
#define tree(int) tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

using namespace __gnu_pbds;
using namespace std;

int t;

tree(int) rbtree;

int main() {
  cin >> t;
  for (int op, x; t; t--) {
    cin >> op >> x;
    if (op == 1) {
      rbtree.insert(x);
    } else if (op == 2) {
      rbtree.erase(x);
    } else if (op == 3) {
      cout << rbtree.order_of_key(x) + 1 << '\n';
    } else if (op == 4) {
      cout << *rbtree.find_by_order(x - 1) << '\n';
    } else if (op == 5) {
      tree(int)::iterator t = rbtree.lower_bound(x), it = rbtree.lower_bound(x);
      for (; *it == *t; it--) {
      }
      cout << *it << '\n';
    } else {
      cout << *rbtree.upper_bound(x) << '\n';
    }
  }
  return 0;
}

经过我多次尝试,应该是前驱寄了,求助!

2022/5/8 22:52
加载中...