#include using namespace std; using Int = signed; #include #include #include using namespace __gnu_pbds; template using gtree = tree,rb_tree_tag,tree_order_statistics_node_update>; // usage: // find_by_order(Int k): return the iterator of k-th smallest element (0-indexed) // order_of_key(T key): return the index of key in tree //INSERT ABOVE HERE signed main(){ using T = gtree; Int n,m; scanf("%d %d",&n,&m); vector a(n),b(n); for(Int i=0;i dat(2<Int{ return G.order_of_key(y)-G.order_of_key(x); }; // [a, b) * [x, y) auto query= [&](Int a,Int b,Int x,Int y){ Int res=0; for(Int l=a+m,r=b+m;l>=1,r>>=1){ if(l&1) res+=calc(dat[l++],x,y); if(r&1) res+=calc(dat[--r],x,y); } return res; }; auto update= [&](Int a,Int b){ a+=m; while(a){ dat[a].insert(b); a>>=1; } }; Int ans=0; for(Int i=0;ib[i]) swap(a[i],b[i]); ans+=query(0,a[i],a[i],b[i]); ans+=query(a[i],b[i],b[i],m); update(a[i],b[i]); } printf("%d\n",ans); return 0; }