-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSomme sur des intervalles d'un tableau dynamique.cpp
82 lines (72 loc) · 1.97 KB
/
Somme sur des intervalles d'un tableau dynamique.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "bits/stdc++.h"
using namespace std;
int n, r;
vector<int> arr, seg;
void init(int index = 1, int l = 0, int r = n - 1) {
// cout << "init " << l << " " << r << "\n";
if (l == r) {
seg[index] = arr[l];
} else {
int m = (l + r) / 2;
init(2 * index, l, m);
init(2 * index + 1, m + 1, r);
seg[index] = seg[2 * index] + seg[2 * index + 1];
}
}
void update(int key, int index = 1, int l = 0, int r = n - 1) {
// cout << "update " << l << " " << r << " " << a[key] << "\n";
if (key < l || key > r) return;
if (l == r) {
seg[index] = arr[key];
} else {
int m = (l + r) / 2;
if (key <= m)
update(key, 2 * index, l, m);
else
update(key, 2 * index + 1, m + 1, r);
seg[index] = seg[2 * index] + seg[2 * index + 1];
}
}
int get(pair<int, int> range, int index = 1, int l = 0, int r = n - 1) {
// cout << "get " << l << " " << r << " " << range.first << " " <<
// range.second << "\n";
if (range.first > range.second) return 0;
if (range.first == l && range.second == r) {
return seg[index];
} else {
int m = (l + r) / 2;
int leftsum =
get(make_pair(range.first, min(range.second, m)), 2 * index, l, m);
int rightsum = get(make_pair(max(m + 1, range.first), range.second),
2 * index + 1, m + 1, r);
return leftsum + rightsum;
}
}
void print() {
for (int i = 1; i < 4 * n; i++) cout << seg[i] << " ";
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
seg.resize(4 * n);
arr.resize(n);
for (auto &i : arr) cin >> i;
init();
cin >> r;
for (int i = 0; i < r; i++) {
char type;
cin >> type;
if (type == 'M') {
int key, value;
cin >> key >> value;
arr[key] = value;
update(key);
} else {
int l, r;
cin >> l >> r;
cout << get(make_pair(l, r)) << "\n";
}
}
}