// using segmented tree with lazy update
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;
long long vec[400001];
long long lazy[400001];
long long retreive( long long id , long long i , long long j , long long s , long long e)
{
if(i > j || s > j || e < i )
return 0;
if(s <= i && e >= j )
return vec[id];
else
{
long long mid = (i+j)/2;
vec[2*id+1]+=(mid-i+1)*lazy[id];
vec[2*id+2]+=(j-mid)*lazy[id];
lazy[2*id+1]+=lazy[id];
lazy[2*id+2]+=lazy[id];
lazy[id]=0;
return retreive(2*id+1,i,mid,s,e)+retreive(2*id+2,mid+1,j,s,e);
}
}
void update( long long id , long long i , long long j , long long s , long long e , long long val )
{
if(i > j || e < i || s > j )
return;
if(s <= i && e >= j )
{
vec[id]+=(j-i+1)*val;
lazy[id]+=val;
}
else {
long long mid = (i+j)/2;
vec[2*id+1]+=(mid-i+1)*lazy[id];
vec[2*id+2]+=(j-mid)*lazy[id];
lazy[2*id+1]+=lazy[id];
lazy[2*id+2]+=lazy[id];
lazy[id]=0;
update(2*id+1,i,mid,s,e,val);
update(2*id+2,mid+1,j,s,e,val);
vec[id]=vec[2*id+1]+vec[2*id+2];
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
memset(lazy,0,sizeof lazy);
memset(vec,0,sizeof vec);
long long n , c;
scanf("%lld%lld",&n,&c);
while(c--)
{
long long ch , p , q , v;
scanf("%lld%lld%lld",&ch,&p,&q);
if(ch==0)
{
scanf("%lld",&v);
update(0,0,n-1,p-1,q-1,v);
}
else {
long long ans = retreive( 0 , 0 , n-1, p-1, q-1);
printf("%lld\n",ans);
}
}
}
return 0;
}
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;
long long vec[400001];
long long lazy[400001];
long long retreive( long long id , long long i , long long j , long long s , long long e)
{
if(i > j || s > j || e < i )
return 0;
if(s <= i && e >= j )
return vec[id];
else
{
long long mid = (i+j)/2;
vec[2*id+1]+=(mid-i+1)*lazy[id];
vec[2*id+2]+=(j-mid)*lazy[id];
lazy[2*id+1]+=lazy[id];
lazy[2*id+2]+=lazy[id];
lazy[id]=0;
return retreive(2*id+1,i,mid,s,e)+retreive(2*id+2,mid+1,j,s,e);
}
}
void update( long long id , long long i , long long j , long long s , long long e , long long val )
{
if(i > j || e < i || s > j )
return;
if(s <= i && e >= j )
{
vec[id]+=(j-i+1)*val;
lazy[id]+=val;
}
else {
long long mid = (i+j)/2;
vec[2*id+1]+=(mid-i+1)*lazy[id];
vec[2*id+2]+=(j-mid)*lazy[id];
lazy[2*id+1]+=lazy[id];
lazy[2*id+2]+=lazy[id];
lazy[id]=0;
update(2*id+1,i,mid,s,e,val);
update(2*id+2,mid+1,j,s,e,val);
vec[id]=vec[2*id+1]+vec[2*id+2];
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
memset(lazy,0,sizeof lazy);
memset(vec,0,sizeof vec);
long long n , c;
scanf("%lld%lld",&n,&c);
while(c--)
{
long long ch , p , q , v;
scanf("%lld%lld%lld",&ch,&p,&q);
if(ch==0)
{
scanf("%lld",&v);
update(0,0,n-1,p-1,q-1,v);
}
else {
long long ans = retreive( 0 , 0 , n-1, p-1, q-1);
printf("%lld\n",ans);
}
}
}
return 0;
}
No comments:
Post a Comment