#include<bits/stdc++.h>
#define mx 100009
using namespace std;
typedef long long ll;
struct s
{
ll sum,prop;
}tree[4*mx];
ll arr[100009];
void init(int node,int b,int e)
{
if (b==e)
{
tree[node].sum=arr[b];
tree[node].prop=0;
return;
}
int Left=node*2;
int Right=(node*2)+1;
int mid=(b+e)/2;
init(Left,b,mid);
init(Right,mid+1,e);
tree[node].sum=tree[Left].sum + tree[Right].sum;
tree[node].prop=0;
}
void update(int node,int b,int e,int i,int j,ll x)
{
if(i>e||j<b)
return;
if(b>=i&&e<=j)
{
tree[node].prop+=x;
tree[node].sum+=(e-b+1)*x;
return;
}
ll Left=node*2;
ll Right=(node*2)+1;
ll mid=(b+e)/2;
update(Left,b,mid,i,j,x);
update(Right,mid+1,e,i,j,x);
tree[node].sum=tree[Left].sum+tree[Right].sum+(e-b+1)*tree[node].prop;
}
ll query(int node,int b,int e,int i,int j,ll carry=0)
{
if(i>e||j<b)
return 0;
if (b>=i&&e<=j)
{
return tree[node].sum+carry*(e-b+1);
}
int Left=node<<1;
int Right=(node<<1)+1;
int mid=(b+e)>>1;
ll p1=query(Left,b,mid,i,j,carry+tree[node].prop);
ll p2=query(Right,mid+1,e,i,j,carry+tree[node].prop);
return p1+p2;
}
int main()
{
int i,j,k,l,m,n,test;
scanf("%lld",&test);
for(m=1;m<=test;m++)
{
scanf("%d%d",&n,&k);
printf("Case %d:\n",m);
memset(arr,0,sizeof(arr));
init(1,1,n);
for(i=0;i<k;i++)
{
int op;
scanf("%d",&op);
if(op==0)
{
int st,ed,vl;
scanf("%d%d%lld",&st,&ed,&vl);
update(1,1,n,st+1,ed+1,vl);
}
if(op==1)
{
int st,ed;
scanf("%d%d",&st,&ed);
ll y=query(1,1,n,st+1,ed+1);
printf("%lld\n",y);
}
}
}
return 0;
}
#define mx 100009
using namespace std;
typedef long long ll;
struct s
{
ll sum,prop;
}tree[4*mx];
ll arr[100009];
void init(int node,int b,int e)
{
if (b==e)
{
tree[node].sum=arr[b];
tree[node].prop=0;
return;
}
int Left=node*2;
int Right=(node*2)+1;
int mid=(b+e)/2;
init(Left,b,mid);
init(Right,mid+1,e);
tree[node].sum=tree[Left].sum + tree[Right].sum;
tree[node].prop=0;
}
void update(int node,int b,int e,int i,int j,ll x)
{
if(i>e||j<b)
return;
if(b>=i&&e<=j)
{
tree[node].prop+=x;
tree[node].sum+=(e-b+1)*x;
return;
}
ll Left=node*2;
ll Right=(node*2)+1;
ll mid=(b+e)/2;
update(Left,b,mid,i,j,x);
update(Right,mid+1,e,i,j,x);
tree[node].sum=tree[Left].sum+tree[Right].sum+(e-b+1)*tree[node].prop;
}
ll query(int node,int b,int e,int i,int j,ll carry=0)
{
if(i>e||j<b)
return 0;
if (b>=i&&e<=j)
{
return tree[node].sum+carry*(e-b+1);
}
int Left=node<<1;
int Right=(node<<1)+1;
int mid=(b+e)>>1;
ll p1=query(Left,b,mid,i,j,carry+tree[node].prop);
ll p2=query(Right,mid+1,e,i,j,carry+tree[node].prop);
return p1+p2;
}
int main()
{
int i,j,k,l,m,n,test;
scanf("%lld",&test);
for(m=1;m<=test;m++)
{
scanf("%d%d",&n,&k);
printf("Case %d:\n",m);
memset(arr,0,sizeof(arr));
init(1,1,n);
for(i=0;i<k;i++)
{
int op;
scanf("%d",&op);
if(op==0)
{
int st,ed,vl;
scanf("%d%d%lld",&st,&ed,&vl);
update(1,1,n,st+1,ed+1,vl);
}
if(op==1)
{
int st,ed;
scanf("%d%d",&st,&ed);
ll y=query(1,1,n,st+1,ed+1);
printf("%lld\n",y);
}
}
}
return 0;
}
No comments:
Post a Comment