#include using namespace std; using ll = long long; template using Pa = pair; template using vec = vector; template using vvec = vector>; template class SegmentTree{ private: int sz; vector seg; const F op; const Monoid e; public: SegmentTree(int n,const F op,const Monoid &e):op(op),e(e){ sz = 1; while(sz<=n) sz <<= 1; seg.assign(2*sz,e); } void set(int k, const Monoid &x){ seg[k+sz] = x; } void build(){ for(int i=sz-1;i>0;i--){ seg[i] = op(seg[2*i],seg[2*i+1]); } } void update(int k,const Monoid &x){ k += sz; seg[k] = x; while(k>>=1){ seg[k] = op(seg[2*k],seg[2*k+1]); } } Monoid query(int l,int r){ Monoid L = e,R = e; for(l+=sz,r+=sz;l>=1,r>>=1){ if(l&1) L = op(L,seg[l++]); if(r&1) R = op(seg[--r],R); } return op(L,R); } Monoid operator[](const int &k)const{ return seg[k+sz]; } }; template class Compress { map idx; map value; vec cmp,v; public: int N; Compress(){} void add(T x){v.push_back(x);} void build(){ for(auto &x:v) cmp.push_back(x); sort(cmp.begin(),cmp.end()); cmp.erase(unique(cmp.begin(),cmp.end()),cmp.end()); N = cmp.size(); for (int i=0;i> N; vec P(N); for(int i=0;i> P[i]; } auto op = [&](ll a,ll b){return a+b;}; auto conquer = [&](vec& A,vec& B){ int N = A.size(),M = B.size(); Compress cmp; for(auto x:A) cmp.add(x); for(auto x:B) cmp.add(x); cmp.add(-1); cmp.build(); int n = cmp.N; SegmentTree seg(n,op,0); vec ma(2,-1),mi(2,1e9); int r = 0; ll res = 0; for(int l=0;lB[r]){ mi[1] = B[r]; seg.update(cmp.id(B[r]),1); } r++; } // cerr << mi[1] << " " << mi[0] << "\n"; res += seg.query(cmp.id(mi[1]),cmp.id(mi[0])); } // cerr << res << "\n"; return res; }; auto devide = [&](auto&& self,int l,int r)->ll{ if(r-l<2) return 0; cerr << l << " " << r << "\n"; int m = (l+r)/2; ll res = self(self,l,m)+self(self,m,r); vec L,R; for(int i=l;i