## https://yukicoder.me/problems/no/443 class SegmentTree: """ 非再帰版セグメント木。 更新は「加法」、取得は「最大値」のもの限定。 """ def __init__(self, init_array): n = 1 while n < len(init_array): n *= 2 self.size = n self.array = [[] for _ in range(2 * self.size)] self.cum_array = [[] for _ in range(2 * self.size)] for i, a in enumerate(init_array): self.array[self.size + i].append(a) end_index = self.size start_index = end_index // 2 while start_index >= 1: for i in range(start_index, end_index): self._op(self.array[i], self.array[2 * i], self.array[2 * i + 1]) end_index = start_index start_index = end_index // 2 for i in range(1, len(self.array)): c = 0 for j in range(len(self.array[i])): c += self.array[i][j] self.cum_array[i].append(c) def _op(self, array, left, right): left_index = 0 right_index = 0 while left_index < len(left) or right_index < len(right): if left_index == len(left): while right_index < len(right): array.append(right[right_index]) right_index += 1 elif right_index == len(right): while left_index < len(left): array.append(left[left_index]) left_index += 1 else: if left[left_index] <= right[right_index]: array.append(left[left_index]) left_index += 1 else: array.append(right[right_index]) right_index += 1 def add(self, x, a): index = self.size + x self.array[index] += a while index > 1: index //= 2 self.array[index] = max(self.array[2 * index], self.array[2 * index + 1]) def _calc_value(self, array, cum_array, x): if len(array) == 0: return 0 if x < array[0]: return x * len(array) low = 0 high = len(array) - 1 while high - low > 1: mid = (high + low) // 2 if array[mid] <= x: low = mid else: high = mid if array[high] <= x: return cum_array[high] + x * (len(array) - 1 - high) else: return cum_array[low] + x * (len(array) - 1 - low) def get_sum(self, l, r, x): L = self.size + l; R = self.size + r # 2. 区間[l, r)の最大値を求める s = 0 while L < R: if R & 1: R -= 1 s += self._calc_value(self.array[R], self.cum_array[R], x) if L & 1: s += self._calc_value(self.array[L], self.cum_array[L], x) L += 1 L >>= 1; R >>= 1 return s def main(): N, Q = map(int, input().split()) A = list(map(int, input().split())) lrx = [] for _ in range(Q): l, r, x = map(int, input().split()) lrx.append((l - 1, r - 1, x)) seg_tree = SegmentTree(A) for l, r, x in lrx: ans = seg_tree.get_sum(l, r + 1, x) print(ans) if __name__ == "__main__": main()