题解:P4118 [Ynoi2018] 末日时在做什么?有没有空?可以来拯救吗?
P4118 [Ynoi2018] 末日时在做什么?有没有空?可以来拯救吗?
前言
这题是 P5073 [Ynoi Easy Round 2015] 世上最幸福的女孩 的加强版,所以先讲弱化版。
::::info[弱化版]
不带修改的线段树做法
线段树每个节点维护四个值:
合并:
因为可以不选数,所有
全局加时发生了什么
如果整个区间都加上
其实,对于固定的节点,当整体加上
这些函数都是关于
当整体加上
- 当前最优值直接加上
k\cdot x ; - 阈值
t 减去x ; - 如果
t<0 ,说明最优直线变了,得递归重建。
离线处理
因为是全局加,所有修改加的是同一个值。把所有询问离线下来,记录每个询问发生时的全局加量
初始时把所有数加上最小可能的
- 如果当前询问的
add 比现在的全局加量大,就对整棵树做一次全局加; - 否则直接查询区间最大子段和。
| 单次查询 |
|---|
::::info[本题]
弱化版的做法在本题上不适用,因为区间加不是全局的,不能简单离线排序。但我们可以用分块把区间加拆成“整块全局加”和“散块暴力”。
分块
把序列分成
对每个块单独建立一棵线段树,线段树的节点维护的东西和弱化版一样:
对每个块,遍历所有
- 如果操作完全覆盖该块:
- 修改:直接累加一个全局变量
tag (表示整个块被加了多少); - 查询:先把这个询问记下来,同时记下当前的
tag 值。
- 修改:直接累加一个全局变量
- 如果操作只部分覆盖该块:
- 修改:直接在线段树上对相应区间进行区间加;
- 查询:直接在线段树上查询部分区间,合并得到答案。
合起来一起算
当完全覆盖的查询攒到一定数量,就一起处理:
- 把这些查询按
tag 排序; - 利用根节点的凸包,用双指针单调求出每个查询的答案。
因为
最后再处理剩下的。
块内线段树的凸包合并
块内线段树的 push_up 需要合并左右儿子的信息,并重新构建父节点的凸包。合并时用到 mergeh 函数:给定左右儿子的
具体做法就是利用凸包的性质,类似归并排序地扫描两个凸包的差分向量,同时维护当前的最优值。
复杂度
设块大小为
- 对每个块,遍历所有
m 个操作。完全覆盖的操作O(1) ,部分覆盖的操作需要在线段树上进行,线段树大小为B ,单次操作O(\log B) 。 - 攒起来的查询一起处理,总复杂度与查询数量和凸包大小有关,均摊下来也是
O(m\log B) 左右。 - 总复杂度约为
O\left(\frac{n}{B}\cdot m\log B\right) 。
| 取 |
|---|
AC 代码
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
const int MAX = (1 << 16) - 1;
const int BLOCK = 250;
char buf[1 << 20], out[1 << 20];
int idx = 0, size = 0, outidx = 0;
inline int gc() {
if (idx >= size) {
size = fread(buf, 1, 1 << 20, stdin);
idx = 0;
if (!size) {
return EOF;
}
}
return buf[idx++];
}
template <typename T>
inline void read(T &x) {
x = 0;
T f = 1;
int c = gc();
while (c < '0' || c > '9') {
if (c == '-') {
f = -1;
}
c = gc();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = gc();
}
x *= f;
}
inline void pc(char c) {
if (outidx == (1 << 20)) {
fwrite(out, 1, outidx, stdout);
outidx = 0;
}
out[outidx++] = c;
}
inline void write(long long x) {
if (x == 0) {
pc('0');
pc('\n');
return;
}
if (x < 0) {
pc('-');
x = -x;
}
char s[25];
int top = 0;
while (x) {
s[top++] = x % 10 + '0';
x /= 10;
}
while (top) {
pc(s[--top]);
}
pc('\n');
}
struct Node {
long long lmax, rmax, tmax, sum;
} ans[MAXN];
struct Point {
int x;
long long y;
} pl[MAXN], t1[BLOCK + 5], t2[BLOCK + 5];
inline Point operator + (const Point &a, const Point &b) {
Point c;
c.x = a.x + b.x;
c.y = a.y + b.y;
return c;
}
inline Point operator - (const Point &a, const Point &b) {
Point c;
c.x = a.x - b.x;
c.y = a.y - b.y;
return c;
}
inline long long operator * (const Point &a, const Point &b) {
return 1LL * a.x * b.y - 1LL * a.y * b.x;
}
struct Hull {
int len;
Point *p;
inline void init(Point *q) {
len = 0;
p = q;
}
inline void insert(Point x) {
while (len > 1) {
Point a = x - p[len - 1];
Point b = p[len] - p[len - 1];
if (a * b <= 0) {
--len;
}
else {
break;
}
}
p[++len] = x;
}
inline long long get(long long k) const {
int l = 1, r = len - 1, pos = len;
while (l <= r) {
int mid = (l + r) >> 1;
if (p[mid + 1].y + 1LL * p[mid + 1].x * k > p[mid].y + 1LL * p[mid].x * k) {
l = mid + 1;
}
else {
pos = mid;
r = mid - 1;
}
}
return p[pos].y + 1LL * p[pos].x * k;
}
} lmax[(BLOCK + 5) << 2], rmax[(BLOCK + 5) << 2], tmax[(BLOCK + 5) << 2];
struct Query {
int op, l, r;
long long x, k;
} q[MAXN];
long long sum[(BLOCK + 5) << 2], tag[(BLOCK + 5) << 2], best[BLOCK + 5];
int a[BLOCK + 5], b[MAXN], len[(BLOCK + 5) << 2], l[MAXN], r[MAXN], id[MAXN], tid[MAXN], cnt[1 << 16], tot, blocks, qcnt, n, m;
inline void mergeh(const Hull &L, const Hull &R, long long tagl, long long tagr) {
for (int i = 1; i < L.len; ++i) {
t1[i] = L.p[i + 1] - L.p[i];
t1[i].y += 1LL * t1[i].x * tagl;
}
for (int i = 1; i < R.len; ++i) {
t2[i] = R.p[i + 1] - R.p[i];
t2[i].y += 1LL * t2[i].x * tagr;
}
Point cur = L.p[1] + R.p[1];
cur.y += 1LL * cur.x * tagl + 1LL * R.p[1].x * tagr;
best[cur.x] = max(best[cur.x], cur.y);
int i = 1, j = 1;
while (i < L.len && j < R.len) {
if (t1[i] * t2[j] <= 0) {
cur = cur + t1[i++];
}
else {
cur = cur + t2[j++];
}
best[cur.x] = max(best[cur.x], cur.y);
}
while (i < L.len) {
cur = cur + t1[i++];
best[cur.x] = max(best[cur.x], cur.y);
}
while (j < R.len) {
cur = cur + t2[j++];
best[cur.x] = max(best[cur.x], cur.y);
}
}
inline void push_up(int p) {
lmax[p].len = rmax[p].len = tmax[p].len = 0;
long long lsum = sum[p << 1] + tag[p << 1] * len[p << 1], rsum = sum[p << 1 | 1] + tag[p << 1 | 1] * len[p << 1 | 1];
sum[p] = lsum + rsum;
Point x;
for (int i = 1; i <= lmax[p << 1].len; ++i) {
x = lmax[p << 1].p[i];
x.y += 1LL * x.x * tag[p << 1];
lmax[p].insert(x);
}
for (int i = 1; i <= lmax[p << 1 | 1].len; ++i) {
x = lmax[p << 1 | 1].p[i];
x.y += 1LL * x.x * tag[p << 1 | 1] + lsum;
x.x += len[p << 1];
lmax[p].insert(x);
}
for (int i = 1; i <= rmax[p << 1 | 1].len; ++i) {
x = rmax[p << 1 | 1].p[i];
x.y += 1LL * x.x * tag[p << 1 | 1];
rmax[p].insert(x);
}
for (int i = 1; i <= rmax[p << 1].len; ++i) {
x = rmax[p << 1].p[i];
x.y += 1LL * x.x * tag[p << 1] + rsum;
x.x += len[p << 1 | 1];
rmax[p].insert(x);
}
for (int i = 0; i <= len[p]; ++i) {
best[i] = -0x3f3f3f3f3f3f3f3fLL;
}
mergeh(rmax[p << 1], lmax[p << 1 | 1], tag[p << 1], tag[p << 1 | 1]);
for (int i = 1; i <= tmax[p << 1].len; ++i) {
x = tmax[p << 1].p[i];
x.y += 1LL * x.x * tag[p << 1];
best[x.x] = max(best[x.x], x.y);
}
for (int i = 1; i <= tmax[p << 1 | 1].len; ++i) {
x = tmax[p << 1 | 1].p[i];
x.y += 1LL * x.x * tag[p << 1 | 1];
best[x.x] = max(best[x.x], x.y);
}
for (int i = 0; i <= len[p]; ++i) {
if (best[i] != -0x3f3f3f3f3f3f3f3fLL) {
x.x = i;
x.y = best[i];
tmax[p].insert(x);
}
}
tag[p] = 0;
}
inline void build(int p, int l, int r) {
len[p] = r - l + 1;
tag[p] = 0;
lmax[p].init(pl + tot);
tot += r - l + 2;
rmax[p].init(pl + tot);
tot += r - l + 2;
tmax[p].init(pl + tot);
tot += r - l + 2;
if (l == r) {
sum[p] = a[l];
lmax[p].len = rmax[p].len = tmax[p].len = 2;
lmax[p].p[1].x = 0; lmax[p].p[1].y = 0;
lmax[p].p[2].x = 1; lmax[p].p[2].y = a[l];
rmax[p].p[1].x = 0; rmax[p].p[1].y = 0;
rmax[p].p[2].x = 1; rmax[p].p[2].y = a[l];
tmax[p].p[1].x = 0; tmax[p].p[1].y = 0;
tmax[p].p[2].x = 1; tmax[p].p[2].y = a[l];
return;
}
int mid = (l + r) >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
push_up(p);
}
inline void Addt(int p, long long x) {
tag[p] += x;
}
inline void push_down(int p) {
if (tag[p]) {
long long x = tag[p];
Addt(p << 1, x);
Addt(p << 1 | 1, x);
tag[p] = 0;
}
}
inline void Add(int p, int l, int r, int ql, int qr, long long x) {
if (ql <= l && r <= qr) {
Addt(p, x);
return;
}
push_down(p);
int mid = (l + r) >> 1;
if (ql <= mid) {
Add(p << 1, l, mid, ql, qr, x);
}
if (qr > mid) {
Add(p << 1 | 1, mid + 1, r, ql, qr, x);
}
push_up(p);
}
inline Node mergen(const Node &a, const Node &b) {
Node c;
c.lmax = max(a.lmax, a.sum + b.lmax);
c.rmax = max(b.rmax, b.sum + a.rmax);
c.tmax = max(max(a.tmax, b.tmax), a.rmax + b.lmax);
c.sum = a.sum + b.sum;
return c;
}
inline Node query(int p, int l, int r, int ql, int qr, long long x) {
if (ql <= l && r <= qr) {
Node res;
long long k = x + tag[p];
res.lmax = lmax[p].get(k);
res.rmax = rmax[p].get(k);
res.tmax = tmax[p].get(k);
res.sum = sum[p] + k * len[p];
return res;
}
int mid = (l + r) >> 1;
x += tag[p];
if (qr <= mid) {
return query(p << 1, l, mid, ql, qr, x);
}
if (ql > mid) {
return query(p << 1 | 1, mid + 1, r, ql, qr, x);
}
Node L = query(p << 1, l, mid, ql, qr, x), R = query(p << 1 | 1, mid + 1, r, ql, qr, x);
return mergen(L, R);
}
inline bool cmp(int x, int y) {
return q[x].k < q[y].k;
}
inline void rsort(int n) {
sort(id + 1, id + n + 1, cmp);
}
inline void solvep() {
if (!qcnt) {
return;
}
rsort(qcnt);
int pl = 1, pr = 1, pt = 1;
for (int i = 1; i <= qcnt; ++i) {
int cur = id[i];
long long x = q[cur].k;
while (pl < lmax[1].len && lmax[1].p[pl + 1].y + 1LL * lmax[1].p[pl + 1].x * x > lmax[1].p[pl].y + 1LL * lmax[1].p[pl].x * x) {
++pl;
}
while (pr < rmax[1].len && rmax[1].p[pr + 1].y + 1LL * rmax[1].p[pr + 1].x * x > rmax[1].p[pr].y + 1LL * rmax[1].p[pr].x * x) {
++pr;
}
while (pt < tmax[1].len && tmax[1].p[pt + 1].y + 1LL * tmax[1].p[pt + 1].x * x > tmax[1].p[pt].y + 1LL * tmax[1].p[pt].x * x) {
++pt;
}
Node res;
res.lmax = lmax[1].p[pl].y + 1LL * lmax[1].p[pl].x * x;
res.rmax = rmax[1].p[pr].y + 1LL * rmax[1].p[pr].x * x;
res.tmax = tmax[1].p[pt].y + 1LL * tmax[1].p[pt].x * x;
res.sum = sum[1] + x * len[1];
ans[cur] = mergen(ans[cur], res);
}
qcnt = 0;
}
inline void solveb(int k) {
tot = 0;
qcnt = 0;
int lpos = l[k], rpos = r[k], lenb = rpos - lpos + 1;
for (int i = 1; i <= lenb; ++i) {
a[i] = b[lpos + i - 1];
}
if (k == 1) {
long long addtag = 0;
for (int i = 1; i <= m; ++i) {
if (q[i].op == 1) {
int ql = max(q[i].l, lpos), qr = min(q[i].r, rpos);
if (ql > qr) {
continue;
}
if (q[i].l <= lpos && rpos <= q[i].r) {
addtag += q[i].x;
}
else {
for (int j = ql; j <= qr; ++j) {
a[j - lpos + 1] += q[i].x;
}
}
}
else {
int ql = max(q[i].l, lpos), qr = min(q[i].r, rpos);
if (ql > qr) {
continue;
}
int L = ql - lpos + 1, R = qr - lpos + 1;
Node res;
res.sum = 0;
long long prefix = 0, suffix = 0, cur = 0, maxl = -0x3f3f3f3f3f3f3f3fLL, maxr = -0x3f3f3f3f3f3f3f3fLL, maxsum = -0x3f3f3f3f3f3f3f3fLL, Sum = 0;
for (int j = L; j <= R; ++j) {
long long val = a[j] + addtag;
Sum += val;
prefix += val;
if (prefix > maxl) {
maxl = prefix;
}
}
suffix = 0;
for (int j = R; j >= L; --j) {
long long val = a[j] + addtag;
suffix += val;
if (suffix > maxr) {
maxr = suffix;
}
}
cur = 0;
for (int j = L; j <= R; ++j) {
long long val = a[j] + addtag;
cur = max(0LL, cur) + val;
if (cur > maxsum) {
maxsum = cur;
}
}
res.sum = Sum;
res.lmax = maxl;
res.rmax = maxr;
res.tmax = maxsum;
ans[i] = mergen(ans[i], res);
}
}
return;
}
tot = 0;
qcnt = 0;
build(1, 1, lenb);
long long x = 0;
for (int i = 1; i <= m; ++i) {
if (q[i].op == 1) {
if (q[i].l <= lpos && rpos <= q[i].r) {
x += q[i].x;
}
else {
int ql = max(q[i].l, lpos), qr = min(q[i].r, rpos);
if (ql > qr) {
continue;
}
if (qcnt > 0) {
if (qcnt < 200) {
for (int j = 1; j <= qcnt; ++j) {
int cur = id[j];
Node res = query(1, 1, lenb, 1, lenb, q[cur].k);
ans[cur] = mergen(ans[cur], res);
}
qcnt = 0;
}
else {
solvep();
}
}
Add(1, 1, lenb, ql - lpos + 1, qr - lpos + 1, q[i].x);
}
}
else {
if (q[i].l <= lpos && rpos <= q[i].r) {
if (qcnt < 200) {
Node res = query(1, 1, lenb, 1, lenb, x);
ans[i] = mergen(ans[i], res);
}
else {
id[++qcnt] = i;
q[i].k = x;
}
}
else {
int ql = max(q[i].l, lpos), qr = min(q[i].r, rpos);
if (ql > qr) {
continue;
}
Node res = query(1, 1, lenb, ql - lpos + 1, qr - lpos + 1, x);
ans[i] = mergen(ans[i], res);
}
}
}
if (qcnt > 0) {
if (qcnt < 200) {
for (int j = 1; j <= qcnt; ++j) {
int cur = id[j];
Node res = query(1, 1, lenb, 1, lenb, q[cur].k);
ans[cur] = mergen(ans[cur], res);
}
qcnt = 0;
}
else {
solvep();
}
}
}
int main() {
read(n);
read(m);
for (int i = 1; i <= n; ++i) {
read(b[i]);
}
for (int i = 1; i <= m; ++i) {
read(q[i].op);
read(q[i].l);
read(q[i].r);
if (q[i].op == 1) {
read(q[i].x);
}
else {
ans[i] = {0, 0, 0, 0};
}
}
blocks = (n + BLOCK - 1) / BLOCK;
for (int i = 1; i <= blocks; ++i) {
l[i] = (i - 1) * BLOCK + 1;
r[i] = min(i * BLOCK, n);
}
for (int i = 1; i <= blocks; ++i) {
solveb(i);
}
for (int i = 1; i <= m; ++i) {
if (q[i].op == 2) {
write(max(0LL, ans[i].tmax));
}
}
fwrite(out, 1, outidx, stdout);
return 0;
}