「Wdsr-2.7」文文的摄影布置

「Wdsr-2.7」文文的摄影布置

一看题目就知道是数据结构题, 稍微一看, 线段树.

比较直接的想法是我们维护每个区间的答案, 但是我们该如何合并呢?

定义 \(v\) 为该区间答案, \(mx, mn\) 为区间最大的 \(a\) 和最小的 \(b\).

分两种情况:

  1. \(i, k\) 全部在左儿子或右儿子 \(v = \max(ls->v, rs->v)\) .
  2. \(i, k\) 分别在左右儿子, 则考虑 \(j\) 的位置, 为了方便计算, 我们定义 \(lv, rv\) 分别表示该区间的 \(a[i] - b[j]\) 的最大值和 \(a[k] - b[j]\) 的最大值. 这样就好更新了, \(v = \max(ls->lv + rs->mx, rs->rv + ls->mx)\) .

所以 \(v = \max(\max(ls->v, rs->v), \max(ls->lv + rs->mx, rs->rv, ls->mx))\) .

接下来我们考虑如何合并 \(lv, rv\) .

仍然是一个简单的分类, 看所取的 \(a, b\) 是在同一个儿子还是不同的儿子, 如果是同一个儿子, 显然直接取 \(\max\) . 如果不在同一个儿子, 那就最大值 - 最小值.

查询的时候我们之间判断查询区间在当前区间的左儿子还是右儿子还是中间, 然后 \(Merge\) 一下就好了.

\(code:\)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}
const int N = 5e5 + 5, inf = 1 << 28;
int n, m, a[N], b[N], opt;
struct Node {
    Node *ls, *rs;
    int l, r, lv, rv, v, mx, mn;
    int Mid() {
        return l + r >> 1;
    }
    bool In(int L, int R) {
        if (l >= L && r <= R) return 1;
        return 0;
    }
    void Maintain() {
        mn = min(ls->mn, rs->mn); mx = max(ls->mx, rs->mx);
        v = max(max(ls->v, rs->v), max(ls->lv + rs->mx, ls->mx + rs->rv));
        lv = max(max(ls->lv, rs->lv), ls->mx - rs->mn);
        rv = max(max(ls->rv, rs->rv), rs->mx - ls->mn);
    }
} *Root = new Node;
void Build(Node* &o, int l, int r) {
    o->l = l, o->r = r;
    o->lv = o->rv = o->v = -inf;
    if (l == r) {
        o->mx = a[l], o->mn = b[l];
        return;
    }
    o->ls = new Node, o->rs = new Node;
    int mid = o->Mid();
    Build(o->ls, l, mid); Build(o->rs, mid + 1, r);
    o->Maintain();
}
void Change(Node* &o, int x, int y) {
    if (o->l == x && o->r == x) {
        if (opt == 1) o->mx = y;
        else o->mn = y;
        return;
    }
    int mid = o->Mid();
    if (x <= mid) Change(o->ls, x, y);
    else if (x > mid) Change(o->rs, x, y);
    o->Maintain();
}
Node* Merge(Node *ls, Node *rs) {
    Node *ans = new Node;
    ans->l = ls->l, ans->r = rs->r;
    ans->mn = min(ls->mn, rs->mn); ans->mx = max(ls->mx, rs->mx);
    ans->v = max(max(ls->v, rs->v), max(ls->lv + rs->mx, ls->mx + rs->rv));
    ans->lv = max(max(ls->lv, rs->lv), ls->mx - rs->mn);
    ans->rv = max(max(ls->rv, rs->rv), rs->mx - ls->mn);
    return ans;
}
Node* Query(Node* o, int l, int r) {
    if (o->In(l, r)) return o;
    Node *ans;
    int mid = o->Mid();
    if (l <= mid && r > mid) ans = Merge(Query(o->ls, l, r), Query(o->rs, l, r));
    else {
        if (l <= mid) ans = Query(o->ls, l, r);
        else if (r > mid) ans = Query(o->rs, l, r);
    }
    return ans;
}
int main() {
    n = read(), m = read();
    for (int i = 1; i <= n; i++) a[i] = read();
    for (int i = 1; i <= n; i++) b[i] = read();
    Build(Root, 1, n);
    while (m--) {
        opt = read(); int x = read(), y = read();
        switch (opt) {
            case 1:
                Change(Root, x, y);
                break;
            case 2:
                Change(Root, x, y);
                break;
            case 3:
                printf("%d\n", Query(Root, x, y)->v);
                break;
            default: break;
        }
    }
    return 0;
}

怎么? 回来了? 恭喜你获得了 \(80pts\) 的好成绩!

咋? 还不满意? 那就来吧!

因为我们是指针党, 所以容易 \(MLE\) .

其实我们发现我们的内存是完全够用的, 只是 \(Merge\) 函数使用的内存过多, 然后我们就优化一下 \(Merge\) 函数和 \(Query\) 函数的空间, 因为我们最多递归 \(\log n\) 层, 所以我们随便开一个不算大的内存池, 每次都使用这个小内存池就好.

\(code:\)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}
const int N = 5e5 + 5, inf = 1 << 28;
int n, m, a[N], b[N], opt;
struct Node {
    Node *ls, *rs;
    int l, r, lv, rv, v, mx, mn;
    int Mid() {
        return l + r >> 1;
    }
    bool In(int L, int R) {
        if (l >= L && r <= R) return 1;
        return 0;
    }
    void Maintain() {
        mn = min(ls->mn, rs->mn); mx = max(ls->mx, rs->mx);
        v = max(max(ls->v, rs->v), max(ls->lv + rs->mx, ls->mx + rs->rv));
        lv = max(max(ls->lv, rs->lv), ls->mx - rs->mn);
        rv = max(max(ls->rv, rs->rv), rs->mx - ls->mn);
    }
} *Root = new Node, Tmp[1000], *top = Tmp;
void Build(Node* &o, int l, int r) {
    o->l = l, o->r = r;
    o->lv = o->rv = o->v = -inf;
    if (l == r) {
        o->mx = a[l], o->mn = b[l];
        return;
    }
    o->ls = new Node, o->rs = new Node;
    int mid = o->Mid();
    Build(o->ls, l, mid); Build(o->rs, mid + 1, r);
    o->Maintain();
}
void Change(Node* &o, int x, int y) {
    if (o->l == x && o->r == x) {
        if (opt == 1) o->mx = y;
        else o->mn = y;
        return;
    }
    int mid = o->Mid();
    if (x <= mid) Change(o->ls, x, y);
    else if (x > mid) Change(o->rs, x, y);
    o->Maintain();
}
Node* Merge(Node *ls, Node *rs) {
    Node *ans = ++top;
    ans->ls = ls, ans->rs = rs;
    ans->Maintain(); 
    return ans;
}
Node* Query(Node* o, int l, int r) {
    if (o->In(l, r)) return o;
    int mid = o->Mid();
    if (l <= mid && r > mid) return Merge(Query(o->ls, l, r), Query(o->rs, l, r));
    else {
        if (l <= mid) return Query(o->ls, l, r);
        else if (r > mid) return Query(o->rs, l, r);
    }
}
int main() {
    n = read(), m = read();
    for (int i = 1; i <= n; i++) a[i] = read();
    for (int i = 1; i <= n; i++) b[i] = read();
    Build(Root, 1, n);
    while (m--) {
        opt = read(); int x = read(), y = read();
        switch (opt) {
            case 1:
                Change(Root, x, y);
                break;
            case 2:
                Change(Root, x, y);
                break;
            case 3:
                top = Tmp;
                printf("%d\n", Query(Root, x, y)->v);
                break;
            default: break;
        }
    }
    return 0;
}
posted @ 2021-09-10 22:04  sshadows  阅读(29)  评论(0编辑  收藏  举报