forked from ncduy0303/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fenwick Tree (point update, range query).cpp
66 lines (54 loc) · 1.24 KB
/
Fenwick Tree (point update, range query).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
// Problem link: https://cses.fi/problemset/task/1648
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define ll long long
const int MAX_N = 2e5 + 1;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LINF = 1e18;
#define LSOne(S) ((S) & (-S))
int n, q;
ll ft[MAX_N];
void update(int x, int v) {
for (; x <= n; x += LSOne(x))
ft[x] += v;
}
ll sum(int x) {
ll res = 0;
for (; x; x -= LSOne(x))
res += ft[x];
return res;
}
ll rsq(int l, int r) {
return sum(r) - sum(l - 1);
}
void solve() {
cin >> n >> q;
for (int i = 1; i <= n; i++) {
int x; cin >> x;
update(i, x);
}
while (q--) {
int t; cin >> t;
if (t == 1) {
int x, v; cin >> x >> v;
update(x, v - rsq(x, x));
}
else {
int l, r; cin >> l >> r;
cout << rsq(l, r) << "\n";
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int tc; tc = 1;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
}