-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfenwick_tree.cpp
More file actions
36 lines (35 loc) · 999 Bytes
/
fenwick_tree.cpp
File metadata and controls
36 lines (35 loc) · 999 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
// Fenwick Tree (Binary Indexed Tree)
//
// Supports point updates and prefix/range sum queries in logarithmic time using a 1-indexed BIT.
//
// complexity: O(log N) per op, O(N)
struct Bit {
ll n;
v64 bit;
Bit(ll _n = 0) : n(_n), bit(n + 1) {}
Bit(v64& v) : n(sz(v)), bit(n + 1) {
for (ll i = 1; i <= n; i++) {
bit[i] += v[i - 1];
ll j = i + (i & -i);
if (j <= n) bit[j] += bit[i];
}
}
void update(ll i, ll x) { // soma x na posicao i
for (i++; i <= n; i += i & -i) bit[i] += x;
}
ll pref(ll i) { // soma [0, i]
ll ret = 0;
for (i++; i; i -= i & -i) ret += bit[i];
return ret;
}
ll query(ll l, ll r) { // soma [l, r]
return pref(r) - pref(l - 1);
}
ll upper_bound(ll x) {
ll p = 0;
for (ll i = __lg(n); i+1; i--)
if (p + (1<<i) <= n && bit[p + (1<<i)] <= x)
x -= bit[p += (1 << i)];
return p;
}
};