#include<bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef long long ll;
const int mx = 10005;
int n,m;
ll add[mx<<2],mul[mx<<2],sum1[mx<<2],sum2[mx<<2];
ll a[mx];
void pushup(int rt){
sum1[rt] = sum1[rt<<1] + sum1[rt<<1|1];
sum2[rt] = sum2[rt<<1] + sum2[rt<<1|1];
}
void build(int l,int r,int rt){
sum1[rt] = sum2[rt] = 0;
add[rt] = 0; mul[rt] = 1;
if(l == r){
sum1[rt] = a[l];
sum2[rt] = a[l]*a[l];
return ;
}
int mid = (l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void pushdown(int rt,int len){
mul[rt<<1] *= mul[rt];//
mul[rt<<1|1] *= mul[rt];//
add[rt<<1] = add[rt] + add[rt<<1]*mul[rt];
add[rt<<1|1] = add[rt] + add[rt<<1|1]*mul[rt];
sum2[rt<<1] = sum2[rt<<1]*mul[rt]*mul[rt]+ 2*add[rt]*sum1[rt<<1] + (len-(len>>1))*add[rt]*add[rt];
sum2[rt<<1|1] = sum2[rt<<1|1]*mul[rt]*mul[rt]+2*add[rt]*sum1[rt<<1|1] + (len>>1)*add[rt]*add[rt];
sum1[rt<<1] = add[rt]*(len-(len>>1)) + sum1[rt<<1]*mul[rt];
sum1[rt<<1|1] = add[rt]*(len>>1) + sum1[rt<<1|1]*mul[rt];
mul[rt] = 1;
add[rt] = 0;
}
void update(int l,int r,int rt,int L,int R,int val,int type){
if(l >= L && r <= R){
if(type == 3){
mul[rt] *= val;
add[rt] *= val;
sum1[rt] *= val;
sum2[rt] *= (val*val);
return ;
}else{
add[rt] += val;
sum2[rt] += (2*val*sum1[rt] + val*val*(r-l+1));
sum1[rt] += (val*(r-l+1));
return ;
}
}
pushdown(rt,r-l+1);
int mid = (l+r)>>1;
if(L <= mid) update(lson,L,R,val,type);
if(R > mid) update(rson,L,R,val,type);
pushup(rt);
}
ll q1,q2;
ll res;
void query(int l,int r,int rt,int L,int R){
if(l >= L && r <= R){
q1 += sum1[rt];
q2 += sum2[rt];
return ;
}
int mid = (l+r)>>1;
res = 0;
pushdown(rt,r-l+1);
if(L <= mid) query(lson,L,R);
if(R > mid) query(rson,L,R);
//pushup(rt);
}
int main(){
while(~scanf("%d%d",&n,&m)){
for(int i = 1; i <= n; i++){
scanf("%lld",&a[i]);
}
build(1,n,1);
int op,l,r;
ll v;
while(m--){
scanf("%d",&op);
q1 = q2 = res = 0;
if(op == 1){
scanf("%d%d",&l,&r);
query(1,n,1,l,r);
printf("%lld\n",q1);
}
else if(op == 2){
scanf("%d%d",&l,&r);
query(1,n,1,l,r);
printf("%lld\n",q2);
}
else if(op == 3){
scanf("%d%d%lld",&l,&r,&v);
update(1,n,1,l,r,v,3);
}
else if(op == 4){
scanf("%d%d%lld",&l,&r,&v);
update(1,n,1,l,r,v,4);
}
}
}

}

0 条评论

目前还没有评论...