#include // #include // #include #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for(int i = (s); i < (int)(n); i++) #define printYN(check) cout << ((check)? "Yes" : "No") << endl #define all(x) x.begin(), x.end() #define allr(x) x.rbegin(), x.rend() #define NUM 998244353 #define IMAX INT_MAX #define LMAX LONG_MAX using namespace std; // using namespace atcoder; // using mint = modint; // using mint = modint998244353; // using mint = modint1000000007; using vi = vector; using vvi = vector; using ll = long long; using vl = vector; using vvl = vector; using vs = vector; using vb = vector; using vvb = vector; using pii = pair; using vpii = vector; using tii = tuple; using mii = map; // using Graph = vvi; // Graph graph(n); // vi dx = {-1, 0, 1, -1, 1, -1, 0, 1}; // vi dy = { 1, 1, 1, 0, 0, -1, -1,-1}; template bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); } template bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); } // snippet -> {UnionFind, dfs_reculsive, dfs_stack, is_prime, mypow, print_array, // bfs_queue, digsum, binary_search, gcd, Dijkstra, bitsearch, // next_combination, binomial} // ------------------------------------------------------------------------- int main() { int n, q; cin >> n >> q; vl a(n); rep(i, n) cin >> a[i]; vl b(n); rep(i, n) b[i] = a[i]; sort(all(a)); auto isOK = [&](int index, ll key, vl& a) -> bool { if (a[index] >= key) return true; else return false; }; auto binary_search = [&](ll key, vl& a) -> int { int ng = -1; //「index = 0」が条件を満たすこともあるので、初期値は -1 int ok = (int)a.size(); // 「index = a.size()-1」が条件を満たさないこともあるので、初期値は a.size() /* ok と ng のどちらが大きいかわからないことを考慮 */ while (abs(ok - ng) > 1) { int mid = (ok + ng) / 2; if (isOK(mid, key, a)) ok = mid; else ng = mid; } return ok; }; while(q > 0) { int x, y; cin >> x >> y; x--, y--; int d = binary_search(b[x], a); int e = binary_search(b[y]+1, a); cout << max(d-e, 0) << endl; q--; } return 0; }