#include int ri() { int n; scanf("%d", &n); return n; } struct SegTree { std::vector > > feed; // feed std::vector > data; // real data int n; SegTree (const std::vector &a) { for (n = 1; n < (int) a.size(); n *= 2); data.resize(2 * n); feed.resize(2 * n); for (int i = 0; i < (int) a.size(); i++) data[i + n] = {a[i]}; for (int i = n - 1; i; i--) { data[i] = data[i << 1]; for (auto j : data[i << 1 | 1]) data[i].push_back(j); std::sort(data[i].begin(), data[i].end(), std::greater<>()); } } void query_reserve(int l, int r, int x) { for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (r & 1) feed[--r][x]; if (l & 1) feed[l++][x]; } } int64_t query(int l, int r, int x) { int64_t res = 0; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (r & 1) res += feed[--r][x]; if (l & 1) res += feed[l++][x]; } return res; } void fetch() { for (int i = 1; i < 2 * n; i++) { int head = 0; int64_t sum = 0; for (auto &j : feed[i]) { int cur = j.first; while (head < data[i].size() && data[i][head] >= cur) sum += data[i][head++]; j.second = sum - head * (int64_t) cur; } } } }; int main() { int n = ri(); int q = ri(); std::vector a(n); for (int i = 0; i < n; i++) a[i] = ri(); SegTree tree(a); int l[q], r[q], x[q]; for (int i = 0; i < q; i++) { ri(); l[i] = ri() - 1, r[i] = ri() - 1; x[i] = ri(); tree.query_reserve(l[i], r[i] + 1, x[i]); } tree.fetch(); for (int i = 0; i < q; i++) printf("%lld\n", (long long) tree.query(l[i], r[i] + 1, x[i])); return 0; }