#line 1 "a.cpp" #include #include #include using namespace std; #line 2 "/home/kotatsugame/library/datastructure/lazysegtree.cpp" #include template struct lazysegtree{ using F=function; using G=function; const F calcfn,lazycalcfn; const G updatefn; int n; T defvalue; vectordat,lazy; vectorlazyflag; lazysegtree(int n_=0,T defvalue_=0, const F calcfn_=[](T a,T b){return a+b;}, const F lazycalcfn_=[](T a,T b){return a+b;}, const G updatefn_=[](T a,T b,int l,int r){return a+b*(r-l);} ):defvalue(defvalue_), calcfn(calcfn_),lazycalcfn(lazycalcfn_),updatefn(updatefn_) { n=1; while(n&v) { for(int i=0;i=0;i--)dat[i]=calcfn(dat[2*i+1],dat[2*i+2]); } void eval(int i,int l,int r) { if(lazyflag[i]) { dat[i]=updatefn(dat[i],lazy[i],l,r); if(r-l>1) { lazy[2*i+1]=lazyflag[2*i+1]?lazycalcfn(lazy[2*i+1],lazy[i]):lazy[i]; lazy[2*i+2]=lazyflag[2*i+2]?lazycalcfn(lazy[2*i+2],lazy[i]):lazy[i]; lazyflag[2*i+1]=lazyflag[2*i+2]=true; } lazyflag[i]=false; } } void update(int a,int b,T x,int k=0,int l=0,int r=-1)//[a,b) { if(r<0)r=n; eval(k,l,r); if(b<=l||r<=a)return; else if(a<=l&&r<=b) { lazy[k]=lazyflag[k]?lazycalcfn(lazy[k],x):x; lazyflag[k]=true; eval(k,l,r); } else { update(a,b,x,2*k+1,l,(l+r)/2); update(a,b,x,2*k+2,(l+r)/2,r); dat[k]=calcfn(dat[2*k+1],dat[2*k+2]); } } T query(int a,int b,int k=0,int l=0,int r=-1)//[a,b) { if(r<0)r=n; eval(k,l,r); if(b<=l||r<=a)return defvalue; else if(a<=l&&r<=b)return dat[k]; else return calcfn( query(a,b,2*k+1,l,(l+r)/2), query(a,b,2*k+2,(l+r)/2,r) ); } }; #line 1 "/home/kotatsugame/library/datastructure/BIT.cpp" //1-indexed #line 3 "/home/kotatsugame/library/datastructure/BIT.cpp" template struct BIT{ int n; vectorbit; BIT(int n_=0):n(n_),bit(n_+1){} T sum(int i) { T ans=0; for(;i>0;i-=i&-i)ans+=bit[i]; return ans; } void add(int i,T a) { if(i==0)return; for(;i<=n;i+=i&-i)bit[i]+=a; } int lower_bound(T k)//k<=sum(ret) { if(k<=0)return 0; int ret=0,i=1; while((i<<1)<=n)i<<=1; for(;i;i>>=1) if(ret+i<=n&&bit[ret+i],int> >G[20000]; int ans[20000]; main() { cin>>N>>Q; for(int i=0;i>A[i]; A[i]--; } for(int i=0;i>l>>r; l--; G[l/B].push_back(make_pair(make_pair(l,r),i)); } int all=0; BITL(N),R(N); lazysegtreeP(N,(int)1e9, [](int a,int b){return a(N,0)); int l=0,r=N; for(int Gi=0;Gi*B,int>a,pair,int>b) { return a.first.second,int>p:G[Gi]) { while(l>p.first.first) { l--; all-=L.sum(N-A[l]-1); all-=R.sum(A[l]); L.add(N-A[l],-1); P.update(0,A[l],-1); } while(rp.first.second) { r--; all+=R.sum(A[r]); all+=L.sum(N-A[r]-1); R.add(A[r]+1,1); P.update(A[r]+1,N,1); } ans[p.second]=P.query(0,N)*(r-l)+all; } } for(int i=0;i