#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; template map compress(vector &A){ map comp; int N = A.size(), i=0; for (int i=0; i struct BIT { vector bit; long long N; T P; BIT (long long n, T p = 0){ N = n; P = p; bit.resize(N); } void add(long long loc, T val){ loc++; while(loc <= N){ bit[loc-1] += val; if (P) bit[loc-1] %= P; loc += loc & -loc; } } long long sum(long long l, long long r){ T res = _sum(r) - _sum(l-1); if (P) res = (res + P) % P; return res; } long long _sum(long long r){ r++; T res = 0; while(r > 0){ res += bit[r-1]; if (P) res %= P; r -= r & -r; } return res; } }; int main(){ ll N, Q, L, R, X, now=0; cin >> N >> Q; vector v, A(N), ans(2*Q); vector> RXM(2*Q); BIT tc(3e5+5), ts(3e5+5); for (int i=0; i> A[i]; v.push_back(A[i]); } for (int i=0; i> L >> R >> X; L--; R--; v.push_back(X); RXM[i*2] = {L-1, X, i*2}; RXM[i*2+1] = {R, X, i*2+1}; } sort(RXM.begin(), RXM.end()); map mp = compress(v); for (auto [R, X, M] : RXM){ while(now <= R){ tc.add(mp[A[now]], 1); ts.add(mp[A[now]], A[now]); now++; } ans[M] = ts.sum(0, mp[X]-1) + tc.sum(mp[X], 3e5+4) * X; } for (int i=0; i