import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int q = sc.nextInt(); PriorityQueue units = new PriorityQueue<>(); for (int i = 1; i <= n; i++) { units.add(new Unit(i, sc.nextInt())); } PriorityQueue queries = new PriorityQueue<>(); for (int i = 0; i < q; i++) { sc.nextInt(); queries.add(new Query(i, sc.nextInt(), sc.nextInt(), sc.nextInt())); } BinaryIndexedTree bit = new BinaryIndexedTree(n + 1); BinaryIndexedTree bit2 = new BinaryIndexedTree(n + 1); long[] ans = new long[q]; while (queries.size() > 0) { Query x = queries.poll(); while (units.size() > 0 && units.peek().value >= x.value) { Unit u = units.poll(); bit.add(u.id, u.value); bit2.add(u.id, 1); } ans[x.id] = bit.getSum(x.left, x.right) - bit2.getSum(x.left, x.right) * x.value; } StringBuilder sb = new StringBuilder(); for (long x : ans) { sb.append(x).append("\n"); } System.out.print(sb); } static class Query implements Comparable { int id; int left; int right; int value; public Query(int id, int left, int right, int value) { this.id = id; this.left = left; this.right = right; this.value = value; } public int compareTo(Query another) { return another.value - value; } } static class Unit implements Comparable { int id; int value; public Unit(int id, int value) { this.id = id; this.value = value; } public int compareTo(Unit another) { return another.value - value; } } } class BinaryIndexedTree { int size; long[] tree; public BinaryIndexedTree(int size) { this.size = size; tree = new long[size]; } public void add(int idx, long value) { int mask = 1; while (idx < size) { if ((idx & mask) != 0) { tree[idx] += value; idx += mask; } mask <<= 1; } } public long getSum(int from, int to) { return getSum(to) - getSum(from - 1); } public long getSum(int x) { int mask = 1; long ans = 0; while (x > 0) { if ((x & mask) != 0) { ans += tree[x]; x -= mask; } mask <<= 1; } return ans; } }