40pt TLE + RE 求调。。

P1253 扶苏的问题

Amazing_llh @ 2024-11-29 01:58:29

RE #10 是为什么 https://www.luogu.com.cn/record/191754035

#include <iostream>
#include <algorithm>
using namespace std;

const int MAXN = 1e6 + 3, inf = 0x7fffffff;
int d[MAXN], a[MAXN];

void createTree(int l, int r, int c) {
    if (l != r) {
        int mid = (l + r) / 2;
        createTree(l, mid, c*2);
        createTree(mid + 1, r, c*2 + 1);
        d[c] = max(d[c*2], d[c*2 + 1]);
    } else {
        d[c] = a[l];
    }
}

void realModify(int val, int l1, int r1, int id) {
    d[id] = val;
    int mid = (l1 + r1) / 2;
    if (l1 != r1) {
        realModify(val, l1, mid, id*2);
        realModify(val, mid+1, r1, id*2+1);
    }
}

void modify(int l, int r, int val, int l1, int r1, int id) {
    if (l <= l1 && r >= r1) {
        realModify(val, l1, r1, id);
        return;
    }
    int mid = (l1 + r1) / 2;
    if (l <= mid)
        modify(l, r, val, l1, mid, id*2);
    if (r > mid)
        modify(l, r, val, mid+1, r1, id*2+1);
    d[id] = max(d[id*2], d[id*2+1]);
}

void realAdd(int val, int l1, int r1, int id) {
    d[id] += val;
    int mid = (l1 + r1) / 2;
    if (l1 != r1) {
        realAdd(val, l1, mid, id*2);
        realAdd(val, mid+1, r1, id*2+1);
    }
}

void add(int l, int r, int val, int l1, int r1, int id) {
    if (l <= l1 && r >= r1) {
        realAdd(val, l1, r1, id);
        return;
    }
    int mid = (l1 + r1) / 2;
    if (l <= mid)
        add(l, r, val, l1, mid, id*2);
    if (r > mid)
        add(l, r, val, mid+1, r1, id*2+1);
    d[id] = max(d[id*2], d[id*2+1]);
}

int query(int l, int r, int l1, int r1, int id) {
    if (l <= l1 && r >= r1)
        return d[id];
    int mid = (l1 + r1) / 2, mx1 = -inf, mx2 = -inf;
    if (l <= mid)
        mx1 = query(l, r, l1, mid, id*2);
    if (r > mid)
        mx2 = query(l, r, mid+1, r1, id*2+1);
    return max(mx1, mx2);
}

int main() {
    cin.tie(nullptr), ios::sync_with_stdio(0);
    int n, q;
    cin >> n >> q;
    for (int i=1; i<=n; i++) cin >> a[i];
    createTree(1, n, 1);
    while (q--) {
        int op, l, r, x;
        cin >> op;
        switch (op) {
            case 1:
                cin >> l >> r >> x;
                modify(l, r, x, 1, n, 1);
                break;
            case 2:
                cin >> l >> r >> x;
                add(l, r, x, 1, n, 1);
                break;
            case 3:
                cin >> l >> r;
                cout << query(l, r, 1, n, 1) << endl;
        }
    }
    return 0;
}

by light_searcher @ 2024-11-29 06:43:00

数组开小了,然后你这没用懒标记啊


by Amazing_llh @ 2024-11-29 21:40:00


|