区间最大区间和范围求助
查看原帖
区间最大区间和范围求助
270854
二叉苹果树楼主2023/10/15 22:29
#include <bits/stdc++.h>
#define maxn 500010
#define lc x * 2
#define rc x * 2 + 1
#define int long long
inline __attribute__((always_inline)) int read()
{
    int x = 0, f = 1;
    char ch = getchar_unlocked();
    while (!isdigit(ch))
    {
        if (ch == '-')
            f = -1;
        ch = getchar_unlocked();
    }
    while (isdigit(ch))
    {
        x = x * 10 + ch - '0';
        ch = getchar_unlocked();
    }
    return x * f;
}
int n, m, k;
struct node
{
    int l, r;
    int ls, rs;
    int max, sum;
} t[maxn << 2];
int a[maxn];
void pushup(int x)
{
    t[x].sum = t[lc].sum + t[rc].sum;
    t[x].ls = std::max(t[lc].sum + t[rc].ls, t[lc].ls);
    t[x].rs = std::max(t[rc].sum + t[lc].rs, t[rc].rs);
    int max = std::max({t[lc].max, t[rc].max, t[lc].rs + t[rc].ls});
    if (t[lc].max == max)
        t[x].l = t[lc].l, t[x].r = t[lc].r;
    if (t[rc].max == max)
        t[x].l = t[rc].l, t[x].r = t[rc].r;
    if (t[lc].rs + t[rc].ls == max)
        t[x].l = t[lc].l, t[x].r = t[rc].r;
    t[x].max = max;
}
void build(int x, int l, int r)
{
    if (l == r)
    {
        t[x].l = t[x].r = l;
        t[x].ls = t[x].rs = a[l];
        t[x].max = t[x].sum = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(lc, l, mid);
    build(rc, mid + 1, r);
    pushup(x);
}
node query(int x, int l, int r, int L, int R)
{
    node ans, a, b;
    if (l >= L && r <= R)
        return t[x];
    int mid = (l + r) >> 1;
    if (mid >= R)
        return query(lc, l, mid, L, R);
    if (mid < L)
        return query(rc, mid + 1, r, L, R);
    a = query(lc, l, mid, L, R);
    b = query(rc, mid + 1, r, L, R);
    ans.sum = a.sum + b.sum;
    ans.ls = std::max(a.sum + b.ls, a.ls);
    ans.rs = std::max(b.sum + a.rs, b.rs);
    int max = std::max({a.max, b.max, a.rs + b.ls});
    ans.max = max;
    if (a.max == max)
        ans.l = a.l, ans.r = a.r;
    if (b.max == max)
        ans.l = b.l, ans.r = b.r;
    if (a.rs + b.ls == max)
        ans.l = a.l, ans.r = b.r;
    return ans;
}
void update(int x, int l, int r, int p, int v)
{
    if (l == r)
    {
        t[x].ls = t[x].rs = v;
        t[x].max = t[x].sum = v;
        return;
    }
    int mid = (l + r) >> 1;
    if (p <= mid)
        update(lc, l, mid, p, v);
    else 
        update(rc, mid + 1, r, p, v);
    pushup(x);
}
signed main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int T = 0;
    while (std::cin >> n >> m)
    {
        for (int i = 1; i <= n; i++)
            std::cin >> a[i];
        build(1, 1, n);
        std::cout << "Case " << ++T << ":\n";
        for (int i = 1; i <= m; i++)
        {
            int l, r;
            std::cin >> l >> r;
            if (l > r)
                std::swap(l, r);
            std::cout << query(1, 1, n, l, r).l << ' ' << query(1, 1, n, l, r).r << '\n';
        }
    }
    return 0;
}

RT,拿小白逛公园直接改的,和数据相比有时候能对一半或者对几个?

应该是一点小问题,求助!

2023/10/15 22:29
加载中...