本地对拍都是对的,提交全WA

P3372 【模板】线段树 1

nahidaa @ 2024-10-03 12:06:35

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll nq,n,m,x,y,val,ka_len,a[100005],ans;
struct kuai{
    ll l,r,val,lazy;
}ka[500];
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;++i){
        cin>>a[i];
    }
    nq=sqrt(n);
    ka_len=nq;
    for(int i=1;i<=ka_len;++i){
        ka[i].l=(i-1)*nq+1,ka[i].r=i*nq;
        for(int j=ka[i].l;j<=ka[i].r;++j){
            ka[i].val+=a[j];
        }
    }
    if(ka[nq].r<n){
        ka[++ka_len].l=(ka_len-1)*nq+1,ka[ka_len].r=n;
        for(int j=ka[ka_len].l;j<=ka[ka_len].r;++j){
            ka[ka_len].val+=a[j];
        }
    }
    for(int i=1;i<=m;++i){
        cin>>x;
        if(x==1){
            cin>>x>>y>>val;
            for(int j=1;j<=ka_len;++j){
                if(ka[j].l>=x&&ka[j].r<=y){
                    ka[j].lazy+=val;
                }else if(ka[j].l<=x||ka[j].r>=y){
                    int L=max(ka[j].l,x),R=min(ka[j].r,y);
                    for(int k=L;k<=R;++k){
                        a[k]+=val;
                        ka[j].val+=val;
                    }
                }
            }
        }else{
            cin>>x>>y;
            ans=0;
            for(int j=1;j<=ka_len;++j){
                if(ka[j].l>=x&&ka[j].r<=y){
                    ans+=ka[j].val+(ka[j].r-ka[j].l+1)*ka[j].lazy;
                }else if(ka[j].l<=x||ka[j].r>=y){
                    int L=max(ka[j].l,x),R=min(ka[j].r,y);
                    for(int k=L;k<=R;++k){
                        ans+=a[k];
                        ans+=ka[j].lazy;
                    }
                }
            }
            cout<<ans<<"\n";
        }
    }
}

by nahidaa @ 2024-10-03 12:08:08

这是 提交记录


by nahidaa @ 2024-10-03 12:09:05

我把第一个数据点下载下来了,本地输出跟答案相同


by dongzirui0817 @ 2024-10-03 12:18:09

@nahidaa 这连样例也没过耶……


by _LFC_ @ 2024-10-03 12:27:56

线段树不递归,该罚


by _LFC_ @ 2024-10-03 12:31:06

@nahidaa 14行 nq=sqrt(n) 改成 nq=log2(n) 能过三个点(逃


by SuperCowHorse @ 2024-10-03 13:29:08

@nahidaa 35行ka[j].val没更新,,


by nahidaa @ 2024-10-03 15:11:07

@dongzirui0817


by nahidaa @ 2024-10-03 15:15:08

这是数据1的本地运行结果,和答案是符合的


by dongzirui0817 @ 2024-10-03 16:59:22

@nahidaa 啊?


by nahidaa @ 2024-10-03 17:08:18

@dongzirui0817 那我本地C++有问题啊?!这个该怎么解决啊(从来没见过这么离谱的事情)


| 下一页