from collections import * from itertools import * from functools import * from heapq import * import sys,math input = sys.stdin.readline class SegTree: """ Segment Tree """ def __init__(self, init_val, segfunc, ide_ele): """ 初期化 init_val: 配列の初期値 """ n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num # 配列の値を葉にセット for i in range(n): self.tree[self.num + i] = init_val[i] # 構築していく for i in range(self.num - 1, 0, -1): self.tree[i] = segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): """ k番目の値をxに更新 k: index(0-index) x: update value """ k += self.num self.tree[k] = x while k > 1: self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]) k >>= 1 def query(self, l, r): """ [l, r)のsegfuncしたものを得る l: index(0-index) r: index(0-index) """ res = self.ide_ele l += self.num r += self.num while l < r: if l & 1: res = self.segfunc(res, self.tree[l]) l += 1 if r & 1: res = self.segfunc(res, self.tree[r - 1]) l >>= 1 r >>= 1 return res N,Q = map(int,input().split()) A = list(map(int,input().split())) query = [tuple(map(int,input().split())) for _ in range(Q)] q = defaultdict(lambda:[]) X = [(a,i) for i,a in enumerate(A)] for _,l,r,x in query: q[x].append((l,r)) X.append((x,-1)) ans = dict() X = list(set(X)) X.sort(reverse=True) T = SegTree([0]*N,lambda x,y:x+y,0) S = SegTree([0]*N,lambda x,y:x+y,0) for val,idx in X: if idx==-1: x = val for l,r in q[x]: l -= 1 ans[(1,l+1,r,x)] = S.query(l,r) - x*T.query(l,r) continue T.update(idx,1) S.update(idx,val) # print(ans) # print(X) for q in query: print(ans[q])