using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static void Main() { var c = NList; var (n, q) = (c[0], c[1]); var a = NList; var lq = new List<(int pos, int val)>(n); for (var i = 0; i < n; ++i) lq.Add((i, a[i])); lq.Sort((l, r) => r.val.CompareTo(l.val)); var que = new List<(int id, int l, int r, int x)>(q); for (var i = 0; i < q; ++i) { c = NList; que.Add((i, c[0], c[1], c[2])); } que.Sort((l, r) => l.x.CompareTo(r.x)); var ftc = new FenwickTree(n); for (var i = 0; i < n; ++i) ftc.Add(i, 1); var fta = new FenwickTree(n); var res = new long[q]; foreach (var qi in que) { while (lq.Count > 0 && lq[lq.Count - 1].val < qi.x) { ftc.Add(lq[lq.Count - 1].pos, -1); fta.Add(lq[lq.Count - 1].pos, lq[lq.Count - 1].val); lq.RemoveAt(lq.Count - 1); } var sub = ftc.Sum(qi.r - 1) * qi.x + fta.Sum(qi.r - 1); if (qi.l > 1) sub -= ftc.Sum(qi.l - 2) * qi.x + fta.Sum(qi.l - 2); res[qi.id] = sub; } WriteLine(string.Join("\n", res)); } class FenwickTree { int size; long[] tree; public FenwickTree(int size) { this.size = size; tree = new long[size + 2]; } public void Add(int index, int value) { ++index; for (var x = index; x <= size; x += (x & -x)) tree[x] += value; } /// 先頭からindexまでの和(include index) public long Sum(int index) { ++index; var sum = 0L; for (var x = index; x > 0; x -= (x & -x)) sum += tree[x]; return sum; } public string Debug() { var sb = new System.Text.StringBuilder(); for (var i = 0; i < tree.Length - 2; ++i) { var val = Sum(i); if (i > 0) val -= Sum(i - 1); sb.Append(val).Append(" "); } return sb.ToString(); } } }