#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long int ll; typedef pair P; random_device rnd; mt19937 mt(rnd()); uniform_real_distribution<> rnd1(0, 1.0); struct node_t{ ll val; ll sum; node_t *lch; node_t *rch; double pri; int cnt; node_t() {} node_t(ll v, double p):val(v), pri(p), cnt(1), sum(v), lch(nullptr), rch(nullptr){} }; int count(node_t *t){ return !t ? 0 : t->cnt; } ll sum(node_t *t){ return !t ? 0 : t->sum; } node_t *update(node_t *t){ t->cnt=count(t->lch)+count(t->rch)+1; t->sum=sum(t->lch)+sum(t->rch)+(t->val); return t; } node_t *merge(node_t *l, node_t *r){ if(!l || !r) return !l ? r : l; if(l->pri > r->pri){ l->rch=merge(l->rch, r); return update(l); }else{ r->lch=merge(l, r->lch); return update(r); } } pair split(node_t *t, ll x){ if(!t) return make_pair(nullptr, nullptr); if(x <= t->val){ pair s=split(t->lch, x); t->lch=s.second; return make_pair(s.first, update(t)); }else{ pair s=split(t->rch, x); t->rch=s.first; return make_pair(update(t), s.second); } } pair split2(node_t *t, int k){ //[0, k), [k,n) if(!t) return make_pair(nullptr, nullptr); if(k <= count(t->lch)){ pair s=split2(t->lch, k); t->lch=s.second; return make_pair(s.first, update(t)); }else{ pair s=split2(t->rch, k-1-count(t->lch)); t->rch=s.first; return make_pair(update(t), s.second); } } node_t *insert(node_t *t, ll x){ pair s=split(t, x); return merge(merge(s.first, new node_t(x, rnd1(mt))), s.second); } node_t *erase(node_t *t, int k){ pair s=split2(t, k); pair s2=split2(s.second, 1); return merge(s.first, s2.second); } ll find(node_t *t, int k){ if(count(t->lch)==k){ return t->val; }else if(count(t->lch)rch, k-count(t->lch)-1); }else{ return find(t->lch, k); } } void find2(node_t *t, ll x, int& ans){ if(x==(t->val)){ ans+=count(t->lch); return; }else if(x<(t->val)){ return find2(t->lch, x, ans); }else{ ans+=(count(t->lch)+1); return find2(t->rch, x, ans); } } ll calc(node_t *t, int k){ ll x=find(t, k); pair s=split2(t, k); ll s1=sum(s.first), s2=sum(s.second); ll ans=x*(ll)k-s1+s2-x*(ll)(count(s.second)); t=merge(s.first, s.second); return ans; } int main() { int n, k; cin>>n>>k; ll a[100000]; for(int i=0; i>a[i]; } node_t *s=nullptr; for(int i=0; i