Cainiao_Chen @ 2024-12-27 20:01:59
代码70分,求调 :(
#include<bits/stdc++.h>
using namespace std;
int n,m,a[100005];
struct segtree{
int l,r;
int k,sum,add;
}arr[400005];
void build(int p,int l,int r)
{
arr[p].l=l;
arr[p].r=r;
if(l==r)
{
arr[p].k=a[l];
return;
}
int mid=l+r>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
arr[p].k=arr[p<<1].k+arr[p<<1|1].k;
}
void push_down(int p){
if(arr[p].add)
{
arr[p<<1].k+=arr[p].add*(arr[p<<1].r-arr[p<<1].l+1);
arr[p<<1|1].k+=arr[p].add*(arr[p<<1|1].r-arr[p<<1|1].l+1);
arr[p<<1].add+=arr[p].add;
arr[p<<1|1].add+=arr[p].add;
arr[p].add=0;
}
}
void add(int p,int x,int y,int z)
{
if(x<=arr[p].l&&y>=arr[p].r)
{
arr[p].k+=(long long)z*(arr[p].r-arr[p].l+1);
arr[p].add+=z;
return;
}
push_down(p);
int mid=(arr[p].l+arr[p].r)>>1;
if(x<=mid)
add(p<<1,x,y,z);
if(y>mid)
add(p<<1|1,x,y,z);
arr[p].k=arr[p<<1].k+arr[p<<1|1].k;
}
long long ask(int p,int x,int y)
{
if(x<=arr[p].l&&y>=arr[p].r)
return arr[p].k;
push_down(p);
int mid=arr[p].l+arr[p].r>>1;
long long ans=0;
if(x<=mid)
ans+=ask(p<<1,x,y);
if(y>mid)
ans+=ask(p<<1|1,x,y);
return ans;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
build(1,1,n);
while(m--)
{
char opt;
int x,y;
cin>>opt>>x>>y;
if(opt=='1')
{
int k;
cin>>k;
add(1,x,y,k);
}
else
cout<<ask(1,x,y)<<endl;
}
return 0;
}
by pika_ @ 2024-12-27 20:13:59
sum要开long long哦!~