#include using namespace std; using ll = long long; bool chmin(auto &a, auto b) { return a > b ? a = b, true : false; } bool chmax(auto &a, auto b) { return a < b ? a = b, true : false; } template struct segtree { using M = Monoid; using S = typename M::S; segtree() : segtree(0) {} segtree(int _n) : segtree(vector(_n, M::e())) {} segtree(const vector &v) : n(v.size()) { log = 1; while ((1 << log) < n) log++; sz = 1 << log; d = vector(2 * sz, M::e()); for (int i = 0; i < n; i++) d[i + sz] = v[i]; for (int i = sz - 1; i >= 1; i--) update(i); } void set(int p, const S &x) { assert(0 <= p && p < n); p += sz; d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) const { assert(0 <= p && p < n); return d[p + sz]; } S prod(int l, int r) const { assert(0 <= l && l <= r && r <= n); l += sz; r += sz; S pl = M::e(), pr = M::e(); while (l < r) { if (l & 1) pl = M::op(pl, d[l++]); if (r & 1) pr = M::op(d[--r], pr); l >>= 1; r >>= 1; } return M::op(pl, pr); } S all_prod() const { return d[1]; } template int max_right(int l, const C &check) const { assert(0 <= l && l <= n); assert(check(M::e())); if (l == n) return l; l += sz; S p = M::e(); do { while (!(l & 1)) l >>= 1; S np = M::op(p, d[l]); if (!check(np)) { while (l < sz) { l <<= 1; np = M::op(p, d[l]); if (check(np)) { p = np; l++; } } return l - sz; } p = np; l++; } while ((l & -l) != l); return n; } template int max_right(const C &check) const { return max_right(0, check); } template int min_left(int r, const C &check) const { assert(0 <= r && r <= n); assert(check(M::e())); if (r == 0) return r; r += sz; S p = M::e(); do { r--; while (r > 1 && r & 1) r >>= 1; S np = M::op(d[r], p); if (!check(np)) { while (r < sz) { (r <<= 1)++; np = M::op(d[r], p); if (check(np)) { p = np; r--; } } return r + 1 - sz; } p = np; } while ((r & -r) != r); return 0; } template int min_left(const C &check) const { return min_left(n, check); } private: int n, log, sz; vector d; void update(int p) { d[p] = M::op(d[2 * p], d[2 * p + 1]); } }; #include template vector merge_and_unique(const vector &a, const Ts &...z) { vector res = a; (res.insert(res.end(), z.begin(), z.end()), ...); sort(res.begin(), res.end()); res.erase(unique(res.begin(), res.end()), res.end()); return res; } template struct fenwick_tree { using M = Abelian_Group; using S = typename M::S; fenwick_tree() = default; fenwick_tree(int _n) : fenwick_tree(vector(_n, M::e())) {} fenwick_tree(const vector &_v) : n(_v.size()) { d = vector(n, M::e()); v = vector(n, M::e()); for (int i = 1; i <= n; i++) { v[i - 1] = _v[i - 1]; d[i - 1] += v[i - 1]; int j = i + (i & -i); if (j <= n) d[j - 1] += d[i - 1]; } } void add(int p, S x) { assert(0 <= p && p < n); v[p] = M::op(v[p], x); for (p++; p <= n; p += p & -p) { d[p - 1] = M::op(d[p - 1], x); } } void set(int p, S x) { assert(0 <= p && p < n); add(p, M::op(x, M::inv(v[p]))); } S prod(int l, int r) const { assert(0 <= l && l <= r && r <= n); return prod(r) - prod(l); } private: int n; vector d, v; S prod(int p) const { S res = M::e(); for (; p > 0; p -= p & -p) { res = M::op(res, d[p - 1]); } return res; } }; template struct static_point_add_rectangle_sum { using M = Abelian_Group; using S = typename M::S; static_point_add_rectangle_sum() = default; static_point_add_rectangle_sum(int n, int q) { xs.reserve(n); ys.reserve(n); ws.reserve(n); xls.reserve(q); yls.reserve(q); xrs.reserve(q); yrs.reserve(q); } void add_point(T x, T y, S w) { xs.emplace_back(x); ys.emplace_back(y); ws.emplace_back(w); } void add_query(T xl, T yl, T xr, T yr) { xls.emplace_back(xl); yls.emplace_back(yl); xrs.emplace_back(xr); yrs.emplace_back(yr); } vector solve() { int n = xs.size(), q = xls.size(); if (n == 0 || q == 0) return vector(q, M::e()); auto yb = merge_and_unique(ys); for (T &y : ys) { y = lower_bound(yb.begin(), yb.end(), y) - yb.begin(); } vector ord_pts(n), ord_query(2 * q); iota(ord_pts.begin(), ord_pts.end(), 0); iota(ord_query.begin(), ord_query.end(), 0); ranges::sort(ord_pts, {}, [&](int i) { return xs[i]; }); ranges::sort(ord_query, {}, [&](int i) { return i < q ? xls[i] : xrs[i - q]; }); fenwick_tree fw(yb.size()); vector res(q); int j = 0; for (int i : ord_query) { T x = (i < q ? xls[i] : xrs[i - q]); while (j < n && xs[ord_pts[j]] < x) { fw.add(ys[ord_pts[j]], ws[ord_pts[j]]); j++; } if (i < q) { int yl = lower_bound(yb.begin(), yb.end(), yls[i]) - yb.begin(); int yr = lower_bound(yb.begin(), yb.end(), yrs[i]) - yb.begin(); res[i] = M::op(res[i], M::inv(fw.prod(yl, yr))); } else { i -= q; int yl = lower_bound(yb.begin(), yb.end(), yls[i]) - yb.begin(); int yr = lower_bound(yb.begin(), yb.end(), yrs[i]) - yb.begin(); res[i] = M::op(res[i], fw.prod(yl, yr)); } } return res; } private: vector xs, ys, xls, yls, xrs, yrs; vector ws; }; template ::max()> struct max_monoid { using S = T; static S op(const S &a, const S &b) { return max(a, b); } static S e() { return E; } }; template struct add_monoid { using S = T; static S op(const S &a, const S &b) { return a + b; } static S e() { return 0; } static S inv(const S &x) { return -x; } static S pow(const S &x, long long k) { return x * k; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, Q; cin >> N >> Q; segtree> seg(N + 1); static_point_add_rectangle_sum> RS(N, 2 * N); for (int i = 1, P; i <= N; i++) { cin >> P; RS.add_point(i, seg.prod(P, N + 1), 1); seg.set(P, i); } for (int i = 0, l, r; i < Q; i++) { cin >> l >> r; r++; RS.add_query(l, l, r, N + 2); } for (auto v : RS.solve()) { cout << v << '\n'; } }