#include using namespace std; using ll =long long; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); ll N,Q; cin>>N>>Q; vector P(N); for(int i=0;i>P[i]; P[i]=N-P[i]; } reverse(P.begin(),P.end()); ll SS=0; for(int q=0;q>L>>R; L--; ll an=R-L; ll pp=N-L; SS+=R-L; if(SS>1e7)assert(0); L=N-R; R=pp; vector D={P.begin()+L,P.begin()+R}; sort(D.begin(),D.end()); ll t=0; for(int j=L;j