-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFenwick.cpp
More file actions
40 lines (38 loc) · 818 Bytes
/
Fenwick.cpp
File metadata and controls
40 lines (38 loc) · 818 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
39
40
using namespace std;
typedef long long ll;
struct fenwick {
int sz; vector<ll> v;
fenwick(int _sz) : sz(_sz + 1), v(_sz + 1) {}
void update(int pos, ll val) {
for (int i = pos; i < sz; i += (i&-i))
v[i] += val;
}
ll query(int pos) {
ll s = 0;
for (int i = pos; i > 0; i -= (i&-i))
s += v[i];
return s;
}
ll query(int le, int ri) {
if (!le) return query(ri);
else return query(ri) - query(le - 1);
}
};
struct fenwick { //±¸°£¾÷µ« Á¡Äõ¸®
int sz; vector<ll> v;
fenwick(int _sz) : sz(_sz + 2), v(_sz + 2) {}
void update(int pos, ll val) {
for (int i = pos; i < sz; i += (i&-i))
v[i] += val;
}
void update(int le, int ri, ll val) {
update(le, val);
update(ri + 1, -val);
}
ll query(int pos) {
ll s = 0;
for (int i = pos; i > 0; i -= (i&-i))
s += v[i];
return s;
}
};