#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] >G[20000]; int ans[20000]; int cnt[20002]; main() { cin>>N>>Q; for(int i=0;i>A[i]; A[i]--; } for(int i=0;i>l>>r; l--; G[l].push_back(make_pair(r,i)); } int L=0; BITcntsum(N); for(int l=0;lP(N,(int)1e9, [](int a,int b){return a(N,0)); for(int i=0;iR(N); for(int i=G[l].size();i--;) { while(id>G[l][i].first) { id--; P.update(A[id]+1,N,1); all+=cntsum.sum(N-A[id]-1); all+=R.sum(A[id]); R.add(A[id]+1,1); } ans[G[l][i].second]=(l+P.query(0,N))*(id-l)+all; } L+=cntsum.sum(N-A[l]-1); cntsum.add(N-A[l],1); cnt[A[l]]++; } for(int i=0;i