-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfen_sum.cpp
More file actions
38 lines (37 loc) · 820 Bytes
/
fen_sum.cpp
File metadata and controls
38 lines (37 loc) · 820 Bytes
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
// add + sum range, 1 - indexed
template <class T>
struct FT {
vector <T> ft;
FT (int _n = 0) {
ft.assign(_n + 5, 0);
}
void upd (int id, T val) {
for (; id < (int)ft.size(); id += id & -id) ft[id] += val;
}
T get (int id) {
T ans = 0;
for (; id > 0; id -= id & -id) ans += ft[id];
return ans;
}
};
template <class T>
struct FT2 {
FT <T> ft1, ft2;
int n;
FT2 (int _n = 0) {
ft1 = FT <T> (_n + 5); ft2 = FT <T> (_n + 5);
n = _n;
}
void upd (int l, int r, T val) {
ft1.upd(l, (n - l + 1) * val);
ft1.upd(r + 1, -(n - r) * val);
ft2.upd(l, val);
ft2.upd(r + 1, -val);
}
T pre (int id) {
return ft1.get(id) - ft2.get(id) * (n - id);
}
T sum (int low, int high) {
return pre(high) - pre(low - 1);
}
};