import sys input = sys.stdin.readline N,Q=map(int,input().split()) A=list(map(int,input().split())) Query=[list(map(int,input().split())) for i in range(Q)] # BIT(BIT-indexed tree) LEN=N BIT=[0]*(LEN+1)# 1-indexedなtree def update(v,w):# index vにwを加える while v<=LEN: BIT[v]+=w v+=(v&(-v))# 自分を含む大きなノードへ. たとえばv=3→v=4 def getvalue(v):# [1,v]の区間の和を求める ANS=0 while v!=0: ANS+=BIT[v] v-=(v&(-v))# 自分より小さい2ベキのノードへ. たとえばv=3→v=2へ return ANS BIT2=[0]*(LEN+1)# 1-indexedなtree def update2(v,w):# index vにwを加える while v<=LEN: BIT2[v]+=w v+=(v&(-v))# 自分を含む大きなノードへ. たとえばv=3→v=4 def getvalue2(v):# [1,v]の区間の和を求める ANS=0 while v!=0: ANS+=BIT2[v] v-=(v&(-v))# 自分より小さい2ベキのノードへ. たとえばv=3→v=2へ return ANS ALL=[(x,i) for i,x in enumerate(A)] for ind,(_,l,r,x) in enumerate(Query): ALL.append((x,l,r,ind)) ALL.sort(reverse=True) ANS=[0]*Q for a in ALL: if len(a)==2: update(a[1]+1,a[0]) update2(a[1]+1,1) else: ANS[a[3]]=getvalue(a[2])-getvalue(a[1]-1)-a[0]*(getvalue2(a[2])-getvalue2(a[1]-1)) for ans in ANS: print(ans)