跳到正文

剪贴板

剪贴板 dwrlzaun

原文
剪贴板信息

剪贴板信息

收录
更新

https://www.luogu.com.cn/record/269332848

C++
#include<bits/stdc++.h>
#define For(i,j,k) for(int i=(j);i<=(k);i++)
#define dFor(i,j,k) for(int i=(j);i>=(k);i--)
using namespace std;
#define MAXN 100005
#define MAXM 100005
int n,m;
long long a[MAXN];
struct Que{
    int op,l,r;
    long long k;
}q[MAXM];
long long ans[MAXM];
struct Range{
    int l,r;
};
vector<Range> seg[MAXM*4];
int mid[MAXM*4];
vector<int> id[MAXM*4];
int tot;
long long sum[MAXM*64],lzy[MAXM*64];
int pool[MAXM*128],*top;
int len[MAXM*64],*st[MAXM*64],deg[MAXM*64];
int New(int l,int r){
    tot++;
    len[tot]=r-l+1;
    return tot;
}
void update(int c,int ls,int rs){
    int i=0,j=0;
    while(i<seg[ls].size()&&j<seg[rs].size()){
        int l=max(seg[ls][i].l,seg[rs][j].l);
        int r=min(seg[ls][i].r,seg[rs][j].r);
        if(l<=r) seg[c].push_back({l,r});
        if(seg[ls][i].r<seg[rs][j].r) i++;
        else if(seg[ls][i].r>seg[rs][j].r) j++;
        else i++,j++;
    }
}
void link(int c,int son){
    static int mp[MAXM*2],cnt[MAXM*2];
    int j=0;
    For(i,0,seg[c].size()-1){
        while(seg[son][j].l>seg[c][i].l||seg[son][j].r<seg[c][i].r){
            j++;
        }
        mp[i]=j;
        cnt[j]++;
    }
    For(i,0,seg[son].size()-1){
        int x=id[son][i];
        st[x]=top;
        deg[x]=cnt[i];
        top+=cnt[i];
        cnt[i]=0;
    }
    For(i,0,seg[c].size()-1){
        int j=mp[i],x=id[son][j];
        st[x][cnt[j]++]=id[c][i];
    }
}
void build(int c,int L,int R){
    if(L==R){
        int l=q[L].l,r=q[L].r;
        if(l>1) seg[c].push_back({1,l-1});
        mid[c]=seg[c].size();
        seg[c].push_back({l,r});
        if(r<n) seg[c].push_back({r+1,n});
        for(auto [l,r]:seg[c]){
            id[c].push_back(New(l,r));
        }
        return ;
    }
    int mid=(L+R)>>1;
    build(c*2,L,mid);
    build(c*2+1,mid+1,R);
    update(c,c*2,c*2+1);
    for(auto [l,r]:seg[c]){
        id[c].push_back(New(l,r));
    }
    link(c,c*2);
    link(c,c*2+1);
}
void build_root(){
    For(i,0,seg[1].size()-1){
        int x=id[1][i];
        int l=seg[1][i].l,r=seg[1][i].r;
        st[x]=top;
        deg[x]=r-l+1;
        top+=deg[x];
        int *p=st[x];
        For(pos,l,r){
            *(p++)=pos;
        }
    }
}
void solve(int c,int L,int R,bool b){
    for(int x:id[c]){
        sum[x]=0;
        For(i,0,deg[x]-1){
            sum[x]+=sum[st[x][i]];
        }
    }
    if(L==R){
        int x=id[c][mid[c]];
        if(q[L].op==1){
            lzy[x]+=q[L].k;
            sum[x]+=q[L].k*len[x];
        }else{
            ans[L]=sum[x];
        }
    }else{
        int mid=(L+R)>>1;
        solve(c*2,L,mid,1);
        solve(c*2+1,mid+1,R,1);
    }
    for(int x:id[c]){
        if(lzy[x]==0) continue;
        For(i,0,deg[x]-1){
            int y=st[x][i];
            if(b) lzy[y]+=lzy[x];
            sum[y]+=lzy[x]*len[y];
        }
        lzy[x]=0;
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>m;
    For(i,1,n){
        cin>>a[i];
        sum[i]=a[i];
        len[i]=1;
    }
    For(i,1,m){
        cin>>q[i].op>>q[i].l>>q[i].r;
        if(q[i].op==1){
            cin>>q[i].k;
        }
    }
    top=pool;tot=n;
    build(1,1,m);
    build_root();
    solve(1,1,m,0);
    For(i,1,m){
        if(q[i].op==2){
            cout<<ans[i]<<'\n';
        }
    }
    return 0;
}