求调线段树,已近吐了

P1253 扶苏的问题

saumtam @ 2023-11-13 20:46:00

rt,代码比较清奇

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int dd=1e6+5;
struct node{
    int l,r,d,add,mdd,da,bj;
}tree[dd*8+5];
int n,m,a[dd];
inline int read(){
    int x=0,f=0;char c=getchar();
    while(c<'0'||c>'9'){f|=c=='-';c=getchar();}
    while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
    return f?-x:x;
}
inline void write(int x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
inline void build(int p,int x,int y){
    tree[p].l=x,tree[p].r=y;
    if(x==y){
        tree[p].d=a[x];
        tree[p].da=a[x];
        return;
    }
    int mid=(x+y)>>1;
    build(p<<1,x,mid);
    build(p<<1|1,mid+1,y);
    tree[p].d=tree[p<<1].d+tree[p<<1|1].d;
    tree[p].da=max(tree[p<<1].da,tree[p<<1|1].da);
}
inline void pushdown(int p){
    if(tree[p].add!=0){
        tree[p<<1].d+=tree[p].add*(tree[p<<1].r-tree[p<<1].l+1);
        tree[p<<1|1].d+=tree[p].add*(tree[p<<1|1].r-tree[p<<1|1].l+1);
        tree[p<<1].add+=tree[p].add;
        tree[p<<1|1].add+=tree[p].add;
        tree[p<<1].da+=tree[p].add;
        tree[p<<1|1].da+=tree[p].add;
        tree[p].add=0;
    }
    if(tree[p].bj!=0){
        tree[p<<1].d=(tree[p<<1].r-tree[p<<1].l+1)*tree[p].mdd;
        tree[p<<1|1].d=(tree[p<<1|1].r-tree[p<<1|1].l+1)*tree[p].mdd;
        tree[p<<1].mdd=tree[p].mdd;
        tree[p<<1|1].mdd=tree[p].mdd;
        tree[p<<1|1].da=tree[p].mdd;
        tree[p<<1].da=tree[p].mdd;
        tree[p<<1|1].add=0;
        tree[p<<1].add=0;
        tree[p].mdd=0;
        tree[p].bj=0;
        tree[p<<1].bj=1;
        tree[p<<1|1].bj=1;
    }
}
inline void change(int p,int x,int y,int z){
    if(tree[p].l>=x&&tree[p].r<=y){
        tree[p].add=0;
        tree[p].d=z*(tree[p].r-tree[p].l+1);
        tree[p].mdd=z;
        tree[p].da=z;
        tree[p].bj=1;
        return;
    }
    pushdown(p);
    int mid=(tree[p].l+tree[p].r)>>1;
    if(mid>=x) change(p<<1,x,y,z);
    if(mid<y) change(p<<1|1,x,y,z);
    tree[p].d=tree[p<<1].d+tree[p<<1|1].d;
    tree[p].da=max(tree[p<<1].da,tree[p<<1|1].da);
}
inline void change_add(int p,int x,int y,int z){
    if(tree[p].l>=x&&tree[p].r<=y){
        tree[p].add+=z;
        tree[p].d+=z*(tree[p].r-tree[p].l+1);
        tree[p].da+=z;
        return;
    }
    pushdown(p);
    int mid=(tree[p].l+tree[p].r)>>1;
    if(mid>=x) change_add(p<<1,x,y,z);
    if(mid<y) change_add(p<<1|1,x,y,z);
    tree[p].d=tree[p<<1].d+tree[p<<1|1].d;
    tree[p].da=max(tree[p<<1].da,tree[p<<1|1].da);
}
inline int ask(int p,int x,int y){
    if(tree[p].l>=x&&tree[p].r<=y) return tree[p].da;
    pushdown(p); 
    int mid=(tree[p].l+tree[p].r)>>1;
    int ans=-LLONG_MAX;
    if(mid>=x) ans=max(ans,ask(p<<1,x,y));
    if(mid<y) ans=max(ans,ask(p<<1|1,x,y));
    return ans;
}
signed main(){
    n=read(),m=read();
    for(int i=1;i<=n;i++) a[i]=read();
    build(1,1,n);
    while(m--){
        int opt,x,y,z;
        opt=read();
        if(opt==1){
            x=read(),y=read(),z=read();
            change(1,x,y,z);
        }
        else if(opt==2){
            x=read(),y=read(),z=read();
            change_add(1,x,y,z);
        }
        else{
            x=read(),y=read();
            write(ask(1,x,y));
            putchar('\n');
        }
    }
    return 0;
}

by 初星逝者 @ 2023-11-13 20:52:52

@saumtam 万一修改是 0 呢。


by saumtam @ 2023-11-13 20:58:38

@初星逝者 谢谢,可是我这个bj是记录的他能不能传递呀......


by 初星逝者 @ 2023-11-13 21:02:51

@saumtam 你维护区间和干嘛,题目有没必要求,你直接抄模板的?


by 初星逝者 @ 2023-11-13 21:04:08

@saumtam 而且你不应该先修改再加吗?


by saumtam @ 2023-11-13 21:11:39

@初星逝者 谢谢谢谢谢,关注了


by PRIMITIVE_LIGHTS @ 2023-11-13 21:12:18

逆天


by saumtam @ 2023-11-13 21:12:48

@qwq_volcano 你还逆天上了


by saumtam @ 2023-11-13 21:13:14

此贴结


|