/* -*- coding: utf-8 -*- * * 2065.cc: No.2065 Sum of Min - yukicoder */ #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100000; const int MAX_QN = 100000; /* typedef */ typedef long long ll; typedef pair pii; struct Elm { ll a; int k; Elm() {} Elm(ll _a, int _k): a(_a), k(_k) {} Elm operator+(const Elm &e) const { return Elm(a + e.a, k + e.k); } }; template struct SegTreeSum { int e2; vector nodes; T defv; SegTreeSum() {} void init(int n, T _defv) { defv = _defv; for (e2 = 1; e2 < n; e2 <<= 1); nodes.assign(e2 * 2, defv); } T &geti(int i) { return nodes[e2 - 1 + i]; } void seti(int i, T v) { geti(i) = v; } void setall() { for (int j = e2 - 2; j >= 0; j--) nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2]; } void set(int i, T v) { int j = e2 - 1 + i; nodes[j] = v; while (j > 0) { j = (j - 1) / 2; nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2]; } } T sum_range(int r0, int r1, int k, int i0, int i1) { if (r1 <= i0 || i1 <= r0) return defv; if (r0 <= i0 && i1 <= r1) return nodes[k]; int im = (i0 + i1) / 2; T v0 = sum_range(r0, r1, k * 2 + 1, i0, im); T v1 = sum_range(r0, r1, k * 2 + 2, im, i1); return v0 + v1; } T sum_range(int r0, int r1) { return sum_range(r0, r1, 0, 0, e2); } }; struct Query { int l, r, x, i; Query() {} Query(int _l, int _r, int _x, int _i): l(_l), r(_r), x(_x), i(_i) {} void read(int _i) { scanf("%d%d%d", &l, &r, &x), l--, i = _i; } bool operator<(const Query &q) const { return x < q.x; } }; /* global variables */ pii ais[MAX_N]; Query qs[MAX_QN]; SegTreeSum st; ll bs[MAX_QN]; /* subroutines */ /* main */ int main() { int n, qn; scanf("%d%d", &n, &qn); for (int i = 0; i < n; i++) scanf("%d", &ais[i].first), ais[i].second = i; sort(ais, ais + n); for (int i = 0; i < qn; i++) qs[i].read(i); sort(qs, qs + qn); st.init(n, Elm(0, 0)); for (int i = 0, j = 0; i < qn; i++) { Query &q = qs[i]; while (j < n && ais[j].first < q.x) { st.set(ais[j].second, Elm(ais[j].first, 1)); j++; } Elm e = st.sum_range(q.l, q.r); bs[q.i] = e.a + (ll)q.x * (q.r - q.l - e.k); } for (int i = 0; i < qn; i++) printf("%lld\n", bs[i]); return 0; }