10分求调,只A了第一个点qwq

P1253 扶苏的问题

6fenzhipai2 @ 2023-12-01 11:12:16

# include<iostream>
# include<algorithm>
# include<cmath>
# define int long long
# define endl "\n"
using namespace std;
const int maxn=100001;
struct Node{
    int l, r, mmax, add, change;    
}tree[maxn*100];
int n, m, a[maxn];
void build_tree(int i, int l, int r) {
    tree[i].l=l, tree[i].r=r;
    if(l==r) {
        tree[i].mmax=a[l];
        return ;
    }
    int mid=(l+r)/2;
    build_tree(i*2, l, mid);
    build_tree(i*2+1, mid+1, r); 
    tree[i].mmax=max(tree[i*2].mmax, tree[i*2+1].mmax);
}
void push_down(int i) {
    if(tree[i].add || tree[i].change) {
        int l=tree[i].l, r=tree[i].r;
        tree[i*2].mmax=tree[i].change+tree[i].add;
        tree[i*2+1].mmax=tree[i].change+tree[i].add;
        tree[i*2].add=tree[i].add;
        tree[i*2+1].add=tree[i].add;
        tree[i*2].change=tree[i].change;
        tree[i*2+1].change=tree[i].change;
        tree[i].add=0; tree[i].change=0;
    }
}
void change(int i, int l, int r, int x) {
    if(tree[i].l>=l && tree[i].r<=r) {
        tree[i].mmax=x;
        tree[i].change=x;
        tree[i].add=0;
        return ;
    }
    push_down(i); 
    int mid=(tree[i].l+tree[i].r)/2;
    if(l<=mid) change(2*i, l, r, x);
    if(r>=mid+1) change(2*i+1, l, r, x);
    tree[i].mmax=max(tree[i*2].mmax, tree[i*2+1].mmax);
}
void add(int i, int l, int r, int x) {
    if(tree[i].l>=l && tree[i].r<=r) {
        tree[i].mmax+=x;
        tree[i].add+=x;
        return ;
    }
    push_down(i); 
    int mid=(tree[i].l+tree[i].r)/2;
    if(l<=mid) add(2*i, l, r, x);
    if(r>=mid+1) add(2*i+1, l, r, x);
    tree[i].mmax=max(tree[i*2].mmax, tree[i*2+1].mmax);
}
int query(int i, int l, int r) {
    if(tree[i].l>=l && tree[i].r<=r) {
        return tree[i].mmax;
    }
    push_down(i); 
    int mid=(tree[i].l+tree[i].r)/2, sum=0;
    if(l<=mid) sum=max(sum, query(2*i, l, r));
    if(r>=mid+1) sum=max(sum, query(2*i+1, l, r));
    return sum;
}
signed main() {
    cin >> n >> m;
    for(int i=1; i<=n; i++) cin >> a[i];
    build_tree(1, 1, n);
    while(m--) {
        int op; cin >> op;
        if(op==1) {
            int l, r, x; cin >> l >> r >> x;
            change(1, l, r, x);
        } else if(op==2) {
            int l, r, x; cin >> l >> r >> x;
            add(1, l, r, x);
        } else {
            int l, r, x; cin >> l >> r;
            cout << query(1, l, r) << endl;
        }
    }
    return 0;
}

跪谢大佬qwq


|