bool TEST = false; using namespace std; #include #include #define rep(i,n) for(ll (i)=0;(i)<(ll)(n);i++) #define rrep(i,n) for(ll (i)=(ll)(n)-1;(i)>=0;i--) #define range(i,start,end,step) for(ll (i)=start;(i)<(ll)(end);(i)+=(step)) #define rrange(i,start,end,step) for(ll (i)=start;(i)>(ll)(end);(i)+=(step)) #define dump(x) cerr << "Line " << __LINE__ << ": " << #x << " = " << (x) << "\n"; #define spa << " " << #define fi first #define se second #define all(a) (a).begin(),(a).end() #define allr(a) (a).rbegin(),(a).rend() using ld = long double; using ll = long long; using ull = unsigned long long; using pii = pair; using pll = pair; using pdd = pair; template using V = vector; template using VV = V>; template using P = pair; template using M = map; template using S = set; template using UM = unordered_map; template using PQ = priority_queue, greater>; template using rPQ = priority_queue, less>; templatevector make_vec(size_t a){return vector(a);} templateauto make_vec(size_t a, Ts... ts){return vector(ts...))>(a, make_vec(ts...));} template ostream& operator << (ostream& os, const pair v){os << "(" << v.first << ", " << v.second << ")"; return os;} template ostream& operator<<(ostream &os, const vector &v) { for (auto &e : v) os << e << ' '; return os; } template ostream& operator<<(ostream& os, const vector> &v){ for(auto &e : v){os << e << "\n";} return os;} struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; template void UNIQUE(vector &x) {sort(all(x));x.erase(unique(all(x)), x.end());} template bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } void fail() { cout << -1 << '\n'; exit(0); } inline int popcount(const int x) { return __builtin_popcount(x); } inline int popcount(const ll x) { return __builtin_popcountll(x); } template void debug(vector>&v){for(ll i=0;i void debug(vector&v){if(v.size()!=0)cerr< void debug(priority_queue&v){V vals; while(!v.empty()) {cerr << v.top() << " "; vals.push_back(v.top()); v.pop();} cerr<<"\n"; for(auto val: vals) v.push(val);} template void debug(map&v){for(auto [k,v]: v) cerr << k spa v << "\n"; cerr<<"\n";} template void debug(unordered_map&v){for(auto [k,v]: v) cerr << k spa v << "\n";cerr<<"\n";} V listrange(int n) {V res(n); rep(i,n) res[i]=i; return res;} template P divmod(T a, T b) {return make_pair(a/b, a%b);} const ll INF = (1ll<<62); // const ld EPS = 1e-10; // const ld PI = acos(-1.0); template tuple, V, UM> press(V &l) { // xs: index-> original value // inds: xs[inds[i]] == l[i] // d: original value -> index UM d; V xs(l.size()); rep(i,l.size()){ xs[i] = l[i]; } V inds(l.size()); UNIQUE(xs); rep(i, xs.size()) d[xs[i]] = i; rep(i, l.size()) inds[i] = d[l[i]]; return forward_as_tuple(xs, inds, d); } template struct fenwick_tree { public: T total; fenwick_tree() : _n(0) {} explicit fenwick_tree(int n) : _n(n), data(n), total(0) {} void add(int p, T x) { assert(0 <= p && p < _n); p++; total += x; while (p <= _n) { data[p - 1] += T(x); p += p & -p; } } T query(int l, int r) { assert(0 <= l && l <= r && r <= _n); return query(r) - query(l); } T query(int r) { T s = 0; while (r > 0) { s += data[r - 1]; r -= r & -r; } return s; } int index(T v) { // minimum i s.t. a_0 + ... + a_i >= v // if not exists, return _n if (v<=0) return 0; if (total0) { if (x+l < _n and data[x+l-1]>= 1; } return x; } void check () { rep(i,_n) cout << query(i,i+1) << " "; cout << endl; } private: int _n; std::vector data; }; using BIT = fenwick_tree; BIT bit; void Main(){ ll n,q; cin >> n >> q; V a(n); V> lrx; rep(i,n) { cin >> a[i]; } rep(i,q) { ll l,r,x; cin >> l >> r >> x; l--; lrx.emplace_back(l,r,x); a.emplace_back(x); } auto [xs,inds,d] = press(a); rep(i,q) a.pop_back(); int m = xs.size(); int B = 300; int s = (n+B-1)/B; rep(i,B - (n%B)) a.emplace_back(0); V bs(s); V vs(s); rep(i,s) { BIT b(m); BIT v(m); range(j,B*i,B*(i+1),1) { b.add(d[a[j]], 1); v.add(d[a[j]], a[j]); } bs[i] = b; } rep(i,q) { auto [l,r,x] = lrx[i]; int bl = l/B; int br = r/B; ll ans = 0; if (bl==br) { range(j,l,r,1) { ans += min(x, a[j]); } } else { range(j,l,(bl+1)*B,1) ans += min(x, a[j]); range(j,br*B, r, 1) ans += min(x, a[j]); range(j,bl+1,br,1) { ll num = bs[j].query(x); ll val = vs[j].query(x); ans += val + (n-num) * x; } } cout << ans << "\n"; } } int main(void){ std::ifstream in("tmp_in"); if (TEST) { std::cin.rdbuf(in.rdbuf()); std::cout << std::fixed << std::setprecision(15); } else { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(15); } Main(); }