1 条题解
-
1
01trie:
# include <bits/stdc++.h> using namespace std; namespace trees { template <const int N> class trie { private : struct node { int ch[2]; int val; }tree[300012]; int id = 2; int add () { tree[id].ch[0] = 0; tree[id].ch[1] = 0; tree[id].val = 0; return id ++; } public : void insert (int x) { int u = 1; for (int i = 25; i >= 0; i --) { short v = (x >> i) & 1; if (!tree[u].ch[v]) tree[u].ch[v] = add (); u = tree[u].ch[v]; tree[u].val += 1; } } void del (int x) { int u = 1; for (int i = 25; i >= 0; i --) { short v = (x >> i) & 1; if (!u) tree[u].ch[v] = add (); u = tree[u].ch[v]; if (tree[u].val) { tree[u].val -= 1; } } } int rank (int x) { int ret = 0, u = 1; for (int i = 25; i >= 0; i --) { short v = (x >> i) & 1; if (v) { ret += tree[tree[u].ch[0]].val; u = tree[u].ch[1]; } else { u = tree[u].ch[0]; } if (!u) break; } return ret; } int find_by_order (int x) { int ret = 0, u = 1; for (int i = 25; i >= 0; i --) { // if (!x) break; // cout << u << ' ' << tree[u].val << endl; if (tree[tree[u].ch[0]].val >= x) { u = tree[u].ch[0]; } else { ret |= (1 << i); x -= tree[tree[u].ch[0]].val; u = tree[u].ch[1]; } } return ret; } int lower_bound (int x) { return find_by_order (rank (x)); } int upper_bound (int x) { return find_by_order (rank (x + 1) + 1); } }; } using namespace trees; int n; trie <100012> tree; const int num = 1e7; int main () { ios :: sync_with_stdio (0); cin.tie (0), cout.tie (0); cin >> n; do { int op, x; cin >> op >> x; switch (op) { case 1 : { tree.insert (x + num); // cout << "num : 1" << endl; break; } case 2 : { tree.del (x + num); // cout << "num : 2" << endl; break; } case 3 : { cout << tree.rank (x + num) + 1 << endl; // cout << "num : 3" << endl; break; } case 4 : { cout << tree.find_by_order (x) - num << endl; // cout << "num : 4" << endl; break; } case 5 : { cout << tree.lower_bound (x + num) - num << endl; // cout << "num : 5" << endl; break; } case 6 : { cout << tree.upper_bound (x + num) - num << endl; // cout << "num : 6" << endl; break; } } } while (-- n); }
信息
- ID
- 15132
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 9
- 标签
- 递交数
- 7
- 已通过
- 5
- 上传者