求助

P4513 小白逛公园

Henly_Z @ 2023-09-23 19:15:15

#include <bits/stdc++.h>

using namespace std;

const int N = 1000000 + 10; 

long long sgt[N*4];//线段树 
int a[N];

void build(int index, int begin, int end) {
    if (begin == end) {
        sgt[index] = a[begin];
        return;
    }
    int mid = (begin + end) / 2;
    build(index * 2, begin, mid);
    build(index * 2 + 1, mid + 1, end);
    sgt[index] = max(sgt[index*2] , sgt[index*2+1]);
}

void update(int index, int begin, int end, int i, int x) {
    if (begin == end){
        sgt[index] += x;
        return;
    } 
    int mid = (begin + end) / 2;
    if (i <= mid) update(index * 2, begin, mid, i, x);
    else update(index * 2 + 1, mid + 1, end, i, x);
    sgt[index] = max(sgt[index*2] , sgt[index*2+1]);
}

long long query(int index, int begin, int end, int left, int right) {
    if (begin == left && end == right) return sgt[index];
    int mid = (begin + end) / 2;
    if (right <= mid) return query(index * 2, begin, mid, left, right);
    else if (left > mid) return query(index * 2 + 1, mid + 1, end, left, right);
    else return max(query(index * 2, begin, mid, left, mid) , query(index * 2 + 1, mid + 1, end, mid + 1, right));
}

int main() {
    int n, q, op, i, x, l, r;
    scanf("%d %d", &n, &q);
    for (int i=1; i<=n; i++) scanf("%d", &a[i]);
    build(1, 1, n); 
    while (q --) {
        scanf("%d", &op);
        if (op == 2) {
            scanf("%d %d", &i, &x);
            update(1, 1, n, i, x - a[i]);
            a[i] = x;
        }
        else {
            scanf("%d %d", &l, &r);
            printf("%lld\n", query(1, 1, n, l, r));
        }
    }
    return 0;
}

求助,只过了一个点


by S0CRiA @ 2023-09-23 19:22:24

query第一行错了


by Henly_Z @ 2023-09-23 19:53:54

@AiRC0S 请问错哪了?


by S0CRiA @ 2023-09-23 20:20:34

@zhl_2010 哦你的写法好想和我的有点不一样,


|