#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define X first #define Y second #define pb push_back #define rep(X,Y) for (int (X) = 0;(X) < (int)(Y);++(X)) #define reps(X,S,Y) for (int (X) = (int)(S);(X) < (int)(Y);++(X)) #define rrep(X,Y) for (int (X) = (int)(Y)-1;(X) >=0;--(X)) #define rreps(X,S,Y) for (int (X) = (int)(Y)-1;(X) >= (int)(S);--(X)) #define repe(X,Y) for ((X) = 0;(X) < (Y);++(X)) #define peat(X,Y) for (;(X) < (Y);++(X)) #define all(X) (X).begin(),(X).end() #define rall(X) (X).rbegin(),(X).rend() #define eb emplace_back #define UNIQUE(X) (X).erase(unique(all(X)),(X).end()) #define Endl endl #define NL <<"\n" #define cauto const auto using namespace std; using ll=long long; using pii=pair; using pll=pair; template using vv=vector>; template inline bool MX(T &l,const T &r){return l inline bool MN(T &l,const T &r){return l>r?l=r,1:0;} //#undef NUIP #ifdef NUIP #include "benri.h" #else #define out(args...) #endif #ifdef __cpp_init_captures templatevector table(int n, T v){ return vector(n, v);} template auto table(int n, Args... args){auto val = table(args...); return vector(n, move(val));} #endif template pair operator+(const pair &p,const pair &q){ return {p.X+q.X,p.Y+q.Y};} template pair& operator+=(pair &p,const pair &q){ p.X+=q.X; p.Y+=q.Y; return p;} template pair operator-(const pair &p,const pair &q){ return {p.X-q.X,p.Y-q.Y};} template pair& operator-=(pair &p,const pair &q){ p.X-=q.X; p.Y-=q.Y; return p;} template istream& operator>>(istream &is, pair &p){ is>>p.X>>p.Y; return is;} template T read(){ T re; cin>>re; return move(re);} template T read(const T &dec){ T re; cin>>re; return re-dec;} template vector readV(const int sz){ vector re(sz); for(auto &x:re) x=read(); return move(re);} template vector readV(const int sz, const T &dec){ vector re(sz); for(auto &x:re) x=read(dec); return move(re);} vv readG(const int &n,const int &m){ vv g(n); rep(_,m){ cauto a=read(1),b=read(1); g[a].pb(b); g[b].pb(a);} return move(g);} vv readG(const int &n){ return readG(n,n-1);} const ll MOD=1e9+7; //998244353 const int SQ=200; const int BK=21234/SQ+2; const int nn=21234; ll bit[nn+1]; ll sum(int i){ ++i; ll s=0; while(i>0){ s+=bit[i]; i-=i&-i; } return s; } void add(int i,ll x){ ++i; while(i<=nn){ bit[i]+=x; i+=i&-i; } } template struct MinMax{ T mn,mx; MinMax(T mn,T mx):mn(mn),mx(mx){} MinMax(T v):mn(v),mx(v){} MinMax():mn(MOD),mx(-MOD){} MinMax operator+(const MinMax &seg){ return MinMax(min(mn,seg.mn),max(mx,seg.mx)); } static MinMax e; }; template MinMax MinMax::e=MinMax(); templateostream& operator<<(ostream &os, const MinMax &seg){ return os<<"("< struct Add{ T ad; Add(T ad=0):ad(ad){} void operator+=(const Add &lfun){ ad+=lfun.ad; } bool operator==(const Add &lfun){ return ad==lfun.ad;} void eval(MinMax &s,int l,int r){ s.mn+=ad; s.mx+=ad; } static Add e; }; template Add Add::e=Add(); templateostream& operator<<(ostream &os, const Add &lfun){ return os<<"("< struct LazySegTree{ vector segs; vector opes; void lazy_eval(int k,int a,int b){ if(opes[k]==Ope::e) return; opes[k].eval(segs[k],a,b); if(2*k+2 LazySegTree(vector vs){ int n=vs.size(); int pw=1; while(pw(q,{1,0}); vector linv(n+1),rinv(n+1); rep(i,n){ add(vs[i],1); linv[i+1]=i+1-sum(vs[i])+linv[i]; } fill(bit,bit+nn+1,0); rrep(i,n){ add(vs[i],1); rinv[i]=sum(vs[i]-1)+rinv[i+1]; } out(linv,rinv,1); LazySegTree,Add> init(vector(n+1,0)); for(cauto v:vs) init.lb(v+1,1); vv qs(BK); rep(i,q) qs[ps[i].X/SQ].eb(i); vector re(q); rep(bk,BK)if(qs[bk].size()){ sort(all(qs[bk]),[&](cauto &i,cauto &j){ return ps[i].Y