-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·60 lines (47 loc) · 1.18 KB
/
main.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
#include <bits/stdc++.h>
#define MAX 1000001
using namespace std;
using ll = long long;
ll N, M, a, b, c;
ll arr[MAX], arrIndex[MAX], tree[4 * MAX];
ll init(int start, int end, int n) {
if (start == end) {
arrIndex[start] = n;
return tree[n] = 0;
}
int mid = (start + end) / 2;
return tree[n] = init(start, mid, 2 * n) + init(mid + 1, end, 2 * n + 1);
}
ll sum(int start, int end, int n, int left, int right) {
if (left > end || right < start)
return 0;
if (left <= start && end <= right)
return tree[n];
int mid = (start + end) / 2;
return sum(start, mid, 2 * n, left, right) +
sum(mid + 1, end, 2 * n + 1, left, right);
}
void update(int index, int value) {
int cur = arrIndex[index];
tree[cur] = value;
while (cur != 1) {
tree[cur / 2] = tree[cur] + tree[(cur % 2 == 0) ? (cur + 1) : (cur - 1)];
cur /= 2;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M;
init(0, N - 1, 1);
for (int i = 0; i < M; i++) {
cin >> a >> b >> c;
if (a == 0) {
cout << sum(0, N - 1, 1, (b < c ? b : c) - 1, (b > c ? b : c) - 1)
<< "\n";
} else {
arr[b - 1] = c;
update(b - 1, c);
}
}
}