class SegTree: #単位元と結合演算はここ変える #いろんな種類のsegは作れないかも #→changeで変えれる unit = (0,0) def f(self,x,y): a,b = x c,d = y return (a + c,b + d) #頂点は1-index、一番下の段は0-index(bitは1-index) def __init__(self,N): self.N = N self.X = [self.unit] * (N + N) def build(self,seq): for i,x in enumerate(seq,self.N): self.X[i] = x for i in range(self.N-1,0,-1): self.X[i] = self.f(self.X[i << 1],self.X[i << 1 | 1]) def set(self,i,x): i += self.N self.X[i] = x while i > 1: i >>= 1 self.X[i] = self.f(self.X[i << 1],self.X[i << 1 | 1]) def fold(self,L,R): #区間[L,R)についてfold #0 <= L,R <= N にしなきゃダメ L += self.N R += self.N vL = self.unit vR = self.unit while L < R: if L & 1: vL = self.f(vL,self.X[L]) L += 1 if R & 1: R -= 1 vR = self.f(self.X[R],vR) L >>= 1 R >>= 1 return self.f(vL,vR) def change(self,f,unit): self.f = f self.unit = unit import sys rr = sys.stdin N,Q = map(int,rr.readline().split()) a = list(map(int,rr.readline().split())) seg = SegTree(N) ll = [(t,i) for i,t in enumerate(a)] query = [] for _ in range(Q): s,l,r,x = map(int,rr.readline().split()) query.append((x,l,r,_)) ll.sort(key = lambda x:x[0]) query.sort(key = lambda x:x[0],reverse = True) ans = [0] * Q for x,l,r,i in query: while ll and ll[-1][0] >= x: t,index = ll.pop() seg.set(index,(t,1)) tmp,num = seg.fold(l-1,r) ans[i] = tmp - num * x print(*ans,sep = "\n")