#pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include #include #define rep(i,n) for(int i=0;i vi; typedef vector vl; typedef vector> vvi; typedef vector> vvl; typedef long double ld; typedef pair P; template ostream& operator<<(ostream& os, const static_modint& a) {os << a.val(); return os;} template ostream& operator<<(ostream& os, const dynamic_modint& a) {os << a.val(); return os;} template istream& operator>>(istream& is, static_modint& a) {long long x; is >> x; a = x; return is;} template istream& operator>>(istream& is, dynamic_modint& a) {long long x; is >> x; a = x; return is;} template istream& operator>>(istream& is, vector& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;} template ostream& operator<<(ostream& os, const pair& p){os << p.first << ' ' << p.second; return os;} template ostream& operator<<(ostream& os, const vector& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;} template ostream& operator<<(ostream& os, const vector>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;} template ostream& operator<<(ostream& os, const set& se){for(T x : se) os << x << " "; os << "\n"; return os;} template ostream& operator<<(ostream& os, const unordered_set& se){for(T x : se) os << x << " "; os << "\n"; return os;} template ostream& operator<<(ostream& os, const atcoder::segtree& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;} template ostream& operator<<(ostream& os, const atcoder::lazy_segtree& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;} template void chmin(T& a, T b){a = min(a, b);} template void chmax(T& a, T b){a = max(a, b);} int main(){ int n, q; cin >> n >> q; vector a(n); cin >> a; vector b = a; sort(b.begin(), b.end()); vector b_sum(n + 1); rep(i, n) b_sum[i + 1] = b_sum[i] + b[i]; auto calc = [&](long long x){ int idx = lower_bound(b.begin(), b.end(), x) - b.begin(); long long res = 0; res += b_sum[n] - b_sum[idx]; res -= (n - idx) * x; res -= b_sum[idx] - b_sum[0]; res += idx * x; return res; }; rep(i, q){ int l, r; long long x; cin >> l >> r >> x; l--; r--; long long ans = 0; if(r - l <= n / 2){ for(int j = l; j <= r; j++){ ans += (a[j] < x ? x - a[j] : a[j] - x); } }else{ ans += calc(x); for(int j = 0; j <= l - 1; j++){ ans -= (a[j] < x ? x - a[j] : a[j] - x); } for(int j = r + 1; j <= n - 1; j++){ ans -= (a[j] < x ? x - a[j] : a[j] - x); } } cout << ans << "\n"; } return 0; }