70pts求调

P3372 【模板】线段树 1

学习柯学 @ 2023-07-21 16:51:09

#include<bits/stdc++.h>
using namespace std;
struct tr{
    int l;
    int r;
    int num;
};
tr tree[10000005];
long long i, j, n, a[1000005], m, opt, mark[100000005], x, y, k;
void spread(long long p)
{
    if(mark[p])
    {
        tree[p*2].num+=(tree[p*2].r-tree[p*2].l+1)*mark[p];
        tree[p*2+1].num+=(tree[p*2+1].r-tree[p*2+1].l+1)*mark[p];
        mark[p*2]+=mark[p];
        mark[p*2+1]+=mark[p];
        mark[p]=0;
    }
    else return ;
}
int build(long l, long r, long p){
    tree[p].l=l,tree[p].r=r;
    if(l==r)
    {
        tree[p].num=a[l];
    }
    else {
        long long mid=(l+r)/2;
        tree[p].num=build(l,mid,p*2)+build(mid+1,r,p*2+1);
    }
    return tree[p].num;
}
void add(int p)
{
    if(tree[p].l>=x&&tree[p].r<=y)
    {
        tree[p].num+=k*(tree[p].r-tree[p].l+1);
        mark[p]+=k;
        return ;
    }
    else{
        spread(p);
        long long mid=(tree[p].l+tree[p].r)/2;
        if(x<=mid) add(p*2);
        if(y>mid) add(p*2+1);
        tree[p].num=tree[p*2].num+tree[p*2+1].num;
    }
}
int sum(long long p)
{
    if(x<=tree[p].l&&y>=tree[p].r)
    {
        return tree[p].num;
    }
    else{
        spread(p);
        long long ans = 0;
        long long mid=(tree[p].l+tree[p].r)/2;
        if(x<=mid) ans+=sum(p*2);
        if(y>mid) ans+=sum(p*2+1);
        return ans;
        //if(x<=mid&&y>mid) return sum(p*2)+sum(p*2+1);
        //if(x<=mid&&y<=mid) return sum(p*2);
        //if(x>mid&&y>mid) return sum(p*2+1);
    }
}
int main()
{
    cin>>n>>m;
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    build(1,n,1);
    for(i=1;i<=m;i++)
    {
        cin>>opt>>x>>y;
        if(opt==1)
        {
            cin>>k;
            add(1);
        }
        else{
            cout<<sum(1)<<endl;
        }
    }
    return 0;
}

by justalearner @ 2023-07-21 17:12:43

和没加long long反倒是下标加了long long...


|