q1uple @ 2022-11-02 17:14:59
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int a[maxn];
struct node{
int l,r,sum;
int lzy;
}tree[4*maxn];
void maketag(int u,int len,int k)//lazy-tag
{
tree[u].lzy+=k;
tree[u].sum+=len*k;
}
void push(int u)//修改
{
tree[u].sum=tree[u*2].sum+tree[u*2+1].sum;
}
void pushdown(int u,int l,int r)//下放
{
int mid=(l+r)/2;
maketag(u*2,mid-l+1,tree[u].lzy);
maketag(u*2+1,r-mid,tree[u].lzy);
tree[u].lzy=0;
}
void build(int u,int l,int r)//建树
{
if(l==r)
{
tree[u].sum=a[l];
tree[u].l=l,tree[u].r=r;
return;
}
tree[u].l=l,tree[u].r=r;
int mid=(l+r)/2;
build(u*2,l,mid);
build(u*2+1,mid+1,r);
push(u);
return;
}
int range(int u,int l,int r)//查询
{
if(tree[u].l>=l&&tree[u].r<=r)
return tree[u].sum;
if(tree[u].l>r||tree[u].r<l)
return 0;
pushdown(u,tree[u].l,tree[u].r);
return range(u*2,l,r)+range(u*2+1,l,r);
}
void update(int u,int l,int r,int k)//区间修改
{
if(tree[u].l>=l&&tree[u].r<=r)
maketag(u,tree[u].r-tree[u].l+1,k);
else if(tree[u].l>r||tree[u].r<l)
return ;
else
{
pushdown(u,tree[u].l,tree[u].l);
update(u*2,l,r,k);
update(u*2+1,l,r,k);
push(u);
}
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
build(1,1,n);
while(m--)
{
int ty;
cin>>ty;
if(ty==1)
{
int x,y,k;
cin>>x>>y>>k;
update(1,x,y,k);
}
if(ty==2)
{
int x,y;
cin>>x>>y;
cout<<range(1,x,y)<<'\n';
}
}
}
by 晴空一鹤 @ 2022-11-02 17:22:06
@q1u___ple
确定不开long long
吗?
by q1uple @ 2022-11-02 17:23:41
@晴空一鹤 我开了long long 后还是没过
by WaterSun @ 2022-11-02 17:49:04
@q1u___ple 你的pushdown错了
by WaterSun @ 2022-11-02 17:50:17
把:
maketag(u*2,mid-l+1,tree[u].lzy);
maketag(u*2+1,r-mid,tree[u].lzy);
改为:
maketag(u*2,tree[u*2].r-tree[u*2].l+1,tree[u].lzy);
maketag(u*2+1,tree[u*2+1].r-tr[u*2+1].l+1,tree[u].lzy);
by CQ_Alice @ 2022-11-02 17:50:22
@SYC0226 显然