题解:P12179 DerrickLo's Game (UBC002B)

· · 题解

solution

首先观察一下题面,发现好像不是很可以合并,然后考虑一下根号算法,发现被卡了。

然后再读题,诶这个操作 2 是不是有一个美妙的性质?

选择整数区间 x,y,将 a_x\dots a_y 全部变为 \max\limits_{i=x}^y a_i,代价为 (y-x+1)^2。

这个东西在长度为 2 的时候费用最小。于是这个操作就等价于选择两个数,花费 4 的代价将较小的数变成另一个数。

然后这个观察一下这个操作 1 好像又是要把一个数加到区间最大值,花费为区间最大值和它数值的差值。

然后计算答案的部分就很明显了:和区间最大值相等的数不用花费,比它小 1 到 3 的数用操作 1 更优,其他每个数都是操作 2 更优,花费都为 4。

那么我只要维护区间内最大值和与它相等、比它小 1、比它小 2、比它小 3 的数的个数。还有一个比它小 4 及以下的数的个数就可以了。

此时好像又变得可以合并了??

于是开始线段树。每个节点维护上述值,合并时讨论左右两个线段的最大值的差值,从而进行数据的合并;查询时把所有 [l,r] 在线段树上查到的线段记录下来,合并到一个空的节点上。于是就可以根据这个节点信息计算答案了。

code

#include<bits/stdc++.h>

namespace IO {
    inline int read() {
        int ret = 0, f = 1;char ch = getchar();
        while (ch < '0' || ch > '9') {
            if (ch == '-') f = -f;
            ch = getchar();
        }
        while (ch >= '0' && ch <= '9') {
            ret = (ret << 1) + (ret << 3) + (ch ^ 48);
            ch = getchar();
        }
        return ret * f;
    }
    void write(long long x) {
        if (x < 0) putchar('-'), x = -x;
        if (x > 9) write(x / 10);
        putchar(x % 10 + '0');
    }
}

using namespace IO;
using namespace std;

const int maxn = 2e5 + 5;

int n, q;
int a[maxn], b[maxn];

namespace seg {
    int L[maxn << 2], R[maxn << 2], Max[maxn << 2], s[maxn << 2][5], tot;
//注意 s[x][4] 是和 max 差值大于等于 4 的数的个数

    int len(int x) {
        return R[x] - L[x] + 1;
    }

    void upd(int x, int y) {//合并两个节点信息
        switch (Max[x] - Max[y]) {
            case 0 : {
                s[x][0] += s[y][0];
                s[x][1] += s[y][1];
                s[x][2] += s[y][2];
                s[x][3] += s[y][3];
                s[x][4] += s[y][4];
                break;
            }
            case 1 : {
                s[x][1] += s[y][0];
                s[x][2] += s[y][1];
                s[x][3] += s[y][2];
                s[x][4] += s[y][3] + s[y][4];
                break;
            }
            case 2 : {
                s[x][2] += s[y][0];
                s[x][3] += s[y][1];
                s[x][4] += s[y][2] + s[y][3] + s[y][4];
                break;
            }
            case 3 : {
                s[x][3] += s[y][0];
                s[x][4] += s[y][1] + s[y][2] + s[y][3] + s[y][4];
                break;
            }
            default : {
                s[x][4] += len(y);
                break;
            }
        }
    }

    void Clear(int x) {
        s[x][0] = s[x][1] = s[x][2] = s[x][3] = s[x][4] = Max[x] = 0;
    }

    void pushup(int x) {
        Clear(x);//Clear 位置错了调了 20min
        Max[x] = max(Max[x << 1], Max[x << 1 | 1]);
        upd(x, x << 1);
        upd(x, x << 1 | 1);
    }

    void build(int l, int r, int x) {
        L[x] = l, R[x] = r;
        if (l == r) {
            Max[x] = a[l];s[x][0] = 1;
            return;
        }
        int mid = l + r >> 1;
        build(l, mid, x << 1);
        build(mid + 1, r, x << 1 | 1);
        pushup(x);
    }

    void update(int p, int x, int d) {
        if (L[x] == R[x] && L[x] == p) {
            Max[x] = d;
            return;
        }
        int mid = L[x] + R[x] >> 1;
        if (p <= mid) update(p, x << 1, d);
        else update(p, x << 1 | 1, d);
        pushup(x);
    }

    int queryMax(int l, int r, int x) {
        if (L[x] >= l && R[x] <= r) {
            b[++tot] = x;
            return Max[x];
        }
        int mid = L[x] + R[x] >> 1, ans = 0;
        if (l <= mid) ans = max(ans, queryMax(l, r, x << 1));
        if (r > mid) ans = max(ans, queryMax(l, r, x << 1 | 1));
        return ans;
    }

    long long solve(int l, int r) {
        tot = 0;Clear((maxn << 2) - 1);
//可以不用(maxn << 2) - 1,用一个空节点就行
        int maxx = queryMax(l, r, 1);long long ans = 0ll;
        Max[(maxn << 2) - 1] = maxx;
        for (int i = 1;i <= tot;i++) upd((maxn << 2) - 1, b[i]);
        for (int i = 1;i <= 3;i++) ans += i * s[(maxn << 2) - 1][i];//和 max 差值小于 4 的数的花费
        ans += s[(maxn << 2) - 1][4] * 4;//使用操作 2 的花费
        return ans;
    }
}

using namespace seg;

int main() {
    n = read(), q = read();

    for (int i = 1;i <= n;i++) a[i] = read();

    build(1, n, 1);

    while (q--) {
        int op = read(), l = read(), r = read();
        if (op == 1) update(l, 1, r);
        else write(solve(l, r)), putchar(10);
    }

    return 0;
}