#include #include #include #include #include #include #include #include #include #include #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=1e9+7; template void chmin(T &a,const T &b){if(a>b) a=b;} template void chmax(T &a,const T &b){if(a template< typename T, typename S > struct LazySegmentTree{ int n; vector data; vector lazy; T te; S se; inline void merge_functions(S& lazy,S& val){ lazy+=val; //lazy=val; } inline void operate(T& data,S& val,int len){ //data+=val*len; //data=val*len; //data+=val; //data=val; T tmp; tmp.first=data.first+val*len; tmp.second=data.second+(val*val)*len+2*val*data.first; data=tmp; } inline T merge_values(T& x,T& y){ return mkp(x.first+y.first,x.second+y.second); //return min(x,y); //return max(x,y); } LazySegmentTree(){} LazySegmentTree(int sz,T te,S se):te(te),se(se){ n=1; while(n &A){ for(int k=0;k=0;k--) data[k]=merge_values(data[2*k+1],data[2*k+2]); } void eval(int k,int l,int r){ if(lazy[k]==se) return; operate(data[k],lazy[k],r-l); if(r-l>1){ merge_functions(lazy[2*k+1],lazy[k]); merge_functions(lazy[2*k+2],lazy[k]); } lazy[k]=se; } void update(int a,int b,S val,int k=0,int l=0,int r=-1){ if(r<0) r=n; eval(k,l,r); if(b<=l||r<=a) return; if(a<=l&&r<=b){ merge_functions(lazy[k],val); eval(k,l,r); }else{ update(a,b,val,2*k+1,l,(l+r)/2); update(a,b,val,2*k+2,(l+r)/2,r); data[k]=merge_values(data[2*k+1],data[2*k+2]); } } T query(int a,int b,int k=0,int l=0,int r=-1){ if(r<0) r=n; eval(k,l,r); if(b<=l||r<=a) return te; if(a<=l&&r<=b) return data[k]; T vl=query(a,b,2*k+1,l,(l+r)/2); T vr=query(a,b,2*k+2,(l+r)/2,r); return merge_values(vl,vr); } }; /* LazySegmentTree seg(N,te,se); vector init(N,0); seg.build(init); */ const pair TE=mkp(0,0); const ll SE=1e18; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin>>N; vector A(N); rep(i,N) cin>>A[i]; LazySegmentTree,ll> seg(N,TE,SE); vector> init(N,mkp(0,0)); rep(i,N) init[i]=mkp(A[i],A[i]*A[i]); seg.build(init); int Q; cin>>Q; rep(i,Q){ int t; cin>>t; if(t==1){ int l,r; ll x; cin>>l>>r>>x; l--;r--; seg.update(l,r+1,x); }else{ int l,r; cin>>l>>r; l--;r--; cout<