#include #include #include using namespace std; class fenwick_tree { private: int N; vector val; public: fenwick_tree() : N(0), val(vector()) {}; fenwick_tree(int N_) : N(N_) { val = vector(N + 1, 0); } void add(int pos, long long x) { for (int i = pos + 1; i <= N; i += i & (-i)) { val[i] += x; } } long long sum(int pos) const { long long res = 0; for (int i = pos; i >= 1; i -= i & (-i)) { res += val[i]; } return res; } }; class query { public: int tp, id, val; query() : tp(-1), id(-1), val(-1) {}; query(int tp_, int id_, int val_) : tp(tp_), id(id_), val(val_) {}; bool operator<(const query& q) const { return val != q.val ? val < q.val : tp < q.tp; } }; int main() { // step #1. read input cin.tie(0); ios_base::sync_with_stdio(false); int N, Q; cin >> N >> Q; vector A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } vector L(Q), R(Q), X(Q); for (int i = 0; i < Q; i++) { cin >> L[i] >> R[i] >> X[i]; L[i] -= 1; } // step #2. sort queries by X, A vector qs; for (int i = 0; i < N; i++) { qs.push_back(query(0, i, A[i])); } for (int i = 0; i < Q; i++) { qs.push_back(query(1, i, X[i])); } sort(qs.begin(), qs.end()); // step #3. answer queries fenwick_tree bit1(N), bit2(N); vector answer(Q); for (query q : qs) { if (q.tp == 0) { bit1.add(q.id, 1); bit2.add(q.id, q.val); } if (q.tp == 1) { answer[q.id] += bit2.sum(R[q.id]) - bit2.sum(L[q.id]); answer[q.id] += ((R[q.id] - L[q.id]) - (bit1.sum(R[q.id]) - bit1.sum(L[q.id]))) * q.val; } } // step #4. output! for (int i = 0; i < Q; i++) { cout << answer[i] << '\n'; } return 0; }