SP1557 GSS2 题解报告

· · 题解

Problem

SP1557 GSS2 - Can you answer these queries II

Solution

很难用传统的前缀后缀最大和来维护,所以考虑离线。

先按照右端点排序。

每次这个数会在以 [pre_i+1,i] 为左端点的区间产生贡献,直接加,然后相当于查询一段区间内的历史最大和,直接上板子就行了。

其中,pre_i 表示上一个和 i 的值一样的下标。

Code

#include <bits/stdc++.h>
#define int long long
#define dd double
using namespace std;
template <typename T> inline void D(T &x)
{
    x = 0; bool f = 0; char ch = getchar();
    while('0' > ch || ch > '9') { if(ch == '-') f = !f; ch = getchar(); }
    while('0' <= ch && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }
    x = f ? -x : x; return;
}
template <typename T> inline void P(T x)
{
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) P(x / 10);
    putchar(x % 10 + '0'); return;
}
const int N = 2e5 + 10;
int n, m, a[N];
struct node
{
    int sum, hissum, laz, hislaz;
    node() {sum = hissum = laz = hislaz = 0;};
}tr[N << 2];
struct qq
{
    int l, r, id;
}q[N];
node operator + (const node &x, const node &y)
{
    node res;
    res.sum = max(x.sum, y.sum);
    res.hissum = max(x.hissum, y.hissum);
    return res;
}
int cmp(qq x, qq y)
{
    return x.r < y.r;
}
int cur[N], ans[N];
void pushdown(int k)
{
    if (!tr[k].laz && !tr[k].hislaz) return;

    tr[k << 1].hissum = max(tr[k << 1].hissum, tr[k << 1].sum + tr[k].hislaz);
    tr[k << 1 | 1].hissum = max(tr[k << 1 | 1].hissum, tr[k << 1 | 1].sum + tr[k].hislaz);

    tr[k << 1].sum += tr[k].laz;
    tr[k << 1 | 1].sum += tr[k].laz;

    tr[k << 1].hislaz = max(tr[k << 1].hislaz, tr[k << 1].laz + tr[k].hislaz);
    tr[k << 1 | 1].hislaz = max(tr[k << 1 | 1].hislaz, tr[k << 1 | 1].laz + tr[k].hislaz);

    tr[k << 1].laz += tr[k].laz;
    tr[k << 1 | 1].laz += tr[k].laz;

    tr[k].laz = tr[k].hislaz = 0;
}
void pushup(int k)
{
    tr[k] = tr[k << 1] + tr[k << 1 | 1];
}
void update(int k, int l, int r, int x, int y, int p)
{
    if (x <= l && r <= y)
    {
        tr[k].sum += p;
        tr[k].hissum = max(tr[k].hissum, tr[k].sum);
        tr[k].laz += p;
        tr[k].hislaz = max(tr[k].hislaz, tr[k].laz);
        return;
    }
    pushdown(k);
    int mid = l + r >> 1;
    if (x <= mid) update(k << 1, l, mid, x, y, p);
    if (mid + 1 <= y) update(k << 1 | 1, mid + 1, r, x, y, p);
    pushup(k);
}
node query(int k, int l, int r, int x, int y)
{
    if (x <= l && r <= y) return tr[k];
    pushdown(k);
    int mid = l + r >> 1;
    node res;
    if (x <= mid) res = res + query(k << 1, l, mid, x, y);
    if (mid + 1 <= y) res = res + query(k << 1 | 1, mid + 1, r, x, y);
    return res;
}
void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    cin >> m;
    for (int i = 1; i <= m; i ++) cin >> q[i].l >> q[i].r, q[i].id = i;
    sort(q + 1, q + 1 + m, cmp);
    int now = 1;
    for (int i = 1; i <= n; i ++)
    {
        int l = cur[a[i] + (int)1e5] + 1, r = i;
        cur[a[i] + (int)1e5] = i;
        update(1, 1, n, l, r, a[i]);
        for (; now <= m && q[now].r <= i; now ++)
        {
            ans[q[now].id] = query(1, 1, n, q[now].l, q[now].r).hissum;
        }
    }
    for (int i = 1; i <= m; i ++) cout << ans[i] << '\n';
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    //freopen(".in", "r", stdin); freopen(".out", "w", stdout);
    int T; T = 1;
    while (T --) solve();
    return 0;
}