MnZn10分求助

P3372 【模板】线段树 1

Svemit @ 2022-11-27 19:37:01

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int n,q;
int a[N];
struct segment_tree
{
    int l,r,val,laz_tag;
}t[N<<2];

inline void push_up(int x)
{
    t[x].val=t[x<<1].val+t[x<<1|1].val;
}

inline void push_down(int x)
{
    if(!t[x].laz_tag) return;

    t[x<<1].val+=(t[x<<1].r-t[x<<1].l+1)*t[x].laz_tag;
    t[x<<1|1].val+=(t[x<<1|1].r-t[x<<1|1].l)*t[x].laz_tag;

    t[x<<1].laz_tag+=t[x].laz_tag;
    t[x<<1|1].laz_tag+=t[x].laz_tag;

    t[x].laz_tag=0;
}

inline void build(int l,int r,int x)
{
    t[x].l=l;
    t[x].r=r;
    if(l==r)
    {
        t[x].val=a[l];
        return;
    }
    int mid=l+r>>1;
    build(l,mid,x<<1);
    build(mid+1,r,x<<1|1);
    push_up(x);
}

inline void update(int nl,int nr,int k,int l,int r,int x)
{
    if(nl<=l&&r<=nr)
    {
        t[x].laz_tag+=k;
        t[x].val+=(r-l+1)*k;
        return;
    }
    push_down(x);
    int mid=l+r>>1;
    if(nl<=mid) update(nl,nr,k,l,mid,x<<1);
    if(nr>mid) update(nl,nr,k,mid+1,r,x<<1|1);
    push_up(x);
}

inline int query(int nl,int nr,int l,int r,int x)
{
    if(nl<=l&&r<=nr)
    {
        return t[x].val;
    }
    push_down(x);
    int mid=l+r>>1,ans=0;
    if(nl<=mid) ans+=query(nl,nr,l,mid,x<<1);
    if(nr>mid) ans+=query(nl,nr,mid+1,r,x<<1|1);
    return ans;
}

signed main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);
    cin>>n>>q;
    for(int i=1;i<=n;i++)
      cin>>a[i];
    build(1,n,1);
    while(q--)
    {
        int op;
        cin>>op;
        if(op==1)
        {
            int l,r,k;
            cin>>l>>r>>k;
            update(l,r,k,1,n,1);
        }
        if(op==2)
        {
            int l,r;
            cin>>l>>r;
            cout<<query(l,r,1,n,1)<<endl;
        }
    }
    return 0;
}

|