Loading

P6406 [COCI2014-2015#2] Norma

好像有别的简单做法?但是我是笨蛋,我用线段树。

\(f(l, r) = \min\limits_{i = l}^r a_i, g(l, r) = \max\limits_{i = l}^r a_i\)

\(i, j\) 分离,得到:

\[\sum_{i = 1}^n\sum_{j = i}^n[(j + 1) \times f(i, j) \times g(i, j) - i \times f(i, j) \times g(i, j)] \]

整理一下,得到:

\[\sum_{r = 1}^n\left[(r + 1)\sum_{l = 1}^r f(l, r) \times g(l, r)\right] - \sum_{l = 1}^n\left[l\sum_{r = l}^nf(l, r) \times g(l, r)\right] \]

于是问题转化为对每个 \(r\)\(\sum\limits_{l = 1}^r f(l, r) \times g(l, r)\) 和对每个 \(l\)\(\sum\limits_{r = l}^n f(l, r) \times g(l, r)\)

这俩问题是对称的,解决一个另一个就同理可得了,先考虑 \(\sum\limits_{l = 1}^r f(l, r) \times g(l, r)\)

枚举 \(r\),记 \(F_l = f(l, r), G_l = g(l, r)\),记 \(a_i\) 左侧第一个小于等于 \(a_i\) 的数为 \(a_{L_i}\),则 \(a_r\) 只会更新 \(F_{L_i + 1} \sim F_{r}\);记 \(a_i\) 左侧第一个大于等于 \(a_i\) 的数为 \(a_{L'_i}\),则 \(a_r\) 只会更新 \(G_{L'_i + 1} \sim G_r\)

线段树维护 \(F, G\)\(\sum F_i \times G_i\) 即可,时间复杂度 \(\mathcal O(n \log n)\)

对每个 \(l\)\(\sum\limits_{r = l}^n f(l, r) \times g(l, r)\) 时把 \(a\) 反转再做一次前面的过程就好了。

总时间复杂度 \(\mathcal O(n \log n)\)

注意空间只有 \(64~\text{MB}\),就尽量别开 long long 了。

代码:

#include <bits/stdc++.h>

using namespace std;

constexpr int N = 5e5 + 10, MOD = 1e9;

int n, a[N], lf[N], lg[N], A[N], B[N];
int top, stk[N];

namespace SGT {
    #define lson pos << 1
    #define rson pos << 1 | 1
    
    int sf[N << 2], sg[N << 2], sfg[N << 2], f[N << 2], g[N << 2];

    inline void pushup(int pos) {sf[pos] = (sf[lson] + sf[rson]) % MOD, sg[pos] = (sg[lson] + sg[rson]) % MOD, sfg[pos] = (sfg[lson] + sfg[rson]) % MOD;}

    inline void fixf(int pos, int l, int r, int c) {f[pos] = c, sf[pos] = 1ll * c * (r - l + 1) % MOD, sfg[pos] = 1ll * c * sg[pos] % MOD;}

    inline void fixg(int pos, int l, int r, int c) {g[pos] = c, sg[pos] = 1ll * c * (r - l + 1) % MOD, sfg[pos] = 1ll * c * sf[pos] % MOD;}

    void build(int pos, int l, int r) {
        sf[pos] = sg[pos] = sfg[pos] = f[pos] = g[pos] = 0;
        if (l == r) return; int mid = (l + r) >> 1;
        build(lson, l, mid), build(rson, mid + 1, r);
    }

    inline void pushdown(int pos, int l, int r) {
        int mid = (l + r) >> 1;
        if (f[pos]) {
            fixf(lson, l, mid, f[pos]), fixf(rson, mid + 1, r, f[pos]);
            f[pos] = 0;
        }
        if (g[pos]) {
            fixg(lson, l, mid, g[pos]), fixg(rson, mid + 1, r, g[pos]);
            g[pos] = 0;
        }
    }

    void updf(int pos, int l, int r, int x, int y, int c) {
        if (x <= l && r <= y) {fixf(pos, l, r, c); return;}
        pushdown(pos, l, r); int mid = (l + r) >> 1;
        if (x <= mid) updf(lson, l, mid, x, y, c);
        if (y > mid) updf(rson, mid + 1, r, x, y, c);
        pushup(pos);
    }

    void updg(int pos, int l, int r, int x, int y, int c) {
        if (x <= l && r <= y) {fixg(pos, l, r, c); return;}
        pushdown(pos, l, r); int mid = (l + r) >> 1;
        if (x <= mid) updg(lson, l, mid, x, y, c);
        if (y > mid) updg(rson, mid + 1, r, x, y, c);
        pushup(pos);
    }
}

void solve(int w[], int res[]) {
    memset(lf, 0, sizeof(lf)), memset(lg, 0, sizeof(lg));
    top = 0;
    for (int i = n; i; i--) {
        while (top && w[i] <= w[stk[top]]) lf[stk[top--]] = i;
        stk[++top] = i;
    }
    top = 0;
    for (int i = n; i; i--) {
        while (top && w[i] >= w[stk[top]]) lg[stk[top--]] = i;
        stk[++top] = i;
    }
    SGT::build(1, 1, n);
    for (int r = 1; r <= n; r++) {
        SGT::updf(1, 1, n, lf[r] + 1, r, a[r]), SGT::updg(1, 1, n, lg[r] + 1, r, a[r]);
        res[r] = SGT::sfg[1];
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(nullptr), cout.tie(nullptr);
    cin >> n; for (int i = 1; i <= n; i++) cin >> a[i];
    solve(a, A), reverse(a + 1, a + n + 1), solve(a, B), reverse(B + 1, B + n + 1);
    int ans = 0;
    for (int r = 1; r <= n; r++) ans = (ans + 1ll * (r + 1) * A[r]) % MOD;
    for (int l = 1; l <= n; l++) ans = (ans - 1ll * l * B[l]) % MOD;
    cout << (ans + MOD) % MOD;
    return 0;
}
posted @ 2024-01-10 20:06  Chy12321  阅读(9)  评论(0编辑  收藏  举报