[bzoj] 2809 dispatching
原题
左偏树板子题。
左偏树详解:
#include<cstdio>
#include<algorithm>
#define N 100010
typedef long long ll;
using namespace std;
int n,m,f[N],cnt=1,l,r,q[N],money[N],lead[N],head[N];
ll ans;
struct node
{
node *ls,*rs;
int dis,sze,val;
ll sum;
node(): ls(NULL), rs(NULL), dis(0), sze(0), val(0), sum(0){}
node(int x): ls(NULL), rs(NULL), dis(0), sze(1), val(x), sum(x){}
node *updt()
{
if(!ls || ls->dis<rs->dis) swap(ls,rs);
dis=rs?rs->dis+1:0;
sze=(ls?ls->sze:0)+(rs?rs->sze:0)+1;
sum=(ls?ls->sum:0)+(rs?rs->sum:0)+val;
return this;
}
}*tre[N];
struct hhh
{
int to,next;
}edge[N];
int read()
{
int ans=0,fu=1;
char j=getchar();
for (;(j<'0' || j>'9') && j!='-';j=getchar()) ;
if (j=='-') fu=-1,j=getchar();
for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';
return ans*fu;
}
void add(int u,int v)
{
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
node *merge(node* x,node* y)
{
if (!x) return y;
if (!y) return x;
if (x->val<y->val) swap(x,y);
x->rs=merge(x->rs,y);
return x->updt();
}
void pop(node* &x)
{
while (x->sum>m)
x=merge(x->ls,x->rs);
}
int main()
{
n=read();
m=read();
for (int i=1;i<=n;i++)
{
f[i]=read();
money[i]=read();
lead[i]=read();
add(f[i],i);
tre[i]=new node(money[i]);
}
q[++r]=1;
for (int l=1;l<=r;l++)
for (int e=head[q[l]];e;e=edge[e].next)
q[++r]=edge[e].to;
for (int l=r,v,u;l>=1;l--)
{
v=q[l];
u=f[v];
ans=max(ans,(ll)lead[v]*tre[v]->sze);
if (u) tre[u]=merge(tre[u],tre[v]),pop(tre[u]);
}
printf("%lld\n",ans);
return 0;
}