-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1190.cpp
57 lines (51 loc) · 1.3 KB
/
1190.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
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
struct Node {
long long left, right, sum, val;
};
int n, m, x[N];
Node ST[N << 2];
void merge(int id) {
ST[id].left = max(ST[id << 1].left, ST[id << 1].sum + ST[id << 1 | 1].left);
ST[id].right = max(ST[id << 1 | 1].right, ST[id << 1 | 1].sum + ST[id << 1].right);
ST[id].sum = ST[id << 1].sum + ST[id << 1 | 1].sum;
ST[id].val = max({ST[id << 1].val, ST[id << 1 | 1].val, ST[id << 1].right + ST[id << 1 | 1].left});
}
void build(int id, int l, int r) {
if (l == r) {
ST[id].val = ST[id].left = ST[id].right = max(0, x[l]);
ST[id].sum = x[l];
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
merge(id);
}
void update(int id, int l, int r, int u) {
if (l == r) {
ST[id].val = ST[id].left = ST[id].right = max(0, x[l]);
ST[id].sum = x[l];
return;
}
int mid = (l + r) >> 1;
if (u <= mid) update(id << 1, l, mid, u);
else update(id << 1 | 1, mid + 1, r, u);
merge(id);
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> x[i];
}
build(1, 1, n);
while (m--) {
int k, a; cin >> k >> a;
x[k] = a;
update(1, 1, n, k);
cout << ST[1].val << "\n";
}
return 0;
}