50分,后面5个WA

P1253 扶苏的问题

GXYZY @ 2023-11-15 22:34:50

rt,代码如下:

#include<bits/stdc++.h>
// #pragma GCC optimize(2)
// #define int long long
using namespace std;
const int inf=0x7fffffff;
int read(){
    int x=0,f=1;
    char c=getchar();
    while(c>'9'||c<'0'){
        if(c=='-') f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}
void write(int x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
    return ;
}
int n,q,a[1000006],op,x,y,z;
struct segtree{
    int lc,rc,mmax,addlazy,changelazy;
    bool ifchange;
}node[5000006];
void pushup(int st){
    node[st].mmax=max(node[st*2].mmax,node[st*2+1].mmax);
    return ;
}
void build(int st,int l,int r){
    node[st].lc=l;
    node[st].rc=r;
    node[st].ifchange=0;
    if(l==r){
        node[st].mmax=a[l];
        return ;
    }
    int md=(l+r)/2;
    build(st*2,l,md);
    build(st*2+1,md+1,r);
    pushup(st);
    return ;
}
void pushdown(int st){
    if(node[st].ifchange==1){
        node[st*2].changelazy=node[st].changelazy;
        node[st*2].addlazy=0;
        node[st*2].mmax=node[st].changelazy;
        node[st*2+1].changelazy=node[st].changelazy;
        node[st*2+1].addlazy=0;
        node[st*2+1].mmax=node[st].changelazy;
        node[st].changelazy=0;
        node[st].ifchange=0;
    }
    else if(node[st].addlazy!=0){
        if(node[st*2].ifchange==1){
            node[st*2].changelazy+=node[st].addlazy;
        }
        else{
            node[st*2].addlazy+=node[st].addlazy;
        }
        node[st*2].mmax+=node[st].addlazy;
        if(node[st*2+1].ifchange==1){
            node[st*2+1].changelazy+=node[st].addlazy;
        }
        else{
            node[st*2+1].addlazy+=node[st].addlazy;
        }
        node[st*2+1].mmax+=node[st].addlazy;
        node[st].addlazy=0;
    }
    return ;
}
void changeinterval(int st,int l,int r,int data){
    if(node[st].lc>r||node[st].rc<l) return ;
    if(node[st].lc>=l&&node[st].rc<=r){
        node[st].mmax=data;
        node[st].changelazy=data;
        node[st].addlazy=0;
        node[st].ifchange=1;
        return ;
    }
    pushdown(st);
    changeinterval(st*2,l,r,data);
    changeinterval(st*2+1,l,r,data);
    pushup(st);
    return ;
}
void addinterval(int st,int l,int r,int data){
    if(node[st].lc>r||node[st].rc<l) return ;
    if(node[st].lc>=l&&node[st].rc<=r){
        node[st].mmax+=data;
        if(node[st].ifchange==1){
            node[st].changelazy+=data;
        }
        else{
            node[st].addlazy+=data;
        }
        return ;
    }
    pushdown(st);
    addinterval(st*2,l,r,data);
    addinterval(st*2+1,l,r,data);
    pushup(st);
    return ;
}
int getmax(int st,int l,int r){
    if(node[st].lc>r||node[st].rc<l) return -inf;
    if(node[st].lc>=l&&node[st].rc<=r){
        return node[st].mmax;
    }
    pushdown(st);
    return max(getmax(st*2,l,r),getmax(st*2+1,l,r));
}
int main(){
    // freopen(".in","r",stdin);
    // freopen(".out","w",stdout);
    n=read();
    q=read();
    for(int i=1;i<=n;i++){
        a[i]=read();
    }
    build(1,1,n);
    for(int i=1;i<=q;i++){
        op=read();
        if(op==1){
            x=read();
            y=read();
            z=read();
            changeinterval(1,x,y,z);
        }
        if(op==2){
            x=read();
            y=read();
            z=read();
            addinterval(1,x,y,z);
        }
        if(op==3){
            x=read();
            y=read();
            write(getmax(1,x,y));
            putchar('\n');
        }
    }
    return 0;
}
/*
4 4
10 4 -3 -7
1 1 3 0
2 3 4 -4
1 2 4 -9
3 1 4
*/

by WsW_ @ 2023-11-15 22:44:16

@GXYZY long long


by cat_lover1 @ 2023-11-15 22:55:31

似乎不是long long的问题?


by GXYZY @ 2023-11-15 22:58:36

确实不是long long的问题


by GXYZY @ 2023-11-15 22:59:06

@cz_awa 你说的对


by cat_lover1 @ 2023-11-15 23:02:19

@GXYZY 我现在一通瞎改,又得回40分了,其余都是TLE


by Transparent @ 2023-11-15 23:29:30

@GXYZY 两个问题


by cat_lover1 @ 2023-11-16 10:10:24

@GXYZY 改出来了,的确有long long的问题。注释的部分即修改处

#include<bits/stdc++.h>
// #pragma GCC optimize(2)
// #define int long long
#define ll long long//
using namespace std;
const ll inf=1e18;//
int read(){
    int x=0,f=1;
    char c=getchar();
    while(c>'9'||c<'0'){
        if(c=='-') f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}
void write(ll x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
    return ;
}
int n,q,/*a[1000006],*/op,x,y,z;
struct segtree{
    int lc,rc;
    ll mmax,addlazy,changelazy;
    bool ifchange;
}node[5000006];
void pushup(int st){
    node[st].mmax=max(node[st*2].mmax,node[st*2+1].mmax);
    return ;
}
void build(int st,int l,int r){
    node[st].lc=l;
    node[st].rc=r;
    if(l==r){
        node[st].mmax=read();//
        return ;
    }
    int md=(l+r)/2;
    build(st*2,l,md);
    build(st*2+1,md+1,r);
    pushup(st);
    return ;
}
void pushdown(int st){
    if(node[st].ifchange==1){
        node[st*2].changelazy=node[st].changelazy;
        node[st*2].addlazy=0;
        node[st*2].mmax=node[st].changelazy;
        node[st*2+1].changelazy=node[st].changelazy;
        node[st*2+1].addlazy=0;
        node[st*2+1].mmax=node[st].changelazy;
        node[st*2].ifchange=node[st*2+1].ifchange=1;//
        //node[st].changelazy=0;
        node[st].ifchange=0;
    }
    else if(node[st].addlazy!=0){
        if(node[st*2].ifchange==1){
            node[st*2].changelazy+=node[st].addlazy;
        }
        else{
            node[st*2].addlazy+=node[st].addlazy;
        }
        node[st*2].mmax+=node[st].addlazy;
        if(node[st*2+1].ifchange==1){
            node[st*2+1].changelazy+=node[st].addlazy;
        }
        else{
            node[st*2+1].addlazy+=node[st].addlazy;
        }
        node[st*2+1].mmax+=node[st].addlazy;
        node[st].addlazy=0;
    }
    return ;
}
void changeinterval(int st,int l,int r,int data){
    if(node[st].lc>r||node[st].rc<l) return ;
    if(node[st].lc>=l&&node[st].rc<=r){
        node[st].mmax=data;
        node[st].changelazy=data;
        node[st].addlazy=0;
        node[st].ifchange=1;
        return ;
    }
    pushdown(st);
    changeinterval(st*2,l,r,data);
    changeinterval(st*2+1,l,r,data);
    pushup(st);
    return ;
}
void addinterval(int st,int l,int r,int data){
    if(node[st].lc>r||node[st].rc<l) return ;
    if(node[st].lc>=l&&node[st].rc<=r){
        node[st].mmax+=data;
        if(node[st].ifchange==1){
            node[st].changelazy+=data;
        }
        else{
            node[st].addlazy+=data;
        }
        return ;
    }
    pushdown(st);
    addinterval(st*2,l,r,data);
    addinterval(st*2+1,l,r,data);
    pushup(st);
    return ;
}
ll getmax(int st,int l,int r){
    if(node[st].lc>r||node[st].rc<l) return -inf;
    if(node[st].lc>=l&&node[st].rc<=r){
        return node[st].mmax;
    }
    pushdown(st);
    return max(getmax(st*2,l,r),getmax(st*2+1,l,r));
}
int main(){
    // freopen(".in","r",stdin);
    // freopen(".out","w",stdout);
    n=read();
    q=read();
    //for(int i=1;i<=n;i++){
        //a[i]=read();
    //}
    build(1,1,n);
    while(q--){
        op=read();x=read();y=read();
        if(op==1)changeinterval(1,x,y,read());//注:函数中只能有最多1个read,出现多了就wa了

        else if(op==2)addinterval(1,x,y,read());

        else{
            write(getmax(1,x,y));
            putchar('\n');
        }
    }
    return 0;
}

by GXYZY @ 2023-11-16 22:41:28

@Transparent @cz_awa 已经解决了,太感谢了


|