-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1275.cpp
More file actions
52 lines (48 loc) · 1.63 KB
/
1275.cpp
File metadata and controls
52 lines (48 loc) · 1.63 KB
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
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll init(vector<ll> &a, vector<ll> &tree, int node, int start, int end) {
if (start == end)
return tree[node] = a[start];
else
return tree[node] =
init(a, tree, node * 2, start, (start + end) / 2) +
init(a, tree, node * 2 + 1, (start + end) / 2 + 1, end);
}
ll sum(vector<ll> &tree, int node, int start, int end, int left, int right) {
if (left > end || right < start) return 0;
if (left <= start && end <= right) return tree[node];
return sum(tree, node * 2, start, (start + end) / 2, left, right) +
sum(tree, node * 2 + 1, (start + end) / 2 + 1, end, left, right);
}
void update(vector<ll> &tree, int node, int start, int end, int index,
ll diff) {
if (index < start || index > end) return;
tree[node] = tree[node] + diff;
if (start != end) {
update(tree, node * 2, start, (start + end) / 2, index, diff);
update(tree, node * 2 + 1, (start + end) / 2 + 1, end, index, diff);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<ll> v(n);
int h = (int)ceil(log2(n));
int tree_size = (1 << (h + 1));
vector<ll> tree(tree_size);
for (int i = 0; i < n; i++) cin >> v[i];
init(v, tree, 1, 0, n - 1);
while (m--) {
int x, y, a, b;
cin >> x >> y >> a >> b;
if (x > y) swap(x, y);
cout << sum(tree, 1, 0, n - 1, x - 1, y - 1) << "\n";
ll diff = b - v[a - 1];
v[a - 1] = b;
update(tree, 1, 0, n - 1, a - 1, diff);
}
return 0;
}