#include #include #include #include #include #include #include using namespace std; using namespace atcoder; using ll = long long; using mint = modint998244353; using vi = vector; using vvi = vector; using vvvi = vector; using vll = vector; using vvll = vector; using vvvll = vector; using vmi = vector; using vvmi = vector; using vvvmi = vector; #define all(a) (a).begin(), (a).end() #define rep2(i, m, n) for (int i = (m); i < (n); ++i) #define rep(i, n) rep2(i, 0, n) #define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i) #define drep(i, n) drep2(i, n, 0) using p = pair; using q = pair; int o1(int a, int b){ return a+b; } int o2(int a, int b){ return max(a, b); } int e1(){return 0;} int e2(){return -1;} void solve(){ } int main(){ int n, Q; cin >> n >> Q; vi a(n); rep(i, n)cin >> a[i]; priority_queue, greater> pq; vi ans(Q, -1); rep(i, Q){ int l, r; cin >> l >> r; pq.push({l-1, {r, i}}); } vi sai(n, -1); segtree seg1(n); segtree seg2(n); rep(i, n){ int c = a[i] - 1; seg2.set(c, i); if(i==0)continue; if(seg2.prod(c+1, n) >=0){ seg1.set(i, 1); sai[i] = seg2.prod(c+1, n); } } priority_queue, greater

> psai; rep(i, n){ if(sai[i] >= 0)psai.push({sai[i], i}); } int l = 0; while(!pq.empty()){ auto u = pq.top(); pq.pop(); while(l < u.first){ while(!psai.empty() && psai.top().first <= l){ auto v = psai.top(); psai.pop(); seg1.set(v.second, 0); } l++; } ans[u.second.second] = seg1.prod(u.first, u.second.first); } rep(i, Q)cout << ans[i] << endl; return 0; }