Shadow_T @ 2024-10-25 19:10:50
https://www.luogu.com.cn/record/184781147 https://www.luogu.com.cn/record/184781102 同样的代码为什么开 o2 会 mle
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e3+10;
int tag[maxn*4],ans[maxn*4];
int a[maxn];
int f(int p,int l,int r,int x)
{
tag[p]+=x;
ans[p]+=(r-l+1)*x;
}
void push_down(int l,int r,int p)//把 tag 下放 区间和结点
{
int mid=(l+r)/2;
f(p*2,l,mid,tag[p]);
f(p*2+1,mid+1,r,tag[p]);
tag[p]=0;
}
void build(int l,int r,int Node)
{
if(l==r)
{
ans[Node]=a[l];
return;
}
int mid=(l+r)/2;
build(l,mid,Node*2);
build(mid+1,r,Node*2+1);
ans[Node]=ans[Node*2]+ans[Node*2+1];
}
void update(int l,int r,int l1,int r1,int Node,int x)
{
if(l1<=l&&r1>=r)
{
ans[Node]+=(r-l+1)*x;
tag[Node]+=x;
return;
}
push_down(l,r,Node);
int mid=(l+r)/2;
if(l1<=mid) update(l,mid,l1,r1,Node*2,x);
if(r1>mid) update(mid+1,r,l1,r1,Node*2+1,x);
ans[Node]=ans[Node*2]+ans[Node*2+1];
}
int query(int l,int r,int l1,int r1,int Node)
{
if(l>=l1&&r<=r1) return ans[Node];
push_down(l,r,Node);
int mid=(l+r)/2,ans=0;
if(l1<=mid) ans+=query(l,mid,l1,r1,Node*2);
if(r1>mid) ans+=query(mid+1,r,l1,r1,Node*2+1);
return ans;
}
signed main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>a[i];
build(1,n,1);
while(m--)
{
int op;
cin>>op;
if(op==1)
{
int x,y,k;
cin>>x>>y>>k;
update(1,n,x,y,1,k);
}
else
{
int x,y;
cin>>x>>y;
cout<<query(1,n,x,y,1)<<"\n";
}
}
}
by Brilliant11001 @ 2024-10-25 19:14:57
@Shadow_T
by Shadow_T @ 2024-10-25 19:15:36
@Brilliant11001 啊?tysm 已关