#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;
const int N=100100;
ll a[N];
struct node{
ll mul,add,val,val2;
}segTree[N<<2];
inline int lson(int x){return x<<1;}
inline int rson(int x){return lson(x)|1;}
void build(int t,int l,int r)
{
segTree[t].add=0;
segTree[t].mul=1;
if(l==r)
{
segTree[t].val=a[l];
segTree[t].val2=a[l]*a[l];
return;
}
int mid=(l+r)>>1;
build(lson(t),l,mid);
build(rson(t),mid+1,r);
segTree[t].val=segTree[lson(t)].val+segTree[rson(t)].val;
segTree[t].val2=segTree[lson(t)].val2+segTree[rson(t)].val2;
}
void pushdown(int t,int l,int r)
{
int mid=(l+r)>>1;
segTree[lson(t)].add=segTree[lson(t)].add*segTree[t].mul+segTree[t].add;
segTree[rson(t)].add=segTree[rson(t)].add*segTree[t].mul+segTree[t].add;
segTree[lson(t)].mul=segTree[lson(t)].mul*segTree[t].mul;
segTree[rson(t)].mul=segTree[rson(t)].mul*segTree[t].mul;

segTree[lson(t)].val2=segTree[lson(t)].val2*segTree[t].mul*segTree[t].mul
+(segTree[t].add*segTree[t].add)*(mid-l+1)+2*segTree[t].add*segTree[t].mul*segTree[lson(t)].val;

segTree[rson(t)].val2=segTree[rson(t)].val2*segTree[t].mul*segTree[t].mul
+(segTree[t].add*segTree[t].add)*(r-mid)+2*segTree[t].add*segTree[t].mul*segTree[rson(t)].val;

segTree[lson(t)].val=segTree[lson(t)].val*segTree[t].mul+segTree[t].add*(mid-l+1);
segTree[rson(t)].val=segTree[rson(t)].val*segTree[t].mul+segTree[t].add*(r-mid);

segTree[t].mul=1;
segTree[t].add=0;

}
///*
void update1(int t,int l,int r,int s,int e,ll c)
{
if(l>e||r<s)///完全不在当前区间
{
return;
}
if(l>=s&&r<=e)///完全在里面
{
segTree[t].mul=segTree[t].mul*c;
segTree[t].val=segTree[t].val*c;
segTree[t].val2=segTree[t].val2*c*c;
segTree[t].add=segTree[t].add*c;
return;
}
pushdown(t,l,r);
int mid=(l+r)>>1;
update1(lson(t),l,mid,s,e,c);
update1(rson(t),mid+1,r,s,e,c);
segTree[t].val=segTree[lson(t)].val+segTree[rson(t)].val;
segTree[t].val2=segTree[lson(t)].val2+segTree[rson(t)].val2;
}
///+
void update2(int t,int l,int r,int s,int e,ll c)
{
if(l>e||r<s)///完全不在当前区间
{
return;
}
if(l>=s&&r<=e)///完全在里面
{
segTree[t].val2=segTree[t].val2+2*c*segTree[t].val+c*c*(r-l+1);
segTree[t].val=segTree[t].val+c*(r-l+1);
segTree[t].add=segTree[t].add+c;
return;
}
pushdown(t,l,r);
int mid=(l+r)>>1;
update2(lson(t),l,mid,s,e,c);
update2(rson(t),mid+1,r,s,e,c);
segTree[t].val=segTree[lson(t)].val+segTree[rson(t)].val;
segTree[t].val2=segTree[lson(t)].val2+segTree[rson(t)].val2;
}
ll query1(int t,int l,int r,int s,int e)
{
if(l>e||r<s)///完全不在当前区间
{
return 0;
}
if(l>=s&&r<=e)///完全在里面
{
return segTree[t].val;
}
pushdown(t,l,r);
int mid=(l+r)>>1;
return query1(lson(t),l,mid,s,e)+query1(rson(t),mid+1,r,s,e);
}
ll query2(int t,int l,int r,int s,int e)
{
if(l>e||r<s)///完全不在当前区间
{
return 0;
}
if(l>=s&&r<=e)///完全在里面
{
return segTree[t].val2;
}
pushdown(t,l,r);
int mid=(l+r)>>1;
return query2(lson(t),l,mid,s,e)+query2(rson(t),mid+1,r,s,e);
}
int main()
{
int n,op,m,x,y;
ll c;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
build(1,1,n);
while(m--)
{
scanf("%d",&op);
if(op==3)
{
scanf("%d%d%lld",&x,&y,&c);
update1(1,1,n,x,y,c);
}
else if(op==4)
{
scanf("%d%d%lld",&x,&y,&c);
update2(1,1,n,x,y,c);
}
else if(op==1)
{
scanf("%d%d",&x,&y);
printf("%lld\n",query1(1,1,n,x,y));
}
else if(op==2)
{
scanf("%d%d",&x,&y);
printf("%lld\n",query2(1,1,n,x,y));
}
}
}
return 0;
}

0 条评论

目前还没有评论...