[Solution]
It seems like a segment tree. However, it queries at most 20 sub-segments, which is the most difficult part. We consider a chosen sub-segment a[l] ~ a[r], the only way to increase the answer by increasing K is to cut it into three parts and abandon the middle part. That means the answer minus part of the previous one. How to do it in a more convenient way? We set all a[i] between l and r as -a[i] and query for the max sum of the sub-segments again. Then after K operations like this, the sum is the answer to the query. After that, you should recover all the sub-segments, or you'll get WA.
[Code]
The reading part is kind of sick. 0 stands for changing operation and 1 stands for queries.
#include <cstdio>
#include <algorithm>
#include <memory.h>
#include <ctype.h>
using namespace std;
struct dat {
int sl, pl, sr, pr, sm, p1, p2, ct, mx;
void fill(int x, int p) {
sm = x;
ct = 1;
sl = x;
pl = p;
sr = x;
pr = p;
p1 = p;
p2 = p;
mx = max(x, 0);
}
};
struct seg {
int l, r, zd, nd;
dat x[2];
seg *ls, *rs;
};
int nextInt() {
int d, s = 0;
bool flag = 0;
do {
d = getchar();
if (d == '-')
flag = 1;
} while (!isdigit(d));
do
s = s * 10 + d - 48, d = getchar();
while (isdigit(d));
return flag ? -s : s;
}
const int maxn = 100009;
const int maxk = 22;
int n, m, a[maxn];
seg *sp, *rt;
#define mid(p) ((p->l+p->r)>>1)
inline dat mkDat(const dat& a, const dat& b) {
dat s;
s. sm = a. sm + b. sm;
s. ct = a. ct + b. ct;
if (a. sl > a. sm + b. sl) {
s. sl = a. sl;
s. pl = a. pl;
}
else {
s. sl = a. sm + b. sl;
s. pl = b. pl;
}
if (b. sr > b. sm + a. sr) {
s. sr = b. sr;
s. pr = b. pr;
}
else {
s. sr = b. sm + a. sr;
s. pr = a. pr;
}
if (a. sr + b. sl > a. mx && a. sr + b. sl > b. mx) {
s. mx = a. sr + b. sl;
s. p1 = a. pr;
s. p2 = b. pl;
}
else if (a. mx > b. mx) {
s. mx = a. mx;
s. p1 = a. p1;
s. p2 = a. p2;
}
else {
s. mx = b. mx;
s. p1 = b. p1;
s. p2 = b. p2;
}
return s;
}
#define update(p) {\
if (p-> l + 1 < p-> r) {\
p-> x[p-> nd] = mkDat(p-> ls-> x[p-> ls-> nd], p-> rs-> x[p-> rs-> nd]);\
p-> x[p-> nd ^ 1] = mkDat(p-> ls-> x[p-> ls-> nd ^ 1], p-> rs-> x[p-> rs-> nd ^ 1]);\
}\
}
#define pushDown(p) {\
if (p-> zd) {\
if (p-> l + 1 < p-> r) {\
p-> ls-> zd ^= 1;\
p-> ls-> nd ^= 1;\
p-> rs-> zd ^= 1;\
p-> rs-> nd ^= 1;\
}\
p-> zd = 0;\
}\
}
seg *sgtMake(int l, int r) {
seg* p = sp ++;
p-> l = l;
p-> r = r;
p-> zd = 0;
p-> nd = 0;
if (l + 1 == r) {
p-> x[0]. fill(a[l], l);
p-> x[1]. fill(-a[l], l);
}
else {
p-> ls = sgtMake(l, mid(p));
p-> rs = sgtMake(mid(p), r);
update(p);
}
return p;
}
void sgtChg(seg *p, int p0, int v0) {
if (p-> l + 1 == p-> r) {
p-> nd = 0;
p-> x[0]. fill(v0, p0);
p-> x[1]. fill(- v0, p0);
}
else {
pushDown(p);
if (p0 < mid(p))
sgtChg(p-> ls, p0, v0);
else
sgtChg(p-> rs, p0, v0);
update(p);
}
}
dat sgtQry(seg *p, int l, int r) {
pushDown(p);
if (p-> l == l && p-> r == r)
return p-> x[p-> nd];
else if (r <= mid(p))
return sgtQry(p-> ls, l, r);
else if (l >= mid(p))
return sgtQry(p-> rs, l, r);
else
return mkDat(sgtQry(p-> ls, l, mid(p)), sgtQry(p-> rs, mid(p), r));
}
void sgtRev(seg* p, int l, int r) {
pushDown(p);
if (p-> l == l && p-> r == r) {
p-> nd ^= 1;
p-> zd ^= 1;
}
else {
if (r <= mid(p))
sgtRev(p-> ls, l, r);
else if (l >= mid(p))
sgtRev(p-> rs, l, r);
else {
sgtRev(p-> ls, l, mid(p));
sgtRev(p-> rs, mid(p), r);
}
update(p);
}
}
int main() {
n = nextInt();
sp = new seg[maxn * 4];
for (int i = 1; i <= n; i ++)
a[i] = nextInt();
rt = sgtMake(1, n + 1);
m = nextInt();
while (m --) {
int opt = nextInt();
if (!opt) {
int p = nextInt();
int x = nextInt();
sgtChg(rt, p, x);
}
else {
int l = nextInt();
int r = nextInt();
int k = nextInt();
int ans = 0;
dat g[maxk];
for (int i = 0; i < k; i ++) {
g[i] = sgtQry(rt, l, r + 1);
sgtRev(rt, g[i]. p1, g[i]. p2 + 1);
if (g[i]. mx > 0)
ans += g[i]. mx;
else {
k = i + 1;
break;
}
}
for (int i = k - 1; i >= 0; i --)
sgtRev(rt, g[i]. p1, g[i]. p2 + 1);
printf("%d\n", ans);
}
}
}