啊?

P3372 【模板】线段树 1

WydnksqhbD @ 2024-02-16 20:31:09

分块代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5,sq=1e3;
int n,m,len,cnt;
int a[N],id[N],charge[sq],sum[sq];
void query(int l,int r)
{
    int total=0;
    if(id[l]==id[r])for(int i=l;i<=r;i++)total+=a[i]+charge[id[i]];
    else
        for(int i=l;i<=r;i++)
            if(i%len==1&&i+len<=r)total+=sum[id[i]],i+=len-1;
            else total+=a[i]+charge[id[i]];
    printf("%lld\n",total);return;
}
void update(int l,int r,int k)
{
    if(id[l]==id[r]){for(int i=l;i<=r;i++)a[i]+=k;return;}
    for(int i=l;i<=r;i++)
        if(i%len==1&&i+len<=r)sum[id[i]]+=len*k,charge[id[i]]+=k,i+=len-1;
        else a[i]+=k,sum[id[i]]+=k;
    return;
}
signed main()
{
    scanf("%lld %lld",&n,&m);len=sqrt(n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        if(i%len==1)cnt++;id[i]=cnt;
    }
    while(m--)
    {
        int op,x,y;scanf("%lld %lld %lld",&op,&x,&y);
        if(op==1){int k;scanf("%lld",&k);update(x,y,k);}
        else query(x,y);
    }
    return 0;
}

样例正确,下载数据后也正确,但是你谷就是显示我全错,不知道啥情况,求条。


|