#include #include using namespace std; using namespace atcoder; typedef long long ll; #define rep(i, n) for (int i = 0; i < n; i++) #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) int main(){ ll n , q ; cin >> n >> q ; vector> A ; vector> B(n) ; rep(i,n){ cin >> B[i].first ; B[i].second = i ; A.push_back(B[i]) ; } sort(A.begin(),A.end()) ; rep(i,q){ ll x , y ; cin >> x >> y ; x-- , y-- ; ll l = 0 , r = n ; while ( r - l > 1 ) { ll m = ( r + l ) / 2 ; if( B[x].first < A[m].first ) l = m ; else r = m ; } ll a = l ; l = 0 , r = n ; while ( r - l > 1 ) { ll m = ( r + l ) / 2 ; if( B[y].first < A[m].first ) l = m ; else r = m ; } ll b = l ; if( a+1 < n && A[a+1].first == B[x].first ) a++ ; if( b+1 < n && A[b+1].first == B[x].first ) b++ ; if( a-1 >= 0 && A[a-1].first == B[x].first ) a-- ; if( b-1 >= 0 && A[b-1].first == B[x].first ) b-- ; cout << max((ll)b-a-1,0LL) << endl ; } }