求调,祝你rp++

P4513 小白逛公园

ko_no_lzx_da @ 2022-11-17 18:55:13

#include<iostream>
#include<cmath>
#include<map>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std;
struct node{
    int ls,rs,sum,ms;
}tree[1000000];
int read(){
    int x = 0, f = 1;
    char c = getchar();
    while(c < '0' || c > '9'){
        if(c == '-'){
            f = -1;
        }
        c = getchar();
    }
    while(c >= '0' && c <= '9'){
        x = x*10+c-'0';
        c = getchar();
    }
    return x*f;
}
void down(int n){
    tree[n].sum=tree[n*2].sum+tree[n*2+1].sum;
    tree[n].ls=max(tree[n*2].ls,tree[n*2].sum+tree[n*2+1].ls);
    tree[n].rs=max(tree[n*2+1].rs,tree[n*2+1].sum+tree[n*2].rs);
    tree[n].ms=max(max(tree[n*2].ms,tree[n*2+1].ms),tree[n*2].rs+tree[n*2+1].ls);
}
void build(int l,int r,int n){
    if(l==r){
        cin >>tree[n].sum;
        tree[n].ls=tree[n].ms=tree[n].rs=tree[n].sum;
        return;
    }
    int mid=(l+r)/2;
    build(l,mid,n*2);
    build(mid+1,r,n*2+1);
    down(n);
}
node find(int l,int r,int n,int L,int R){
    if(L<=l&&r<=R){
        return tree[n];
    }
    int mid=(l+r)/2;
    node v;
    if(R<=mid)v=find(l,mid,n*2,L,R);
    else if(L>mid)v=find(mid+1,r,n*2+1,L,R);
    else{
        node v1,v2;
        v1=find(l,mid,n*2,L,R);
        v2=find(mid+1,r,n*2+1,L,R);
        v.sum=v1.sum+v2.sum;
        v.ls=max(v1.ls,v1.sum+v2.ls);
        v.rs=max(v2.rs,v2.sum+v1.rs);
        v.ms=max(max(v1.ms,v2.ms),v1.rs+v2.ls);
    }
    down(n);
    return v;
}
void gai(int l,int r,int n,int N,int v){
    if(l==r==N){
        tree[n].ls=tree[n].ms=tree[n].rs=tree[n].sum=v;
        return;
    }
    int mid=(l+r)/2;
    if(mid<=N)gai(l,mid,n*2,N,v);
    if(N>mid)gai(mid+1,r,n*2+1,N,v);
    down(n);
}
int n,m;
int main(){
    n=read();
    m=read();
    build(1,n,1);
    for(int i=1;i<=m;i++){
        int t=read(),l=read(),r=read();
        if(t==1){
            cout <<find(1,n,1,l,r).ms<<endl;
        }else{
            gai(1,n,1,l,r);
        }
    }
    return 0;
}

by Jusc @ 2022-11-17 19:05:22

为什么写区间修改?


by Jusc @ 2022-11-17 19:06:03

不好意思,看错了


by bamboo12345 @ 2022-11-17 19:19:07

@ko_no_lzx_da 有可能 l>r


by bamboo12345 @ 2022-11-17 19:22:59

@ko_no_lzx_da 数组也开小了,线段树数组要4倍


|