-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathfenwick_tree_multiset.cpp
149 lines (132 loc) · 3.59 KB
/
fenwick_tree_multiset.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <bits/stdc++.h>
using namespace std;
// The maximum limit of the multiset.
// Must be power of two.
const int N = (1 << 17);
/**
* Multiset that store positive integers in the range of [1, N].
* The multiset is implemented using Fenwick tree.
*
* Note that the multiset is is 1-indexed.
*
* The most complex function in this class is done in time complexity of O(log(N)).
*/
class fenwick_multiset {
int cnt, BIT[N + 1];
void update(int idx, int val) {
while (idx <= N) {
BIT[idx] += val;
idx += idx & -idx;
}
}
int get(int idx) {
int res = 0;
while (idx > 0) {
res += BIT[idx];
idx -= idx & -idx;
}
return res;
}
public:
/**
* Constructs a new Fenwick multiset.
*/
fenwick_multiset() {
clear();
}
/**
* Clears and removes all the elements from the multiset.
*/
void clear() {
cnt = 0;
memset(BIT, 0, sizeof(BIT));
}
/**
* Returns the total number of integers stored in the multiset.
*
* @return the size of the multiset.
*/
int size() {
return cnt;
}
/**
* Counts the number of occurrence of an integer in the multiset.
*
* @param val the integer to count its occurrences.
*
* @return how many times the given integer is stored in the multiset.
*/
int count(int val) {
if (val < 1 || val > N) {
return 0;
}
return get(val) - get(val - 1);
}
/**
* Inserts a new integer to the multiset.
* The value of the inserted integer should be in the range of [1, N].
*
* @param val the integer to insert.
*/
void insert(int val) {
update(val, 1);
cnt++;
}
/**
* Removes one occurrence of an integer from the multiset if exists.
*
* @param val the integer to remove.
*/
void remove(int val) {
if (count(val) > 0) {
update(val, -1);
cnt--;
}
}
/**
* Returns an integer from the multiset by its index.
* Note that the multiset is kept sorted in non-descending order.
* The index should be in the range of [1, {@link fenwick_multiset#size()}].
*
* @param idx the index of the integer to return.
*
* @return the idx-th smallest integer in the multiset.
*/
int operator[](int idx) {
int val = 0;
for (int len = (N >> 1); len; len >>= 1)
if (idx > BIT[val + len])
idx -= BIT[val += len];
return val + 1;
}
/**
* Returns the index of the first integer with value greater
* than or equals to the given value.
* Note that the multiset is kept sorted in non-descending order.
*
* @param val the value to returns its lower bound index.
*
* @return the specified index; or {@code fenwick_multiset#size() + 1}
* if such integer does not exist.
*/
int lower_bound(int val) {
if (val <= 0)
return 1;
if (val >= N)
return cnt + 1;
return get(val - 1) + 1;
}
/**
* Returns the index of the first integer with value greater
* than the given value.
* Note that the multiset is kept sorted in non-descending order.
*
* @param val the value to returns its upper bound index.
*
* @return the specified index; or {@code fenwick_multiset#size() + 1}
* if such integer does not exist.
*/
int upper_bound(int val) {
return lower_bound(val + 1);
}
};