forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgss3.cpp
89 lines (89 loc) · 2.43 KB
/
gss3.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
83
84
85
86
87
88
89
// 2014-04-26
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int maxisum[200000];
int sum[200000];
int maxlsum[200000];
int maxrsum[200000];
int a[50000];
int N;
void set(int node, int val) {
sum[node] = maxisum[node] = maxlsum[node] = maxrsum[node] = val;
}
void calc(int node) {
sum[node] = sum[2*node] + sum[2*node+1];
maxlsum[node] = max(maxlsum[2*node], sum[2*node] + maxlsum[2*node+1]);
maxrsum[node] = max(maxrsum[2*node+1], sum[2*node+1] + maxrsum[2*node]);
maxisum[node] = max(max(maxisum[2*node], maxisum[2*node+1]),
maxrsum[2*node] + maxlsum[2*node+1]);
}
void build_tree(int node, int l, int r) {
if (r - l == 1) {
set(node, a[l]);
} else {
int m = l + (r - l + 1)/2;
build_tree(2*node, l, m);
build_tree(2*node+1, m, r);
calc(node);
}
}
void update(int node, int l, int r, int idx, int newval) {
if (r - l == 1) {
a[idx] = newval;
set(node, newval);
} else {
int m = l + (r - l + 1)/2;
if (idx < m) {
update(2*node, l, m, idx, newval);
} else {
update(2*node+1, m, r, idx, newval);
}
calc(node);
}
}
void query_rec(int node, int tbegin, int tend, int abegin, int aend,
vector<int>& nodelist) {
if (tbegin >= abegin && tend <= aend) {
nodelist.push_back(node);
} else {
int mid = tbegin + (tend - tbegin + 1)/2;
if (mid > abegin && tbegin < aend) {
query_rec(2*node, tbegin, mid, abegin, aend, nodelist);
}
if (tend > abegin && mid < aend) {
query_rec(2*node+1, mid, tend, abegin, aend, nodelist);
}
}
}
int query(int l, int r) {
vector<int> nodelist;
query_rec(1, 0, N, l, r, nodelist);
int res = -2e9;
int t = -2e9;
for (int i = 0; i < nodelist.size(); i++) {
res = max(res, maxisum[nodelist[i]]);
res = max(res, t + maxlsum[nodelist[i]]);
t = max(t + sum[nodelist[i]], maxrsum[nodelist[i]]);
}
return res;
}
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", a+i);
}
build_tree(1, 0, N);
int M; scanf("%d", &M);
while (M--) {
int op, x, y;
scanf("%d %d %d", &op, &x, &y);
if (op == 0) {
update(1, 0, N, x-1, y);
} else {
printf("%d\n", query(x-1, y));
}
}
}