#include #include using mint = atcoder::modint998244353; // using mint = atcoder::modint1000000007; using namespace atcoder; using namespace std; #define rep(i,n) for (int i = 0; i < n; i++) using ll = long long; set abc = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; const int inf =1e9; const ll infl = 4e18; template bool chmax(T &a, const T& b) {if (a < b) {a = b; return true;}return false; } template bool chmin(T &a, const T& b) {if (a > b) {a = b; return true;}return false;} template struct compress { vector xs; void add(const T &x){ xs.emplace_back(x); } void build(){ sort(xs.begin(), xs.end()); xs.erase(unique(xs.begin(), xs.end()), xs.end()); } int get(const T &x){ return lower_bound(xs.begin(), xs.end(), x) - xs.begin(); } int size() const { return xs.size(); } const T &operator[](int i) const { return xs[i]; } }; int main(){ int N,Q; cin >> N >> Q; vector A(N);rep(i,N) cin >> A[i]; vector> xy(Q); rep(i,Q){ int x,y; cin >> x >> y; x--;y--; xy[i] = {x,y}; } compress cc; rep(i,N){ cc.add(A[i]); } cc.build(); fenwick_tree ft(cc.size()); rep(i,N){ ft.add(cc.get(A[i]),1); } for (auto [x,y] : xy){ if (cc.get(A[x])<=cc.get(A[y])){ cout << 0 << endl; continue; } cout << ft.sum(cc.get(A[y])+1,cc.get(A[x])) << endl; } }