-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfen_minmax.cpp
More file actions
49 lines (42 loc) · 1.01 KB
/
fen_minmax.cpp
File metadata and controls
49 lines (42 loc) · 1.01 KB
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
// Just use to get prefix OR suffix (...), please don't combine them
struct BIT {
typedef pair <int, int> T;
T func (T a, T b) {
return max(a, b);
}
vector <T> fen;
int n = 0;
T unit;
BIT (int _n = 0, T _unit = {0, 0}) {
n = _n;
unit = _unit;
fen.assign(n + 5, unit);
}
void update_pref (int idx, T val) {
assert (idx >= 1 && idx <= n);
for (; idx <= n; idx += idx & -idx) {
fen[idx] = func(fen[idx], val);
}
}
T get_pref (int idx) {
assert (idx >= 1 && idx <= n);
T ans = unit;
for (; idx > 0; idx -= idx & -idx) {
ans = func(ans, fen[idx]);
}
return ans;
}
void update_suff (int idx, T val) {
assert (idx >= 1 && idx <= n);
for (; idx > 0; idx -= idx & -idx)
fen[idx] = func(fen[idx], val);
}
T get_suff (int idx) {
assert (idx >= 1 && idx <= n);
T ans = unit;
for (; idx <= n; idx += idx & -idx) {
ans = func(ans, fen[idx]);
}
return ans;
}
};