求助

P4513 小白逛公园

Henly_Z @ 2023-09-24 12:15:29

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1000000 + 10;

struct Node {
    ll sum, max , lmax , rmax;
};

Node sgt[N*4];
int a[N];
Node merge(Node n1 , Node n2){
    Node nd;
    nd.sum = n1.sum + n2.sum;
    nd.max = max({n1.max , n2.max , n1.rmax + n2.lmax});
    nd.lmax = max(n1.lmax , n1.sum + n2.lmax);
    nd.rmax = max(n2.rmax , n2.sum + n1.rmax);
    return nd;
}
void push_up(int index) {
    sgt[index] = merge(sgt[index * 2] , sgt[index * 2 + 1]);
}

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

void update(int index , int begin,int end,int p,int s) {
    if (begin == end) {
        sgt[index].lmax = sgt[index].rmax = sgt[index].max = sgt[index].sum = s;
        return;
    }   
    int mid = (begin + end) / 2;
    if (p <= mid) update(index * 2 , begin , mid , p , s);
    else  update(index * 2 + 1,mid + 1,end, p , s);
    push_up(index);
}

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

int main() {
    int n, q, op, l, r, x;
    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", &l, &r);
            update(1,1,n,l,r);
        }
        else {
            scanf("%d %d", &l, &r);
            if(l > r) swap(l , r);
            printf("%lld\n", getsum(1,1,n,l,r).max);
        }
    }
    return 0;
}

在昨天的基础上有了修改 但是不对


by BensonChen @ 2023-10-02 11:20:36

你打的是个啥玩意 看不懂


|