求差查错/ hack

P1253 扶苏的问题

Micnation_AFO @ 2022-01-26 09:35:41

50pts,有 WA 有 TLE : https://www.luogu.com.cn/record/67962422

自己拍了一下小数据范围,拍了 1000 组没问题。洛谷上给的下载数据没有调试意义,所以恳求大佬们帮忙看看我的代码或提供一下 hack 数据。

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define maxn 1000005
struct SegmentTree {
    int l, r;
    int dat, add = 0, flag = -maxn;
} t[maxn * 4];
int n, m;
int a[maxn];

void build(int p, int l, int r) {
    t[p].l = l, t[p].r = r;
    if (l == r) { t[p].dat = a[l]; return; }
    int mid = (l + r) >> 1;
    build(p << 1, l, mid), build((p << 1) | 1, mid + 1, r);
    t[p].dat = max(t[p << 1].dat, t[(p << 1) | 1].dat);
}

void spread(int p) {
    if (t[p].flag != -maxn) {
        t[p << 1].dat = t[p].flag, t[(p << 1) | 1].dat = t[p].flag;
        t[p << 1].flag = t[(p << 1) | 1].flag = t[p].flag;
        t[p].flag = -maxn;
    }
    if (t[p].add) {
        t[p << 1].dat += t[p].add, t[(p << 1) | 1].dat += t[p].add;
        t[p << 1].add += t[p].add, t[(p << 1) | 1].add += t[p].add;
        t[p].add = 0;
    }
}

void change1(int p, int l, int r, int k) {
    if (l <= t[p].l && r >= t[p].r) {
        t[p].dat = k;
        t[p].add = 0, t[p].flag = k;
        return;
    }
    spread(p);
    int mid = (t[p].l + t[p].r) >> 1;
    if (l <= mid) change1(p << 1, l, r, k);
    if (r > mid) change1((p << 1) | 1, l, r, k);
    t[p].dat = max(t[p << 1].dat, t[(p << 1) | 1].dat);
}

void change2(int p, int l, int r, int k) {
    if (l <= t[p].l && r >= t[p].r) {
        t[p].dat += k;
        t[p].add += k;
        return;
    }
    spread(p);
    int mid = (t[p].l + t[p].r) >> 1;
    if (l <= mid) change2(p << 1, l, r, k);
    if (r > mid) change2((p << 1) | 1, l, r, k);
    t[p].dat = max(t[p << 1].dat, t[(p << 1) | 1].dat);
}

int ask(int p, int l, int r) {
    if (l <= t[p].l && r >= t[p].r) return t[p].dat;
    spread(p);
    int mid = (t[p].l + t[p].r) >> 1, val = -maxn;
    if (l <= mid) val = max(val, ask(p << 1, l, r));
    if (r > mid) val = max(val, ask((p << 1) | 1, l, r));
    return val;
}

signed main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    build(1, 1, n);
    for (int i = 1; i <= m; i++) {
        int op, l, r, x;
        cin >> op >> l >> r;
        if (op == 1) { cin >> x; change1(1, l, r, x); }
        else if (op == 2) { cin >> x; change2(1, l, r, x); }
        else cout << ask(1, l, r) << endl;
    }
    return 0;
}

by Albert_van @ 2022-01-26 09:50:14

long long

不要用

iostream

cstdio

或者是快读快输


by Albert_van @ 2022-01-26 09:51:05

long long

开了啊,那没事了


by qwq___qaq @ 2022-01-26 09:54:20

@Leap_hash_jperm |x|\le 10^9,覆盖时 maxn 开大点。


by Albert_van @ 2022-01-26 09:54:32

@Leap_hash_jperm 您的

flag

和查询时的

val

要开小一点,不能是

-maxn

题目里面有说 |x|\le 10^9


by Micnation_AFO @ 2022-01-26 11:31:00

@wαngxingdu @_sto_pengzijunorz 谢谢两位大佬!


by Micnation_AFO @ 2022-01-26 11:31:46

不过只涨了 10 pts

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define maxn 1000005
#define minn -100000000000
struct SegmentTree {
    int l, r;
    int dat, add = 0, flag = minn;
} t[maxn * 4];
int n, m;
int a[maxn];

void build(int p, int l, int r) {
    t[p].l = l, t[p].r = r;
    if (l == r) { t[p].dat = a[l]; return; }
    int mid = (l + r) >> 1;
    build(p << 1, l, mid), build((p << 1) | 1, mid + 1, r);
    t[p].dat = max(t[p << 1].dat, t[(p << 1) | 1].dat);
}

void spread(int p) {
    if (t[p].flag != minn) {
        t[p << 1].dat = t[p].flag, t[(p << 1) | 1].dat = t[p].flag;
        t[p << 1].flag = t[(p << 1) | 1].flag = t[p].flag;
        t[p].flag = minn;
    }
    if (t[p].add) {
        t[p << 1].dat += t[p].add, t[(p << 1) | 1].dat += t[p].add;
        t[p << 1].add += t[p].add, t[(p << 1) | 1].add += t[p].add;
        t[p].add = 0;
    }
}

void change1(int p, int l, int r, int k) {
    if (l <= t[p].l && r >= t[p].r) {
        t[p].dat = k;
        t[p].add = 0, t[p].flag = k;
        return;
    }
    spread(p);
    int mid = (t[p].l + t[p].r) >> 1;
    if (l <= mid) change1(p << 1, l, r, k);
    if (r > mid) change1((p << 1) | 1, l, r, k);
    t[p].dat = max(t[p << 1].dat, t[(p << 1) | 1].dat);
}

void change2(int p, int l, int r, int k) {
    if (l <= t[p].l && r >= t[p].r) {
        t[p].dat += k;
        t[p].add += k;
        return;
    }
    spread(p);
    int mid = (t[p].l + t[p].r) >> 1;
    if (l <= mid) change2(p << 1, l, r, k);
    if (r > mid) change2((p << 1) | 1, l, r, k);
    t[p].dat = max(t[p << 1].dat, t[(p << 1) | 1].dat);
}

int ask(int p, int l, int r) {
    if (l <= t[p].l && r >= t[p].r) return t[p].dat;
    spread(p);
    int mid = (t[p].l + t[p].r) >> 1, val = minn;
    if (l <= mid) val = max(val, ask(p << 1, l, r));
    if (r > mid) val = max(val, ask((p << 1) | 1, l, r));
    return val;
}

signed main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    build(1, 1, n);
    for (int i = 1; i <= m; i++) {
        int op, l, r, x;
        cin >> op >> l >> r;
        if (op == 1) { cin >> x; change1(1, l, r, x); }
        else if (op == 2) { cin >> x; change2(1, l, r, x); }
        else cout << ask(1, l, r) << endl;
    }
    return 0;
}

TLE 的问题不难解决,就是不知道为什么 WA

https://www.luogu.com.cn/record/67981836


by Albert_van @ 2022-01-26 11:49:32

@Leap_hash_jperm 您在 spread 的时候把左右儿子的 add 标记都清空一下应该就行了


by Micnation_AFO @ 2022-01-26 11:55:26

@wαngxingdu 过了,万分感谢(wssb,写之前还提醒自己注意这一点)


|