class Bit: def __init__(self, n): self.size = n self.n0 = 1 << (n.bit_length() - 1) self.tree = [0] * (n + 1) def range_sum(self, l, r): return self.sum(r - 1) - self.sum(l - 1) def sum(self, i): i += 1 s = 0 while i > 0: s += self.tree[i] i -= i & -i return s def get(self, i): return self.sum(i) - self.sum(i - 1) def add(self, i, x): i += 1 while i <= self.size: self.tree[i] += x i += i & -i def lower_bound(self, x): pos = 0 plus = self.n0 while plus > 0: if pos + plus <= self.size and self.tree[pos + plus] < x: x -= self.tree[pos + plus] pos += plus plus //= 2 return pos n, Q = map(int, input().split()) A = list(map(int, input().split())) se = set(A) L = [[] for _ in range(n)] R = [[] for _ in range(n)] X = [0] * n for i in range(Q): _, l, r, X[i] = map(int, input().split()) L[l - 1].append(i) R[r - 1].append(i) se.add(X[i]) lst = sorted(se) dic = {l:i for i, l in enumerate(lst)} le = len(lst) + 1 tot = Bit(le) cnt = Bit(le) ans = [0] * Q for i, a in enumerate(A): for j in L[i]: x = dic[X[j]] tmp = tot.range_sum(x + 1, le) - cnt.range_sum(x + 1, le) * X[j] ans[j] -= tmp cnt.add(dic[a], 1) tot.add(dic[a], a) for j in R[i]: x = dic[X[j]] tmp = tot.range_sum(x + 1, le) - cnt.range_sum(x + 1, le) * X[j] ans[j] += tmp print(*ans, sep="\n")