50pts,求调
查看原帖
50pts,求调
643180
fengenrong楼主2023/11/25 11:21
#include<bits/stdc++.h>
#define int long long
#define left_son p*2
#define right_son (p*2)+1
using namespace std;
int n,m;
int a[10000005];
struct op{
    int max,l,r,lazy,lazy1,lazy_yao;
}tree[10000005];
void build_tree(int p,int l,int r){
    if(l==r){
        tree[p].l=l,tree[p].r=r;
        tree[p].max=a[l];
        return;
    }
    int mid=(l+r)>>1;
    build_tree(left_son,l,mid);
    build_tree(right_son,mid+1,r);
    tree[p].l=l,tree[p].r=r;
    tree[p].max=max(tree[left_son].max,tree[right_son].max);
}
void pushdown(int p){
    tree[left_son].lazy+=tree[p].lazy;
    tree[right_son].lazy+=tree[p].lazy;
    tree[left_son].max+=tree[p].lazy;
    tree[right_son].max+=tree[p].lazy;
    tree[p].lazy=0;
}
void pushdown1(int p){
    tree[left_son].lazy1=tree[p].lazy1;
    tree[right_son].lazy1=tree[p].lazy1;
    tree[left_son].lazy_yao=1;
    tree[right_son].lazy_yao=1;
    tree[left_son].max=tree[p].lazy1;
    tree[right_son].max=tree[p].lazy1;
    tree[p].lazy_yao=0;
    tree[p].lazy1=0;
}
void change(int p,int l,int r,int x){
    if(l<=tree[p].l&&tree[p].r<=r){
        tree[p].max=x;
        tree[p].lazy=0;
        tree[p].lazy_yao=1;
        // if(tree[p].lazy1!=0) pushdown1(p);
        tree[p].lazy1=x;
        return;
    }
    if(tree[p].lazy!=0) pushdown(p);
    if(tree[p].lazy_yao==1) pushdown1(p);
    if(tree[left_son].r>=l)
		change(left_son,l,r,x);
	if(tree[right_son].l<=r)
		change(right_son,l,r,x);
    tree[p].max=max(tree[left_son].max,tree[right_son].max);
}
void change1(int p,int l,int r,int x){
    if(l<=tree[p].l&&tree[p].r<=r){
        tree[p].max+=x;
        tree[p].lazy+=x;
        return;
    }
    if(tree[p].lazy!=0) pushdown(p);
    if((tree[p].lazy_yao==1)) pushdown1(p);
    if(tree[left_son].r>=l)
		change1(left_son,l,r,x);
	if(tree[right_son].l<=r)
		change1(right_son,l,r,x);
    tree[p].max=max(tree[left_son].max,tree[right_son].max);
}
int ask(int p,int l,int r){
    if(l<=tree[p].l&&tree[p].r<=r){
        return tree[p].max;
    }
    if(tree[p].lazy!=0) pushdown(p);
    if((tree[p].lazy_yao==1)) pushdown1(p);
    int maxa=INT_MIN;
    if(tree[left_son].r>=l)
		maxa=max(maxa,ask(left_son,l,r));
	if(tree[right_son].l<=r)
		maxa=max(maxa,ask(right_son,l,r));
    return maxa;
}
signed main(){
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
    }
    build_tree(1,1,n);
    while(m--){
        int op;
        scanf("%lld",&op);
        if(op==1){
            int l,r,x;
            scanf("%lld%lld%lld",&l,&r,&x);
            change(1,l,r,x);
        }
        else if(op==2){
            int l,r,x;
            scanf("%lld%lld%lld",&l,&r,&x);
            change1(1,l,r,x);
        }
        else{
            int l,r;
            scanf("%lld%lld",&l,&r);
            printf("%lld\n",ask(1,l,r));
        }
    }
    return 0;
}
2023/11/25 11:21
加载中...