import sys input=sys.stdin.readline N=int(input()) A=list(map(int,input().split())) n=int(N**.5) Adata=[0]*((N-1)//n+1) RSQdata=[0]*((N-1)//n+1) RSSQdata=[0]*((N-1)//n+1) for i in range(N): q=i//n RSQdata[q]+=A[i] RSSQdata[q]+=A[i]**2 #print(RSSQdata) def update(l,r,x): id=l while id<=r: L=id q=id//n R=min((q+1)*n-1,r) if R-L+1==n: Adata[q]+=x RSQdata[q]+=x*n RSSQdata[q]+=2*x*RSQ(L,R)-n*x**2 else: for i in range(L,R+1): A[i]+=x RSQdata[q]+=(R-L+1)*x res=0 #print("HDHDH",q,L,R) for i in range(q*n,min(N,n*(q+1))): res+=(A[i]+Adata[q])**2 RSSQdata[q]=res id=R+1 def RSQ(l,r): id=l res=0 while id<=r: L=id q=id//n R=min((q+1)*n-1,r) if R-L+1==n: res+=RSQdata[q] else: for i in range(L,R+1): res+=A[i]+Adata[q] id=R+1 return res def RSSQ(l,r): id=l res=0 while id<=r: L=id q=id//n R=min((q+1)*n-1,r) if R-L+1==n: res+=RSSQdata[q] else: for i in range(L,R+1): res+=(A[i]+Adata[q])**2 id=R+1 return res def showA(): res=[A[i] for i in range(N)] for i in range(N): q=i//n res[i]+=Adata[q] print(res) for _ in range(int(input())): q=tuple(map(int,input().split())) if q[0]==1: i,l,r,x=q l-=1;r-=1 update(l,r,x) #showA() #print(RSQdata) #print(RSSQdata) else: i,l,r=q l-=1;r-=1 #print("ANS") print(RSSQ(l,r))