#include int leaf[100001]; typedef struct { int left, right; long long sum; } seg_node; void init_node(seg_node v[], int k, int l, int r) { v[k].left = l; v[k].right = r; v[k].sum = 0; if (l < r) { init_node(v, k << 1, l, (l + r) / 2); init_node(v, (k << 1) ^ 1, (l + r) / 2 + 1, r); } else leaf[l] = k; } void update_node(seg_node v[], int k, int x) { int i, j = leaf[k]; v[j].sum = x; for (i = j >> 1; i > 0; j = i, i >>= 1) v[i].sum = v[j].sum + v[j^1].sum; } long long get_sum(seg_node v[], int k, int l, int r) { if (v[k].right < l || v[k].left > r) return 0; else if (v[k].left >= l && v[k].right <= r) return v[k].sum; else return get_sum(v, k << 1, l, r) + get_sum(v, (k << 1) ^ 1, l, r); } typedef struct { int key, id; } data; typedef struct { data obj[100001]; int size; } min_heap; void push(min_heap* h, data x) { int i = ++(h->size), j = i >> 1; data tmp; h->obj[i] = x; while (j > 0) { if (h->obj[i].key < h->obj[j].key) { tmp = h->obj[j]; h->obj[j] = h->obj[i]; h->obj[i] = tmp; i = j; j >>= 1; } else break; } } data pop(min_heap* h) { int i = 1, j = 2; data output = h->obj[1], tmp; h->obj[1] = h->obj[(h->size)--]; while (j <= h->size) { if (j < h->size && h->obj[j^1].key < h->obj[j].key) j ^= 1; if (h->obj[j].key < h->obj[i].key) { tmp = h->obj[j]; h->obj[j] = h->obj[i]; h->obj[i] = tmp; i = j; j <<= 1; } else break; } return output; } int main() { int i, N, Q, A[100001], L[100001], R[100001], X[100001]; scanf("%d %d", &N, &Q); for (i = 1; i <= N; i++) scanf("%d", &(A[i])); for (i = 1; i <= Q; i++) scanf("%d %d %d", &(L[i]), &(R[i]), &(X[i])); min_heap hA, hQ; data d, dd; hA.size = 0; hQ.size = 0; for (i = 1; i <= N; i++) { d.key = A[i]; d.id = i; push(&hA, d); } for (i = 1; i <= Q; i++) { d.key = X[i]; d.id = i; push(&hQ, d); } long long ans[100001]; seg_node v[2][262144]; init_node(v[0], 1, 1, N); init_node(v[1], 1, 1, N); for (i = 1; i <= N; i++) update_node(v[1], i, 1); while (hQ.size > 0) { d = pop(&hQ); while (hA.size > 0 && hA.obj[1].key < d.key) { dd = pop(&hA); i = dd.id; update_node(v[0], i, A[i]); update_node(v[1], i, 0); } i = d.id; ans[i] = get_sum(v[0], 1, L[i], R[i]) + get_sum(v[1], 1, L[i], R[i]) * X[i]; } for (i = 1; i <= Q; i++) printf("%lld\n", ans[i]); fflush(stdout); return 0; }