阔爱的萌心63分求助qwq
查看原帖
阔爱的萌心63分求助qwq
342584
流夏的美楼主2021/2/25 09:38

rt

#include <stdio.h>
#include <iostream>
#define int long long
const int N = 100005;

struct node {
	int lc, rc, val, dis, size, sum;
} T[N];

struct G {
	int to, pre;
} g[N];

int n, m, r, cnt, top, ans;
int v[N], L[N], val[N], fa[N], root[N];

int merge(int x, int y) {
	if(!x || !y) {
		T[x + y].size = T[x + y].size;
		T[x + y].sum = T[T[x + y].rc].sum + T[T[x + y].lc].sum + T[x + y].val;
		return x + y;
	} 
	if(T[x].val < T[x].val) std :: swap(x, y);
	T[x].rc = merge(T[x].rc, y);
	if(T[T[x].rc].dis > T[T[x].lc].dis) std :: swap(T[x].rc, T[x].lc);
	T[x].size = T[T[x].rc].size + T[T[x].lc].size + 1;
	T[x].sum = T[T[x].rc].sum + T[T[x].lc].sum + T[x].val;
	T[x].dis = T[T[x].rc].dis + 1;
	return x;
}

void del_(int now) {
	root[now] = merge(T[root[now]].lc, T[root[now]].rc);
	return ;
}

void dfs(int u) {
	T[u].val = T[u].sum = val[u], root[u] = u, T[u].size = 1;
	for(int i = v[u]; i; i = g[i].pre) {
		int p = g[i].to;
		dfs(p);
		root[u] = merge(root[u], root[p]);
	}
	while(T[root[u]].sum > m) 
		del_(u);
	ans = std :: max(ans, T[root[u]].size * L[u]);
	return ;
}

void add(int fa, int child) {
	g[++cnt].pre = v[fa];
	g[cnt].to = child;
	v[fa] = cnt;
	return ;
}

signed main() {
	scanf("%lld %lld", &n, &m);
	for(int i = 1; i <= n; ++i) {
		scanf("%lld %lld %lld", &fa[i], &val[i], &L[i]);
		if(fa[i]) add(fa[i], i);
		else r = i;
	}
	dfs(r);
	printf("%lld\n", ans);
	return 0;
}
2021/2/25 09:38
加载中...