蒟蒻求助线段树

P1253 扶苏的问题

Weight_of_the_Soul @ 2022-09-23 20:12:10

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#define ll long long
#define int ll
using namespace std;

const int INF = 1e18 + 7;

int rd() {
    int x = 0, w = 1;
    char c = getchar();

    while(c < '0' || c > '9') {
        if(c == '-') w = -1;
        c = getchar();
    }

    while(c >= '0' && c <= '9') {
        x = x * 10 + (c - '0');
        c = getchar();
    }

    return x * w;
}

const int N = 1e7 + 5;

int n, a[N];
int q;

struct Tree {
    int l, r;
    int dat;
    int tag1, tag2;

    #define tag1(x) tr[x].tag1
    #define l(x) tr[x].l
    #define r(x) tr[x].r
    #define ls (p << 1)
    #define rs (ls | 1)
    #define dat(x) tr[x].dat
    #define tag2(x) tr[x].tag2

}tr[N << 2];

void build(int p, int l, int r) {
    l(p) = l, r(p) = r;
    tag1(p) = INF;
    if(l == r) {
        dat(p) = a[l];
        return ;
    }

    int mid = (r - l) / 2 + l;
    build(ls, l, mid);
    build(rs, mid + 1, r);

    dat(p) = max(dat(ls), dat(rs));
}

void spr(int p) {
    if(tag1(p) != INF) {
        tag1(ls) = tag1(rs) = tag1(p);
        tag2(p) = tag2(ls) = tag2(rs) = 0;
        dat(ls) = dat(rs) = tag1(p);
        tag1(p) = INF;
    }

    if(tag2(p) != 0) {
        dat(ls) += tag2(p);
        dat(rs) += tag2(p);
        tag2(ls) += tag2(p);
        tag2(rs) += tag2(p);
        tag2(p) = 0;
    }
}

void change(int p, int l, int r, int del, int opt) {
    if(l <= l(p) && r(p) <= r) {
        if(opt == 1) {
            tag1(p) = del;
            dat(p) = del;
            tag2(p) = 0;
        } else {
            spr(p);
            dat(p) += del;
            tag2(p) += del;
        }

        return ;
    }

    spr(p);

    int mid = (r(p) - l(p)) / 2 + l(p);

    if(l <= mid) change(ls, l, r, del, opt);
    if(r > mid) change(rs, l, r, del, opt);

    dat(p) = max(dat(ls), dat(rs));
}

int ask(int p, int l, int r) {
    if(l <= l(p) && r(p) <= r) return dat(p);

    spr(p);

    int mid = (r(p) - l(p)) / 2 + l(p);
    int res = -INF;

    if(l <= mid) res = max(res, ask(ls, l, r));
    if(r > mid) res = max(res, ask(rs, l, r));

    dat(p) = max(dat(ls), dat(rs));

    return res;
}

signed main() {
    n = rd(), q = rd();
    for(int i = 1; i <= n; ++i) a[i] = rd();

    build(1, 1, n);
    while(q--) {
        int opt = rd(), l = rd(), r = rd();
        if(opt <= 2) {
            int del = rd();
            change(1, l, r, del, opt);
        } else printf("%lld\n", ask(1, l, r));
    }
    return 0;
}

by Weight_of_the_Soul @ 2022-09-23 20:12:50

@Ptilopsis_w


by Weight_of_the_Soul @ 2022-09-23 20:13:30

后 4 个点 WA


by Weight_of_the_Soul @ 2022-09-23 20:20:47

@LordLaffey


by Ptilopsis_w @ 2022-09-23 20:31:43

有没有考虑一下开long long呢?


by Ptilopsis_w @ 2022-09-23 20:32:31

对不起没看见 #define int long long


by LordLaffey @ 2022-09-23 20:36:09

push_down() 里,下传完 tag1 ,不应该清空 tag2 ,因为 tag2 的代表的操作的时间在 tag1 之后 @Napoleon_Bonaparte


by LordLaffey @ 2022-09-23 20:36:47

(指 tag2(p)


by Weight_of_the_Soul @ 2022-09-23 20:38:21

@LordLaffey az,删掉了 tag2(p) 之后依旧只有 60


by Weight_of_the_Soul @ 2022-09-23 20:39:39

@LordLaffey 改过了,thx dalao

%%%

此贴完结


by 丙戌年 @ 2022-09-23 20:40:32

膜拜 @LordLaffey


|