#include using namespace std; using Int = long long; const char newl = '\n'; template inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template inline void chmax(T1 &a,T2 b){if(a void drop(const T &x){cout< vector read(size_t n){ vector ts(n); for(size_t i=0;i>ts[i]; return ts; } struct Mo{ using F = function; vector ls,rs,ord; Int n,width,nl,nr,ptr; F expandL,expandR; F shrinkL,shrinkR; Mo(Int n,Int width,F expandL,F expandR,F shrinkL,F shrinkR): n(n),width(width),nl(0),nr(0),ptr(0), expandL(expandL),expandR(expandR), shrinkL(shrinkL),shrinkR(shrinkR){} Mo(Int n,Int width,F expand,F shrink): Mo(n,width,expand,expand,shrink,shrink){} void add(Int l,Int r){ ls.emplace_back(l); rs.emplace_back(r); } void build(){ ord.resize(ls.size()); iota(ord.begin(),ord.end(),0); sort(ord.begin(),ord.end(), [&](Int a,Int b){ if(ls[a]/width!=ls[b]/width) return ls[a]ls[idx]) expandL(--nl); while(nrrs[idx]) shrinkR(--nr); return idx; } }; template struct SegmentTree{ using F = function; using G = function; using H = function; Int n,height; F f; G g; H h; T ti; E ei; vector dat; vector laz; SegmentTree(F f,G g,H h,T ti,E ei): f(f),g(g),h(h),ti(ti),ei(ei){} void init(Int n_){ n=1;height=0; while(n &v){ Int n_=v.size(); init(n_); for(Int i=0;i>i); } inline void recalc(Int k){ while(k>>=1) dat[k]=f(reflect((k<<1)|0),reflect((k<<1)|1)); } void update(Int a,Int b,E x){ if(a>=b) return; thrust(a+=n); thrust(b+=n-1); for(Int l=a,r=b+1;l>=1,r>>=1){ if(l&1) laz[l]=h(laz[l],x),l++; if(r&1) --r,laz[r]=h(laz[r],x); } recalc(a); recalc(b); } void set_val(Int a,T x){ thrust(a+=n); dat[a]=x;laz[a]=ei; recalc(a); } T query(Int a,Int b){ if(a>=b) return ti; thrust(a+=n); thrust(b+=n-1); T vl=ti,vr=ti; for(Int l=a,r=b+1;l>=1,r>>=1) { if(l&1) vl=f(vl,reflect(l++)); if(r&1) vr=f(reflect(--r),vr); } return f(vl,vr); } template Int find(Int st,C &check,T &acc,Int k,Int l,Int r){ if(l+1==r){ acc=f(acc,reflect(k)); return check(acc)?k-n:-1; } propagate(k); Int m=(l+r)>>1; if(m<=st) return find(st,check,acc,(k<<1)|1,m,r); if(st<=l&&!check(f(acc,dat[k]))){ acc=f(acc,dat[k]); return -1; } Int vl=find(st,check,acc,(k<<1)|0,l,m); if(~vl) return vl; return find(st,check,acc,(k<<1)|1,m,r); } template Int find(Int st,C &check){ T acc=ti; return find(st,check,acc,1,0,n); } }; template class BIT{ private: // \sum_{j < i} v[j] T sum(Int i){ T s(0); for(Int x=i;x>0;x-=(x&-x)) s+=bit[x]; return s; } public: Int n; vector bit; BIT(Int n_):n(n_+1),bit(n+1,0){} // v[i] += a void add(Int i,T a){ for(Int x=++i;x<=n;x+=(x&-x)) bit[x]+=a; } // \sum_{l <= i < r} v[i] T query(Int l,Int r){return sum(r)-sum(l);} // min({x | sum(x) >= w}) Int lower_bound(const T w){ if(w<=0) return 0; T r=w; Int x=0,p=1; while(p0;k>>=1){ if(x+k<=n&&bit[x+k]=w); return x; } // min({x | sum(x) > w}) Int upper_bound(T w){return lower_bound(w+1);} }; //INSERT ABOVE HERE signed main(){ cin.tie(0); ios::sync_with_stdio(0); Int n,q; cin>>n>>q; auto as=read(n); auto f=[&](Int a,Int b){return min(a,b);}; auto g=[&](Int a,Int b){return a+b;}; Int ti(1e9),ei(0); SegmentTree seg(f,g,g,ti,ei); seg.build(vector(n+2,0)); BIT bitL(n+2),bitR(n+2); Int res=0; auto expandL=[&](Int i){ res-=bitL.query(as[i]+1,n+2); res-=bitR.query(0,as[i]); bitL.add(as[i],-1); seg.update(0,as[i],-1); }; auto expandR=[&](Int i){ res-=bitL.query(as[i]+1,n+2); res-=bitR.query(0,as[i]); bitR.add(as[i],-1); seg.update(as[i]+1,n+2,-1); }; auto shrinkL=[&](Int i){ res+=bitL.query(as[i]+1,n+2); res+=bitR.query(0,as[i]); bitL.add(as[i],+1); seg.update(0,as[i],+1); }; auto shrinkR=[&](Int i){ res+=bitL.query(as[i]+1,n+2); res+=bitR.query(0,as[i]); bitR.add(as[i],+1); seg.update(as[i]+1,n+2,+1); }; for(Int i=n-1;i>=0;i--){ res+=bitR.query(0,as[i]); bitR.add(as[i],+1); seg.update(as[i]+1,n+2,+1); } Mo mo(n,200,expandL,expandR,shrinkL,shrinkR); vector ls,rs; for(Int i=0;i>l>>r; l--; ls.emplace_back(l); rs.emplace_back(r); mo.add(l,r); } mo.build(); vector ans(q,0); for(Int i=0;i