2019杭电多校Problem 5 Snowy Smile题解

2019杭电多校Problem 5 Snowy Smile题解_第1张图片

2019杭电多校Problem 5 Snowy Smile题解_第2张图片

转化为维护最大连续子串和

#include
#define ll long long
#define pi pair
#define mk make_pair
using namespace std;
const int maxn = 2010;
struct node {
    int x, y;
    ll w;
    bool operator<(const node& t) const {
        if (y == t.y)
            return x < t.x;
        return y > t.y;
    }
}p[maxn];
int X[maxn], Y[maxn];
ll mx[maxn * 4], L[maxn * 4], R[maxn * 4], sum[maxn * 4];
vector G[maxn];
#define ls o * 2
#define rs o * 2 + 1
#define mid (l + r) / 2
void up(int o, int l, int r, int k, ll w) {
    if (l == r) {
        mx[o] += w;
        L[o] += w;
        R[o] += w;
        sum[o] += w;
        return;
    }
    if (k <= mid)
        up(ls, l, mid, k, w);
    else
        up(rs, mid + 1, r, k, w);

    sum[o] = sum[ls] + sum[rs];
    L[o] = max(L[ls], sum[ls] + L[rs]);
    R[o] = max(R[rs], sum[rs] + R[ls]);
    mx[o] = max(L[o], R[o]);
    ll tmp = max(mx[ls], mx[rs]);
    ll tmp2 = max(tmp, R[ls] + L[rs]);
    mx[o] = max(mx[o], tmp2);
}
int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        int n, m = 0;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            scanf("%d%d%lld", &p[i].x, &p[i].y, &p[i].w);
            X[i] = p[i].x;
            Y[i] = p[i].y;
        }
        sort(X + 1, X + 1 + n);
        sort(Y + 1, Y + 1 + n);
        int sz1 = unique(X + 1, X + 1 + n) - X - 1;
        int sz2 = unique(Y + 1, Y + 1 + n) - Y - 1;
        for (int i = 1; i <= n; i++) {
            p[i].x = lower_bound(X + 1, X + 1 + sz1, p[i].x) - X;
            p[i].y = lower_bound(Y + 1, Y + 1 + sz2, p[i].y) - Y;
            G[p[i].y].push_back(mk(p[i].x, p[i].w));
        }
        ll ans = 0;
        for (int i = 1; i <= sz2; i++) { //枚举下边界
            for (int j = 1; j <= sz1 * 4; j++)
            mx[j] = L[j] = R[j] = sum[j] = 0;
            for (int j = i; j; j--) { //枚举上边界
                for (auto tmp : G[j])
                    up(1, 1, sz1, tmp.first, tmp.second);
                ans = max(ans, mx[1]);
            }
        }
        printf("%lld\n", ans);
        for (int i = 1; i <= sz2; i++)
            G[i].clear();
    }
}

XLS代码

你可能感兴趣的:(C++,ACM,算法,线段树,数据结构专题,题解)