SP1716 GSS3 - Can you answer these queries III

SP1716 GSS3 - Can you answer these queries

也真是服了,浪费几分钟来搞这种题目。

直接线段树维护一下端点信息即可,具体来说就是左右端点的权值最大值,答案还有区间权值和。

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
90
91
92
93
94
#include <bits/stdc++.h>
using namespace std;

//#define Fread
//#define Getmod

#ifdef Fread
char buf[1 << 21], *iS, *iT;
#define gc() (iS == iT ? (iT = (iS = buf) + fread (buf, 1, 1 << 21, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
#define getchar gc
#endif // Fread

template <typename T>
void r1(T &x) {
x = 0;
char c(getchar());
int f(1);
for(; c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
for(; '0' <= c && c <= '9';c = getchar()) x = (x * 10) + (c ^ 48);
x *= f;
}

template <typename T,typename... Args> inline void r1(T& t, Args&... args) {
r1(t); r1(args...);
}

//#define int long long
const int maxn = 2e5 + 5;
const int maxm = maxn << 1;

int n, m;
int a[maxn];

struct Node {
int mxl, mxr, val, sum;
}t[maxn << 2];

Node merge(Node u, Node v) {
Node res;
res.mxl = max(u.mxl, u.sum + v.mxl);
res.mxr = max(v.mxr, v.sum + u.mxr);
res.sum = u.sum + v.sum;
res.val = max(max(u.val, v.val), u.mxr + v.mxl);
return res;
}

struct Seg {
#define ls (p << 1)
#define rs (p << 1 | 1)
#define mid ((l + r) >> 1)
void build(int p,int l,int r) {
if(l == r) return t[p].mxl = t[p].mxr = t[p].val = t[p].sum = a[l], void();
build(ls, l, mid), build(rs, mid + 1, r);
t[p] = merge(t[ls], t[rs]);
}

void change(int p,int l,int r,int pos) {
if(l == r) return t[p].mxl = t[p].mxr = t[p].val = t[p].sum = a[l], void();
if(pos <= mid) change(ls, l, mid, pos);
else change(rs, mid + 1, r, pos);
t[p] = merge(t[ls], t[rs]);
}

Node Ask(int p,int l,int r,int ll,int rr) {
if(ll <= l && r <= rr) return t[p];
if(ll <= mid && rr <= mid) return Ask(ls, l, mid, ll, rr);
else if(mid < rr && mid < ll) return Ask(rs, mid + 1, r, ll, rr);
else return merge(Ask(ls, l, mid, ll, rr), Ask(rs, mid + 1, r, ll, rr));
}

}T;

signed main() {
// freopen("S.in", "r", stdin);
// freopen("S.out", "w", stdout);
int i, j;
r1(n);
for(i = 1; i <= n; ++ i) r1(a[i]);
T.build(1, 1, n);
r1(m);
for(i = 1; i <= m; ++ i) {
int opt, l, r;
r1(opt, l, r);
if(opt == 0) {
a[l] = r;
T.change(1, 1, n, l);
}
else {
printf("%d\n", T.Ask(1, 1, n, l, r).val);
}
}
return 0;
}