本题数据有点离谱
查看原帖
本题数据有点离谱
723198
AAA404楼主2023/9/8 18:14

rt,在别的OJ上同样时间限制能过,同样的代码在洛谷过不了,永远T#25~#30

#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
const int N=1e6+5;
int n,m,sz[N],sum[N],c[N],l[N],root[N];
vector<int>v[N];
unsigned long long ans;
struct node{
	int l,r,val,dis;
}ltt[N];
#define ls(x) ltt[x].l
#define rs(x) ltt[x].r
inline int merge(int x,int y)
{
	if(!x||!y)return x+y;
	if(ltt[x].val<ltt[y].val)swap(x,y);
	rs(x)=merge(rs(x),y);
	if(ltt[ls(x)].dis<ltt[rs(x)].dis)
		swap(ls(x),rs(x));
	ltt[x].dis=ltt[rs(x)].dis;
	sz[x]=sz[ls(x)]+sz[rs(x)]+1;
	sum[x]=sum[ls(x)]+sum[rs(x)]+ltt[x].val;
	return x;
}
inline int pop(int x){return merge(ls(x),rs(x));}
inline void newnode(int x)
{
	sum[x]=ltt[x].val=c[x];
	sz[x]=1;root[x]=x;
}
inline void dfs(int now)
{
	newnode(now);
	for(int t:v[now])
	{
		dfs(t);
		root[now]=merge(root[now],root[t]);
	}
	while(sum[root[now]]>m&&sz[root[now]])root[now]=pop(root[now]);
	ans=max(ans,1ull*sz[root[now]]*l[now]);
	return;
}
inline int read()
{
	char ch=getchar();int s=0,w=1;
	while(ch<'0' || ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0' && ch<='9'){s=s*10+ch-48;ch=getchar();}
	return s*w;
}
signed main()
{
#ifdef LOCAL
 	freopen("1.in","r",stdin);
 	freopen("1.out","w",stdout);
#endif
    n=read(),m=read();
	for(int i=1;i<=n;i++)
	{
		int b=read();
		c[i]=read(),l[i]=read();
		v[b].push_back(i);
	}
	dfs(1);
	cout<<ans;
 	return 0;
}
2023/9/8 18:14
加载中...