#include using namespace std; typedef long long ll; typedef pair P; typedef vector

VP; typedef vector VI; typedef vector VVI; #define REP(i, n) for (ll i = 0; i < (n); i++) #define ALL(v) v.begin(), v.end() #define OUT(n) cout << n << "\n" // constexpr ll MOD=998244353; // constexpr ll MOD=1000000007; // constexpr ll INF=2e18; int main() { ios::sync_with_stdio(false); cin.tie(0); ll N, Q; cin >> N >> Q; VP A(N); REP(i, N) { cin >> A[i].first; A[i].second = i; } VP sorted_A = A; sort(ALL(sorted_A)); VI C(N); // 一個前の人のindex ll prev = -1; REP(i, N) { if (i != 0) { if (sorted_A[i].first != sorted_A[i - 1].first) { prev = sorted_A[i - 1].second; } } C[sorted_A[i].second] = prev; } VI B(N); // i番目の人前にいる人の数 ll cnt = 0; ll wait = 0; REP(i, N) { if (i != 0) { if (sorted_A[i].first != sorted_A[i - 1].first) { cnt += wait; wait = 0; } } B[sorted_A[i].second] = cnt; wait += 1; } REP(i, Q) { ll x, y; cin >> x >> y; ll k = 0; if (C[x - 1] != -1) k = B[C[x - 1]]; ll howmany = k - B[y - 1]; if(C[x - 1] != -1) { if(B[x - 1] - B[C[x - 1]] != 1) { howmany += B[x - 1] - B[C[x - 1]]; } } if (howmany < 0) { howmany = 0; } OUT(howmany); } }