-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexed_set.cpp
91 lines (76 loc) · 1.49 KB
/
indexed_set.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// begin of indexed_set
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset;
// end of indexed_set
// log
struct indexed_set {
int sz = 0, bit[N];
int size() {
return sz;
}
void update(int k, int x) {
while (k < N) {
bit[k] += x;
k += k & -k;
}
sz += x;
}
int find_by_order(int k) {
int ans = 0, sum = 0;
for (int j = 17; j >= 0; --j) {
ans += 1 << j;
if (ans < N && sum + bit[ans] < k) {
sum += bit[ans];
} else {
ans -= 1 << j;
}
}
return ans + 1;
}
int order_of_key(int k) {
int ans = 0;
while (k >= 1) {
ans += bit[k];
k -= k & -k;
}
return ans;
}
} is;
// log ^ 2
const MAX = 1e9;
struct indexed_set {
map<int, int> bit;
indexed_set() {
bit.clear();
}
void update(int k, int x) {
while (k <= MAX) {
bit[k] += x;
k += k & -k;
}
}
int find_by_order(int k) {
int ans = 0, sum = 0;
for (int j = log2(MAX); j >= 0; --j) {
ans += 1 << j;
if (ans < MAX && sum + bit[ans] < k) {
sum += bit[ans];
} else {
ans -= 1 << j;
}
}
return ans + 1;
}
int order_of_key(int k) {
int ans = 0;
while (k >= 1) {
ans += bit[k];
k -= k & -k;
}
return ans;
}
} is;