线段树20pts,新人求助

P1253 扶苏的问题

TomRiddle @ 2024-08-01 17:33:25

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 1, INF = -1e9;
int n, q, a[N], oper, l, r, x;
struct Tree
{
    int lbian = INF, ladd = 0;
    int maxx;
} tree[4 * N];
void pushup(int p)
{
    tree[p].maxx = max(tree[p * 2].maxx, tree[p * 2 + 1].maxx);
}
void build(int p, int l, int r)
{
    if (l == r)
    {
        tree[p].maxx = a[l];
        return;
    }
    int mid = l + r >> 1;
    build(p * 2, l, mid), build(p * 2 + 1, mid + 1, r);
    pushup(p);
}
void pushdown(int p)
{
    if (tree[p].lbian != INF)
    {
        tree[p * 2].lbian = tree[p * 2 + 1].lbian = tree[p].lbian;
        int mid = l + r >> 1;
        tree[p * 2].maxx = tree[p].lbian, tree[p * 2 + 1].maxx = tree[p].lbian, tree[p].lbian = INF;
    }
    if (tree[p].ladd != 0)
    {
        tree[p * 2].ladd += tree[p * 2 + 1].ladd = tree[p].ladd;
        int mid = l + r >> 1;
        tree[p * 2].maxx += tree[p].ladd, tree[p * 2 + 1].maxx += tree[p].ladd, tree[p].ladd = 0;
    }
}
void qbian(int p, int ql, int qr, int k, int l, int r)
{
    if (r < ql || l > qr) return;
    if (l >= ql && r <= qr)
    {
        tree[p].maxx = k, tree[p].lbian = k;
        return;
    }
    int mid = l + r >> 1;
    pushdown(p);
    qbian(p * 2, ql, qr, k, l, mid);
    qbian(p * 2 + 1, ql, qr, k, mid + 1, r);
    pushup(p);
}
void qjia(int p, int ql, int qr, long long k, int l, int r)
{
    if (r < ql || l > qr) return;
    if (ql <= l && qr >= r)
    {
        tree[p].ladd += k, tree[p].maxx += k;
        return;
    }
    pushdown(p);
    int mid = l + r >> 1;
    qjia(p * 2, ql, qr, k, l, mid), qjia(p * 2 + 1, ql, qr, k, mid + 1, r);
    pushup(p);
}
int askm(int p, int ql, int qr, int l, int r)
{
    if (l > qr || r < ql) return INF;
    if (l >= ql && r <= qr) return tree[p].maxx;
    int mid = l + r >> 1;
    pushdown(p);
    return max(askm(p * 2, ql, qr, l, mid), askm(p * 2 + 1, ql, qr, mid + 1, r));
}
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin>>n>>q;
    for (int i = 1;i <= n;i++) cin>>a[i];
    build(1, 1, n);
    while (q--)
    {
        cin>>oper>>l>>r;
        if (oper == 1)
        {
            cin>>x;
            qbian(1, l, r, x, 1, n);
        }
        else if (oper == 2)
        {
            cin>>x;
            qjia(1, l, r, x, 1, n);
        }
        else cout<<askm(1, l, r, 1, n)<<'\n';
    }
}

by Xlw6friend @ 2024-08-02 21:54:58

孩子,你好可怜啊,我来看看


by Xlw6friend @ 2024-08-02 22:02:25

if (tree[p].ladd != 0)
    {
        tree[p * 2].ladd += tree[p * 2 + 1].ladd = tree[p].ladd;
        int mid = l + r >> 1;
        tree[p * 2].maxx += tree[p].ladd, tree[p * 2 + 1].maxx += tree[p].ladd, tree[p].ladd = 0;
    }

首先,你看看你在写什么。其次,加标记和覆盖标记有重合的情况要特判消去互相影响


by TomRiddle @ 2024-08-20 11:45:14

@Xlw6friend 新代码50pts,帮调

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 1, INF = -1e9;
int n, q, a[N], oper, l, r, x;
struct Tree
{
    int lbian = INF, ladd = 0;
    int maxx;
} tree[4 * N];
void pushup(int p)
{
    tree[p].maxx = max(tree[p * 2].maxx, tree[p * 2 + 1].maxx);
}
void build(int p, int l, int r)
{
    if (l == r)
    {
        tree[p].maxx = a[l];
        return;
    }
    int mid = l + r >> 1;
    build(p * 2, l, mid), build(p * 2 + 1, mid + 1, r);
    pushup(p);
}
void pushdown(int p)
{
    if (tree[p].lbian != INF)
    {
        tree[p * 2].lbian = tree[p * 2 + 1].lbian = tree[p].lbian;
        tree[p * 2].maxx = tree[p * 2 + 1].maxx = tree[p].lbian;
        tree[p].lbian = INF;
        tree[p * 2].ladd = 0;
        tree[p * 2 + 1].ladd = 0;
    }
    if (tree[p].ladd != 0)
    {
        tree[p * 2].ladd += tree[p].ladd;
        tree[p * 2 + 1].ladd += tree[p].ladd;
        tree[p * 2].maxx += tree[p].ladd;
        tree[p * 2 + 1].maxx += tree[p].ladd;
        tree[p].ladd = 0;
    }
}
void qbian(int p, int ql, int qr, int k, int l, int r)
{
    if (r < ql || l > qr) return;
    if (l >= ql && r <= qr)
    {
        tree[p].maxx = k, tree[p].lbian = k;
        return;
    }
    int mid = l + r >> 1;
    pushdown(p);
    qbian(p * 2, ql, qr, k, l, mid);
    qbian(p * 2 + 1, ql, qr, k, mid + 1, r);
    pushup(p);
}
void qjia(int p, int ql, int qr, long long k, int l, int r)
{
    if (r < ql || l > qr) return;
    if (ql <= l && qr >= r)
    {
        tree[p].ladd += k, tree[p].maxx += k;
        return;
    }
    pushdown(p);
    int mid = l + r >> 1;
    qjia(p * 2, ql, qr, k, l, mid), qjia(p * 2 + 1, ql, qr, k, mid + 1, r);
    pushup(p);
}
int askm(int p, int ql, int qr, int l, int r)
{
    if (l > qr || r < ql) return INF;
    if (l >= ql && r <= qr) return tree[p].maxx;
    int mid = l + r >> 1, mx = -1e18;
    pushdown(p);
    if (l <= mid) mx = max(mx, askm(p * 2, ql, qr, l, mid));
    if (r > mid) mx = max(mx, askm(p * 2 + 1, ql, qr, mid + 1, r));
    return mx;
}
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin>>n>>q;
    for (int i = 1;i <= n;i++) cin>>a[i];
    build(1, 1, n);
    while (q--)
    {
        cin>>oper>>l>>r;
        if (oper == 1)
        {
            cin>>x;
            qbian(1, l, r, x, 1, n);
        }
        else if (oper == 2)
        {
            cin>>x;
            qjia(1, l, r, x, 1, n);
        }
        else cout<<askm(1, l, r, 1, n)<<'\n';
    }
}


by Xlw6friend @ 2024-08-24 00:56:19

码风是很久以前的 见谅

#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18+7;
const int MAXN = 1e6+5;
int n, q, op, l, r;
long long a[MAXN], x;
struct Node
{
    int l, r;
    long long tag, data, cover;
}st[4*MAXN];
void build(int rt, int l, int r)
{
    st[rt].l = l;
    st[rt].r = r;
    st[rt].tag = 0;
    st[rt].cover = inf;
    if (l == r)
    {
        st[rt].data = a[l];
        return;
    }
    int mid = (l+r) / 2;
    build(rt*2, l, mid);
    build(rt*2+1, mid+1, r);
    st[rt].data = max(st[rt*2].data, st[rt*2+1].data);
}
void push_down_cover(int rt)
{
    if (st[rt].cover != inf)
    {
        st[rt*2].data = st[rt].cover;
        st[rt*2].cover = st[rt].cover;
        st[rt*2].tag = 0;
        st[rt*2+1].data = st[rt].cover;
        st[rt*2+1].cover = st[rt].cover;
        st[rt*2+1].tag = 0;
        st[rt].cover = inf;
    }
}
void push_down_tag(int rt)
{
    st[rt*2].tag += st[rt].tag;
    st[rt*2].data += st[rt].tag;
    st[rt*2+1].tag += st[rt].tag;
    st[rt*2+1].data += st[rt].tag;
    st[rt].tag = 0;
}
void update(int rt, int l, int r, long long x)
{
    if (st[rt].l >= l && st[rt].r <= r)
    {
        st[rt].cover = x;
        st[rt].data = x;
        st[rt].tag = 0;
        return;
    }
    push_down_cover(rt);
    push_down_tag(rt);
    int mid = (st[rt].l + st[rt].r) / 2;
    if (l <= mid)
    {
        update(rt*2, l, r, x);
    }
    if (mid+1 <= r)
    {
        update(rt*2+1, l, r, x);
    }
    st[rt].data = max(st[rt*2].data, st[rt*2+1].data);
}
void add(int rt, int l, int r, long long x)
{
    if (st[rt].l >= l && st[rt].r <= r)
    {
        st[rt].tag += x;
        st[rt].data += x;
        return;
    }
    push_down_cover(rt);
    push_down_tag(rt);
    int mid = (st[rt].l + st[rt].r) / 2;
    if (l <= mid)
    {
        add(rt*2, l, r, x);
    }
    if (mid+1 <= r)
    {
        add(rt*2+1, l, r, x);
    }
    st[rt].data = max(st[rt*2].data, st[rt*2+1].data);
}
long long ask(int rt, int l, int r)
{
    if (st[rt].l >= l && st[rt].r <= r)
    {
        return st[rt].data;
    }
    push_down_cover(rt);
    push_down_tag(rt);
    int mid = (st[rt].l + st[rt].r) / 2;
    long long ans = -inf;
    if (l <= mid)
    {
        ans = max(ans, ask(rt*2, l, r));
    }
    if (mid+1 <= r)
    {
        ans = max(ans, ask(rt*2+1, l, r));
    }
    return ans;
}
int main()
{
    cin >> n >> q;
    for (int i = 1; i <= n; i++)
        scanf("%lld", &a[i]);
    build(1, 1, n);
    while (q--)
    {
        scanf("%d", &op);
        if (op == 1)
        {
            scanf("%d%d%lld", &l, &r, &x);
            update(1, l, r, x);
        }
        else if (op == 2)
        {
            scanf("%d%d%lld", &l, &r, &x);
            add(1, l, r, x);
        }
        else
        {
            scanf("%d%d", &l, &r);
            printf("%lld\n", ask(1, l, r));
        }
    } 
    return 0;
}

|