我不会数学。
首先,一个“修改”操作和一个询问操作可以变成只有一个询问操作,询问参数是左端点,右端点,全局加了多少。
假设不仅只询问一整个序列还对预处理时间不限;可以怎么做呢?显然全局加 会导致任意一个长度为 的区间和多了 ,但是一样长度的区间之间不能被全局加拉开差距。于是,我们先对所有 处理出来最大长度为 子段和 并且加入到一个凸包里一次函数 。询问变成在这个凸包里询问一个单位置,离线排序询问位置并用单调队列可以做到 询问, 预处理。但是这个显然不是本题要求的东西。
考虑如何把这个拓展到任意区间上;维护区间最大子段和常规套路就是用线段树维护每一个节点对应区间前缀最大和,后缀最大和,全局和,与最大字段和。在这里面,所有和都变成一个凸包,然后又因为
我不会数学。
这些凸包没法“合并”。
于是考虑把这些东西只维护一层,也就是分块大法。将序列分成一些大小 的块。对每一个块维护前缀最大和,后缀最大和,以及最大子段和对应的凸包;这样可以直接分类讨论的跑一个接近最大子段和东西。
首先需要建立这些块的凸包:对每一个块,建前缀最大,后缀最大需要 时间;建最大子段和就只能暴力 ;总共预处理时间为
考虑具体怎么分块跑最大子段和:
- 当两端点在同一块时,暴力跑,。
- 否则,先把左端点暴力到一个块的开始,右端点暴力到一个块的尾部。
- 把块从左到右遍历,按照线段树标记合并一样方法合并,毕竟现在在 凸包求值后已经全退化成整数了。。
所有询问都离线并且按照 递减排序可以做到 凸包询问,达到最坏 。
总共时间复杂度 ,选 达到严格 ,但是因为预处理常数挺小就可以选一个偏大的 。
可读的代码:
#pragma GCC target("avx2")
#pragma GCC optimize("O4")
// writer: w33z8kqrqk8zzzx33
#include <bits/stdc++.h>
using namespace std;
// begin fast read template by CYJian (source: https://www.luogu.com.cn/paste/i11c3ppx)
namespace io {
const int __SIZE = (1 << 21) + 1;
char ibuf[__SIZE], *iS, *iT, obuf[__SIZE], *oS = obuf, *oT = oS + __SIZE - 1, __c, qu[55]; int __f, qr, _eof;
#define Gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
inline void flush () { fwrite (obuf, 1, oS - obuf, stdout), oS = obuf; }
inline void gc (char &x) { x = Gc(); }
inline void pc (char x) { *oS ++ = x; if (oS == oT) flush (); }
inline void pstr (const char *s) { int __len = strlen(s); for (__f = 0; __f < __len; ++__f) pc (s[__f]); }
inline void gstr (char *s) { for(__c = Gc(); __c < 32 || __c > 126 || __c == ' ';) __c = Gc();
for(; __c > 31 && __c < 127 && __c != ' '; ++s, __c = Gc()) *s = __c; *s = 0; }
template <class I> inline bool gi (I &x) { _eof = 0;
for (__f = 1, __c = Gc(); (__c < '0' || __c > '9') && !_eof; __c = Gc()) { if (__c == '-') __f = -1; _eof |= __c == EOF; }
for (x = 0; __c <= '9' && __c >= '0' && !_eof; __c = Gc()) x = x * 10 + (__c & 15), _eof |= __c == EOF; x *= __f; return !_eof; }
template <class I> inline void print (I x) { if (!x) pc ('0'); if (x < 0) pc ('-'), x = -x;
while (x) qu[++ qr] = x % 10 + '0', x /= 10; while (qr) pc (qu[qr --]); }
struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
} using io::pc; using io::gc; using io::pstr; using io::gstr; using io::gi; using io::print;
// end fast read template by CYJian
#define iter(i, a, b) for(int i=(a); i<(b); i++)
#define rep(i, a) iter(i, 0, a)
#define rep1(i, a) iter(i, 1, (a)+1)
#define log(a) cerr<<"\033[32m[DEBUG] "<<#a<<'='<<(a)<<" @ line "<<__LINE__<<"\033[0m"<<endl
#define all(a) a.begin(), a.end()
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using ll=long long;
using pii=pair<int, int>;
//#define int ll
const int MOD = 1000000007;
inline bool fracle(ll n1, ll d1, ll n2, ll d2) {
//return ((double)n1)/d1 <= ((double)n2)/d2;
if(d1 == 0) return 0;
if(d2 == 0) return 1;
if(d1*d2 > 0) return (n1*d2) < (n2*d1);
else return (n1*d2) > (n2*d1);
}
struct LineContainer {
int qux[1095]; ll quy[1095];
int qh, qt;
vector<pair<int,ll>> que;
void add(const int& m, const ll& b) {
que.pb({m, b});
}
void finalize() {
sort(all(que));
reverse(all(que));
qux[0] = -que[0].fi, quy[0] = que[0].se;
rep1(i, que.size()-1) {
int m = que[i].fi; ll b = que[i].se;
while(qh != qt && fracle(quy[qh-1]-quy[qh], qux[qh-1]-qux[qh], quy[qh]-b, qux[qh]+m)) qh--;
qux[++qh] = -m; quy[qh] = b;
}
}
ll query(ll y) {
while(qh != qt && fracle(y, 1, quy[qt]-quy[qt+1], qux[qt]-qux[qt+1])) qt++;
return quy[qt] - 1ll * qux[qt] * y;
}
};
const int cs = 1024;
int cc = 0;
ll chunk[705][cs+5]; ll tot[705];
LineContainer prefix[705], suffix[705], midfix[705];
ll qry(int l, int r, ll curd) {
int lckid = l/cs, lcklo = l%cs;
int rckid = r/cs, rcklo = r%cs;
if(lckid == rckid) {
ll o1 = 0, c1 = 0;
int j = lcklo;
for(;j<rcklo+1;j++) {
c1 = max(c1, 0ll) + chunk[lckid][j] + curd;
o1 = max(o1, c1);
}
return o1;
}
ll o1 = 0, c1 = o1;
int j = lcklo;
for(;j<cs;j++) {
c1 = max(c1, 0ll) + chunk[lckid][j] + curd;
o1 = max(o1, c1);
}
ll o2 = 0, c2 = o2;
j = 0;
for(;j<rcklo+1;j++) {
c2 = max(c2, 0ll) + chunk[rckid][j] + curd;
o2 = max(o2, c2);
}
ll glob = max(o1, o2);
ll cur = 0, csuf = 0;
for(int j=cs-1; j>=lcklo; j--) {
csuf += chunk[lckid][j] + curd;
cur = max(cur, csuf);
}
iter(ckid, lckid+1, rckid) {
glob = max(glob, max(midfix[ckid].query(curd), cur + prefix[ckid].query(curd)));
cur = max(cur + tot[ckid] + curd*cs, suffix[ckid].query(curd));
glob = max(glob, cur);
}
rep(j, rcklo+1) {
cur += chunk[rckid][j] + curd;
glob = max(glob, cur);
}
return glob;
}
struct q {
int l, r; ll curd; int id;
const bool operator<(const q& o) const {
if(curd != o.curd) return curd > o.curd;
return id < o.id;
};
} qs[600005];
ll ans[600005];
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int n, m; gi(n), gi(m); cc = (n+cs-1)/cs;
rep(i, cc) rep(j, cs) chunk[i][j] = -1000000000000000000/cs+1;
rep(i, n) {
int ckid = i/cs, cklo = i%cs;
gi(chunk[ckid][cklo]);
}
rep(ck, cc) {
prefix[ck].add(0, 0);
suffix[ck].add(0, 0);
midfix[ck].add(0, 0);
ll ps = 0, ss = 0;
rep(i, cs) {
ps += chunk[ck][i]; prefix[ck].add(i+1, ps);
ss += chunk[ck][cs-i-1]; suffix[ck].add(i+1, ss);
ll opt = ps, cur = ps;
int j = i+1;
for(;j<cs;j++) {
cur += chunk[ck][j] - chunk[ck][j-i-1];
opt = max(opt, cur);
}
midfix[ck].add(i+1, opt);
}
tot[ck] = ps;
prefix[ck].finalize();
suffix[ck].finalize();
midfix[ck].finalize();
}
ll curd = 0;
int cnt = 0;
while(m--) {
int op; gi(op);
if(op == 1) {
int k; gi(k);
curd += k;
} else {
int l, r; gi(l), gi(r);
l--; r--;
qs[cnt] = {l, r, curd, cnt};
cnt++;
}
}
sort(qs, qs+cnt);
rep(i, cnt) ans[qs[i].id] = qry(qs[i].l, qs[i].r, qs[i].curd);
rep(i, cnt) print(ans[i]), pc('\n');
}