萌新求助50分线段树..

P1253 扶苏的问题

_Goodnight @ 2022-01-08 13:13:10

到负数就WA了

#include <bits/stdc++.h>
#define lk k<<1
#define rk k<<1|1
#define mlr l+r>>1

using namespace std;
const int N = 1e6 + 5;
typedef long long ll;
ll n, q,l,r,x,op;
ll a[N], t[4 * N], a1[4 * N],a2[4*N];
#define INF 1<<50
void modify(int k, int l, int r,int v,int op) {
    if (op == 1) {a1[k] = v;t[k] = v;a2[k] = 0;}
    else {a2[k] += v;t[k] += v;}
}
void pushdown(int k, int l, int r, int mid) {
    if (a1[k]!=INF) {
        modify(lk, l, mid, a1[k], 1);
        modify(rk, mid + 1,r, a1[k], 1);
        a1[k] = INF;
        //a2[k] = 0;
    }
    if (a2[k]) {
        modify(lk, l, mid, a2[k], 2);
        modify(rk, mid + 1, r, a2[k], 2);
        a2[k] = 0;
    }
}
void build(int k, int l, int r) {
    if (l == r) {
        t[k] = a[l];
        return;
    }
    int mid = mlr;
    build(lk, l, mid); build(rk, mid + 1, r);
    t[k] = max(t[lk], t[rk]);
}
void update(int op,int k, int l, int r, int x, int y, ll v) {
    if (l > y || r < x)return;
    if (l >= x && r <= y) {
        modify(k, l, r, v,op);
        return;
    }
    int mid = mlr;
    pushdown(k, l, r, mid);
    update(op, lk, l, mid, x, y, v); update(op, rk, mid + 1, r, x, y, v);
    t[k] = max(t[lk], t[rk]);
}
ll query(int k, int l, int r, int x, int y) {
    if (l > y || r < x)return 0;
    if (l >= x && r <= y) {
        return t[k];
    }
    int mid = mlr;
    pushdown(k, l, r, mid);//
    return max(query(lk, l, mid, x, y), query(rk, mid + 1, r, x, y));
}
int main() {
    freopen("C:\\Users\\Administrator\\Downloads\\P1253_6.in", "r", stdin);
    freopen("E:\\out.out", "w", stdout);
    scanf("%lld%lld", &n, &q);
    for (int i = 1; i <= n; i++)scanf("%lld", &a[i]);
    for (int i = 0; i <= N; i++) {
        a1[i] = INF;
    }
    build(1, 1, n);
    while (q--) {
        scanf("%lld%lld%lld", &op, &l, &r);
        if (op == 1 || op == 2) {
            scanf("%lld", &x);
            update(op, 1, 1, n, l, r, x);
        }
        else {
            printf("%lld\n", query(1, 1, n, l, r));
        }
    }
}

by peterwuyihong @ 2022-01-08 13:21:28

草,怎么上一个贴也是求助的


by OldVagrant @ 2022-01-08 13:35:26

@_Goodnight INF开成-1ll<<60,然后覆盖标记的初值也赋成这个,清空覆盖标记也是给它赋成这个
你试试


by OldVagrant @ 2022-01-08 13:37:57

@_Goodnight 我看了一下,你a1数组没赋初值,要全部初始化成INF的,否则会挂


by OldVagrant @ 2022-01-08 13:39:17

还有下面的query,超过询问区间范围不能返回0,要返回一个极小值,因为可能存在某个询问区间的最大值是负的


by OldVagrant @ 2022-01-08 13:39:42

挂了再@我


by _Goodnight @ 2022-01-08 13:45:24

md又要去学校了


by __zzy__ @ 2022-01-08 13:45:40

好吧我眼瞎没看见你a1数组赋了初值,当我没说


by __zzy__ @ 2022-01-08 13:48:31

@_Goodnight 我是sb,你把你函数传的参数改成longlong,再照我说的把query函数的返回值改一下应该就过了


by __zzy__ @ 2022-01-08 13:50:20

@_Goodnight 改完的代码给你放这了

#include <bits/stdc++.h>
#define lk k<<1
#define rk k<<1|1
#define mlr l+r>>1

using namespace std;
const int N = 1e6 + 5;
typedef long long ll;
#define int ll
ll n, q,l,r,x,op;
ll a[N], t[4 * N], a1[4 * N],a2[4*N];
#define INF -1ll<<60
void modify(int k, int l, int r,int v,int op) {
    if (op == 1) {a1[k] = v;t[k] = v;a2[k] = 0;}
    else {a2[k] += v;t[k] += v;}
}
void pushdown(int k, int l, int r, int mid) {
    if (a1[k]!=INF) {
        modify(lk, l, mid, a1[k], 1);
        modify(rk, mid + 1,r, a1[k], 1);
        a1[k] = INF;
        //a2[k] = 0;
    }
    if (a2[k]) {
        modify(lk, l, mid, a2[k], 2);
        modify(rk, mid + 1, r, a2[k], 2);
        a2[k] = 0;
    }
}
void build(int k, int l, int r) {
    if (l == r) {
        t[k] = a[l];
        return;
    }
    int mid = mlr;
    build(lk, l, mid); build(rk, mid + 1, r);
    t[k] = max(t[lk], t[rk]);
}
void update(int op,int k, int l, int r, int x, int y, ll v) {
    if (l > y || r < x)return;
    if (l >= x && r <= y) {
        modify(k, l, r, v,op);
        return;
    }
    int mid = mlr;
    pushdown(k, l, r, mid);
    update(op, lk, l, mid, x, y, v); update(op, rk, mid + 1, r, x, y, v);
    t[k] = max(t[lk], t[rk]);
}
ll query(int k, int l, int r, int x, int y) {
    if (l > y || r < x)return INF;
    if (l >= x && r <= y) {
        return t[k];
    }
    int mid = mlr;
    pushdown(k, l, r, mid);//
    return max(query(lk, l, mid, x, y), query(rk, mid + 1, r, x, y));
}
signed main() {
    scanf("%lld%lld", &n, &q);
    for (int i = 1; i <= n; i++)scanf("%lld", &a[i]);
    for (int i = 0; i <= N; i++) {
        a1[i] = INF;
    }
    build(1, 1, n);
    while (q--) {
        scanf("%lld%lld%lld", &op, &l, &r);
        if (op == 1 || op == 2) {
            scanf("%lld", &x);
            update(op, 1, 1, n, l, r, x);
        }
        else {
            printf("%lld\n", query(1, 1, n, l, r));
        }
    }
}

by _Goodnight @ 2022-01-08 17:56:37


| 下一页