# 21.6.1 t3

tag:平衡树，二分

• $$t_i<t_{mx}$$$$s_i$$ += $$t_{mx}$$，表示选了 $$i$$ 以后，$$mx$$ 这一堆会往后拖一天

• $$t_i\ge t_{mx}$$$$s_i$$ += $$t_i$$，表示 $$i$$ 会往后拖一天

#include<bits/stdc++.h>
using namespace std;

template<typename T>
char ch; bool flag=false;
while(!isdigit(ch=getchar()))if(ch==‘-‘)flag=true;
for(n=ch^48;isdigit(ch=getchar());n=(n<<1)+(n<<3)+(ch^48));
if(flag)n=-n;
}

enum{
MAXN = 500005
};

typedef long long ll;

int n;

struct node{
int son[2], fa, sz; ll val, add;
#define lc(x) t[x].son[0]
#define rc(x) t[x].son[1]
#define fa(x) t[x].fa
#define sz(x) t[x].sz
#define val(x) t[x].val
}t[MAXN];
int node_cnt, root;

inline char Which(int x){return rc(fa(x))==x;}
inline void Push_Up(int x){sz(x) = sz(lc(x))+sz(rc(x))+1;}

}

inline void Push_Down(int x){
}
}

inline void Rotate(int x){
int y=fa(x), z=fa(y), k=Which(x), u=t[x].son[!k];
if(z) t[z].son[Which(y)]=x; else root=x; fa(x)=z;
if(u) fa(u)=y; t[y].son[k]=u;
fa(y)=x; t[x].son[!k]=y;
Push_Up(y); Push_Up(x);
}

inline void Splay(int x, int Tar=0){
while(fa(x)!=Tar){
int y=fa(x), z=fa(y);
if(z!=Tar) Rotate(Which(x)==Which(y)?y:x);
Rotate(x);
}
}

struct ele{
ll s, t;
inline ll f(int x){return s+t*(x-1);}
inline bool operator <(const ele &k)const{return t<k.t;}
}a[MAXN];

void Insert(int &x, int y, ele k, int rk){
if(!x){
x = ++node_cnt;
val(x) = k.f(rk+1);
fa(x) = y; sz(x) = 1;
int tx=x;
Splay(tx);
return;
}
Push_Down(x);
if(k.f(rk+sz(lc(x))+1) < val(x)) Insert(rc(x),x,k,rk+sz(lc(x))+1);
else Insert(lc(x),x,k,rk);
}

ll s;
void Check(int x){
if(!x) return;
Push_Down(x);
Check(lc(x));
s += val(x); cout<<s<<‘\n‘;
Check(rc(x));
}

int main(){
// freopen("3.in","r",stdin);
// freopen("3.out","w",stdout);
sort(a+1,a+n+1);
for(register int i=1; i<=n; i++) Insert(root,0,a[i],0);// Check(root), puts("");
Check(root);
return 0;
}


21.6.1 t3

(0)
(0)