#include #include #include #include using namespace std; using ll = long long; template struct FenwickTree{ const int n; vector arr; FenwickTree() = default; FenwickTree(int n): n(n), arr(vector(n+1, 0)){} void add(int ind, T x){ for(int i = ind+1; i <= n; i += i & (-i)){ arr[i] += x; } } T sum_sub(int end){ T res = 0; for(int i = end; i > 0; i -= i & (-i)){ res += arr[i]; } return res; } T sum(int start, int end){ return sum_sub(end) - sum_sub(start); } int lower_bound(T w){ if(w <= 0) return 0; int x = 0; int k = 1 << 30; while(k > n) k /= 2; for(; k > 0; k /= 2){ if(x+k <= n && arr[x+k] < w){ w -= arr[x+k]; x += k; } } return x; } }; template vector compress(vector &x){ vector vals = x; sort(vals.begin(), vals.end()); vals.erase(unique(vals.begin(), vals.end()), vals.end()); for(auto &p: x){ p = lower_bound(vals.begin(), vals.end(), p) - vals.begin(); } return vals; } struct tup{ int l, r, x, id; }; int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, q; cin >> n >> q; vector a(n); for(auto &it: a) cin >> it; vector vec(n+q); for(int i = 0; i < q; i++){ cin >> vec[i].l >> vec[i].r >> vec[i].x; vec[i].l--; vec[i].id = i; } for(int i = 0; i < n; i++){ vec[q+i].x = a[i]; vec[q+i].id = q+i; } sort(vec.begin(), vec.end(), [](const tup &x, const tup &y){ return x.x < y.x; }); FenwickTree cnt(n), tot(n); for(int i = 0; i < n; i++){ cnt.add(i, 1); } vector ans(q); for(auto &it: vec){ if(it.id < q){ ans[it.id] += tot.sum(it.l, it.r); ans[it.id] += cnt.sum(it.l, it.r)*it.x; // cerr << cnt.sum(it.l, it.r) << " / " << tot.sum(it.l, it.r) << " " << it.x << " " << ans[it.id] << endl; }else{ cnt.add(it.id-q, -1); tot.add(it.id-q, it.x); } } for(int i = 0; i < q; i++) cout << ans[i] << '\n'; return 0; }