萌新求助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 killer_queen4804 @ 2022-02-08 17:55:06

@z_z_y 听君一席话,胜过调十年代码,改了就ac了,nb!


上一页 |