#include #include #include #include #include #include #include using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) template using vi = vector; template using vii = vector>; template using viii = vector>; using pii = pair; template struct segtree { int n; vector d; const T e = 0; segtree(int n_ = 0) { n = 1; while (n < n_) n <<= 1; d.resize(2 * n - 1, e); } T op(T a, T b) { return a + b; } void update(int i, T x) { int idx = i + n - 1; d[idx] = x; while (idx) { idx = (idx - 1) / 2; d[idx] = op(d[2 * idx + 1], d[2 * idx + 2]); } return; } void add(int i, T x) { int idx = i + n - 1; d[idx] += x; while (idx) { idx = (idx - 1) / 2; d[idx] = op(d[2 * idx + 1], d[2 * idx + 2]); } return; } T get(int l, int r) { return get_sub(l, r, 0, 0, n); } T get_sub(int l, int r, int i, int ok, int ng) { if (l <= ok && ng <= r) return d[i]; if (r <= ok || ng <= l) return e; int mid = (ok + ng) / 2; T vl = get_sub(l, r, 2 * i + 1, ok, mid); T vr = get_sub(l, r, 2 * i + 2, mid, ng); return op(vl, vr); } }; using tii = tuple; int main() { int n, q; cin >> n >> q; vi a(n), l(q), r(q), x(q); rep(i, n) cin >> a[i]; rep(i, q) cin >> l[i] >> r[i] >> x[i]; rep(i, q) l[i]--; vi d; rep(i, n) d.push_back({ a[i], 0, i }); rep(i, q) d.push_back({ x[i], 1, i }); sort(d.begin(), d.end()); segtree seg(n + 10), segn(n + 10); vi ans(q); rep(i, n + q) { ll num, ax, pos; tie(num, ax, pos) = d[i]; if (ax == 0) { seg.add(pos, num); segn.add(pos, 1); } else if (ax == 1) { ll acum = seg.get(l[pos], r[pos] + 1); int cnt = segn.get(l[pos], r[pos] + 1); ans[pos] = acum + x[pos] * (r[pos] - l[pos] - cnt); } } for (ll res : ans) cout << res << endl; return 0; }