#include using namespace std; #ifdef LOCAL #include "settings/debug.cpp" #define _GLIBCXX_DEBUG #else #define Debug(...) void(0) #endif #define rep(i, n) for (int i = 0; i < (n); ++i) using ll = long long; using ull = unsigned long long; #include int main() { int n, q; cin >> n >> q; vector a(n); rep(i, n) cin >> a[i]; vector p2p(n); atcoder::dsu d(n); { vector> tmp; rep(i, n) tmp.push_back({ a[i], i }); sort(tmp.begin(), tmp.end()); rep(i, n) { if (i > 0 && tmp[i - 1].first == tmp[i].first) { p2p[tmp[i].second] = p2p[tmp[i - 1].second]; d.merge(tmp[i].second, tmp[i - 1].second); } else { p2p[tmp[i].second] = i; } } } while (q--) { int x, y; cin >> x >> y; --x, --y; cout << max(0, p2p[x] - p2p[y] - d.size(y)) << endl; } return 0; }