#include #include #include #include #include #include #include #include #define rep(i, a, b) for (int i = int(a); i < int(b); i++) using namespace std; using ll = long long int; // NOLINT using P = pair; // clang-format off #ifdef _DEBUG_ #define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; debug_print(__VA_ARGS__); } while(false) template void debug_print(const T &t, const Ts &...ts) { cerr << t; ((cerr << ", " << ts), ...); cerr << endl; } #else #define dump(...) do{ } while(false) #endif template vector make_v(size_t a, T b) { return vector(a, b); } template auto make_v(size_t a, Ts... ts) { return vector(a, make_v(ts...)); } template bool chmin(T &a, const T& b) { if (a > b) {a = b; return true; } return false; } template bool chmax(T &a, const T& b) { if (a < b) {a = b; return true; } return false; } template void print(const T& t, const Ts&... ts) { cout << t; ((cout << ' ' << ts), ...); cout << '\n'; } constexpr static struct PositiveInfinity { template constexpr operator T() const { return numeric_limits::max() / 2; } constexpr auto operator-() const; } inf; // NOLINT constexpr static struct NegativeInfinity { template constexpr operator T() const { return numeric_limits::lowest() / 2; } constexpr auto operator-() const; } NegativeInfinityVal; constexpr auto PositiveInfinity::operator-() const { return NegativeInfinityVal; } constexpr auto NegativeInfinity::operator-() const { return inf; } // clang-format on #include #include #include #include template void compress_insert(std::vector &inserter, const std::vector &v) { inserter.insert(inserter.end(), v.begin(), v.end()); } template std::vector compress_update(std::vector &inserter, const std::vector &v) { std::vector ret(v.size()); std::transform(v.begin(), v.end(), ret.begin(), [&](const E &e) { return static_cast(std::lower_bound(inserter.begin(), inserter.end(), e) - inserter.begin()); }); return ret; } template auto compress(const Args &...args) { std::vector> inserter; (..., compress_insert(inserter, args)); sort(inserter.begin(), inserter.end()); inserter.erase(unique(inserter.begin(), inserter.end()), inserter.end()); return std::tuple_cat(std::make_tuple(inserter), std::make_tuple(compress_update(inserter, args)...)); } #include #include namespace STF { template [[maybe_unused]] constexpr static auto Sum = [](const T &a, const T &b) -> T { return a + b; }; template [[maybe_unused]] constexpr static auto Update = []([[maybe_unused]] const T &a, const T &b) -> T { return b; }; template [[maybe_unused]] constexpr static auto Min = [](const T &a, const T &b) -> T { return min(a, b); }; template [[maybe_unused]] constexpr static auto Max = [](const T &a, const T &b) -> T { return max(a, b); }; } // namespace STF template class SegmentTree { std::vector data; int n; UpdateFunction update_func; QueryFunction query_func; public: SegmentTree(const std::vector &v, const T &init, const UpdateFunction &uf, const QueryFunction &qf) : update_func(uf), query_func(qf) { n = static_cast(v.size()); int size = static_cast(std::bit_ceil(v.size())); data.resize(2 * size, init); for (int i = 0; i < n; i++) { data[size + i] = v[i]; } for (int i = size - 1; i >= 1; i--) { data[i] = query_func(data[2 * i], data[2 * i + 1]); } } SegmentTree(const int nv, const T &init, const UpdateFunction &uf, const QueryFunction &qf) : SegmentTree(std::vector(nv, init), init, uf, qf) {} // 0-indexed void update(int pos, const T &val) { if (pos < 0 || pos >= n) return; pos += static_cast(data.size()) / 2; data[pos] = update_func(data[pos], val); while (pos > 1) { pos >>= 1; data[pos] = query_func(data[2 * pos], data[2 * pos + 1]); } } // [l, r), 0-indexed T query(int l, int r) const { const T &init = data.front(); if (l < 0 || r > n) return init; T res_l = init, res_r = init; int size = static_cast(data.size()) / 2; for (l += size, r += size; l < r; l >>= 1, r >>= 1) { if (l & 1) { res_l = query_func(res_l, data[l++]); } if (r & 1) { res_r = query_func(data[--r], res_r); } } return query_func(res_l, res_r); } }; template SegmentTree(const std::vector &, const T &, const UpdateFunction &, const QueryFunction &) -> SegmentTree; template SegmentTree(const int, const T &, const UpdateFunction &, const QueryFunction &) -> SegmentTree; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, q; cin >> n >> q; vector a(n); rep(i, 0, n) { cin >> a[i]; } a = get<1>(compress(a)); vector cnt(a.size(), 0); rep(i, 0, a.size()) { cnt[a[i]]++; } SegmentTree seg(cnt, 0, STF::Update, STF::Sum); rep(i, 0, q) { int x, y; cin >> x >> y; x = a[x - 1]; y = a[y - 1]; dump(x, y); if (x <= y) { print(0); } else { print(seg.query(y + 1, x)); } } return 0; }