#include #include using namespace std; using namespace atcoder; using ll = long long; using mint = modint998244353; using vi = vector; using vvi = vector; using vvvi = vector; using vll = vector; using vvll = vector; using vvvll = vector; using vmi = vector; using vvmi = vector; using vvvmi = vector; #define all(a) (a).begin(), (a).end() #define rep2(i, m, n) for (int i = (m); i < (n); ++i) #define rep(i, n) rep2(i, 0, n) #define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i) #define drep(i, n) drep2(i, n, 0) ll op(ll a, ll b){ return a+b; } ll e(){return 0;} using t = tuple; using p = pair; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; vll a(n); rep(i, n)cin >> a[i]; vll s(n+1, 0); rep(i, n)s[i+1] = s[i] + a[i]; priority_queue, greater

> pq; rep(i, n)pq.push({a[i], i}); vector vt; rep(i, q){ int l, r; ll x; cin >> l >> r >> x; l--; vt.push_back({x, l, r, i}); }sort(all(vt)); vll ans(q, 0); segtree seg1(n), seg2(n); rep(i, q){ auto [x, l, r, j] = vt[i]; while(!pq.empty() && pq.top().first < x){ auto [b, k] = pq.top(); pq.pop(); seg1.set(k, b); seg2.set(k, 1); } ll c = seg2.prod(l, r), base = s[r] - s[l]; ans[j] = c*x - 2*seg1.prod(l, r) + base - x*(r-l-c); } rep(i, q){ cout << ans[i] << endl; } return 0; }