forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkgss.cpp
74 lines (74 loc) · 1.96 KB
/
kgss.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
// 2014-05-01
#include <cstdio>
#include <utility>
#include <algorithm>
using namespace std;
pair<int, int> segtree[400000];
int a[200000];
void set(int node, int val) {
segtree[node].first = val;
segtree[node].second = -1e9;
}
pair<int, int> combine(pair<int, int> l, pair<int, int> r) {
return make_pair(
max(l.first, r.first),
max(max(l.second, r.second), l.first + r.first)
);
}
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);
segtree[node] = combine(segtree[2*node], segtree[2*node+1]);
}
}
void update(int node, int l, int r, int pos, int val) {
if (r == l+1) {
set(node, val);
} else {
int m = l + (r - l + 1)/2;
if (pos < m) {
update(2*node, l, m, pos, val);
} else {
update(2*node+1, m, r, pos, val);
}
segtree[node] = combine(segtree[2*node], segtree[2*node+1]);
}
}
pair<int, int> query(int node, int tl, int tr, int al, int ar) {
if (tl >= al && tr <= ar) {
return segtree[node];
} else {
int m = tl + (tr - tl + 1)/2;
if (m > al && tl < ar) {
if (tr > al && m < ar) {
return combine(query(2*node, tl, m, al, ar),
query(2*node+1, m, tr, al, ar));
} else {
return query(2*node, tl, m, al, ar);
}
} else {
return query(2*node+1, m, tr, al, ar);
}
}
}
int main() {
int N; scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", a+i);
}
build_tree(1, 0, N);
int Q; scanf("%d\n", &Q);
while (Q--) {
char c; int x, y;
scanf("%c %d %d\n", &c, &x, &y);
if (c == 'U') {
update(1, 0, N, x-1, y);
} else {
printf("%d\n", query(1, 0, N, x-1, y).second);
}
}
}