跟一篇题解对比过,依然有俩测试点WA,什么原因
查看原帖
跟一篇题解对比过,依然有俩测试点WA,什么原因
1839893
durx楼主2025/8/29 12:33
#include<bits/stdc++.h>
using namespace std;
long long a[100002];
struct tree{
    int l,r;
    long long s,lazy;
}t[400002];
void build(int p,int l,int r){
    t[p].l=l;t[p].r=r;
    if(l==r){
        t[p].s=a[l];
        return;
    }
    int m=(l+r)>>1;
    build(2*p,l,m);
    build(2*p+1,m+1,r);
    t[p].s=t[2*p].s+t[2*p+1].s;
}
void tag(int p){
    if(t[p].lazy){
        t[2*p].s+=t[p].lazy*(t[2*p].r-t[2*p].l+1);
        t[2*p+1].s+=t[p].lazy*(t[2*p+1].r-t[2*p+1].l+1);
        t[2*p].lazy+=t[p].lazy;
        t[2*p+1].lazy+=t[p].lazy;
        t[p].lazy=0;
    }
}
void c(int p,int x,int y,int z){
    if(x<=t[p].l && y>=t[p].r){
        t[p].s+=(long long)z*(t[p].r-t[p].l+1);
        t[p].lazy+=z;
        return;
    }
    tag(p);
    int m=(t[p].l+t[p].r)>>1;
    if(x<=m)c(2*p,x,y,z);
    if(y>m)c(2*p+1,x,y,z);
    t[p].s=t[2*p].s+t[2*p+1].s;
}
long long ask(int p,int x,int y){
    if(x<=t[p].l && y>=t[p].r)return t[p].s;
    tag(p);
    int m=(t[p].l+t[p].r)>>1;
    long long ans=0;
    if(x<=m)ans+=ask(2*p,x,y);
    if(y>m)ans+=ask(2*p+1,x,y);
    return ans;
}

int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    build(1,1,n);
    for(int i=1;i<=m;i++){
        int q,x,y,z;
        scanf("%d",&q);
        if(q==1){
            scanf("%d%d%d",&x,&y,&z);
            c(1,x,y,z);
        }
        else{
            scanf("%d%d",&x,&y);
            cout<<ask(1,x,y)<<endl;
        }
    }
    return 0;
}

求解答,改了几遍了,但最后还是18个点AC最后两个WA

2025/8/29 12:33
加载中...