rt,TLE #28 和 #30,已知正确性应该是没有问题的(全开 long long 也是在这两个点TLE),全部用 int 就会 WA 一些点,求问如何卡过这两个点或者指出错误
以下代码(不要在意这个混合的马蜂)
#include <bits/stdc++.h>
const int maxn=3e5+5;
using namespace std;
int n,c;
long long ans;
int l[maxn],cost[maxn],f[maxn];
int x[maxn],len,tot,rt[maxn],sz[maxn];
long long sum[maxn];
vector<int> e[maxn];
struct node
{
int l,r,val,dis;
} tr[maxn];
inline int merge(int x,int y)
{
if(!x||!y)
return x+y;
if(tr[x].val<tr[y].val)
swap(x,y);
tr[x].r=merge(tr[x].r,y);
if(tr[tr[x].l].dis<tr[tr[x].r].dis)
swap(tr[x].l,tr[x].r);
tr[x].dis=tr[tr[x].l].dis+1;
return x;
}
int newnode(int val)
{
tr[++tot]=node{0,0,val,0};
return tot;
}
bool operator<(node x,node y)
{
return x.val<y.val;
}
priority_queue<node> q;
inline void dfs(int u)
{
sz[u]=1;
sum[u]=cost[u];
for(int i=0;i<e[u].size();i++)
{
dfs(e[u][i]);
rt[u]=merge(rt[u],rt[e[u][i]]);
sz[u]+=sz[e[u][i]],sum[u]+=sum[e[u][i]];
}
rt[u]=merge(rt[u],newnode(cost[u]));
if(l[u]<l[f[u]])
return;
int cnt=sz[u];
long long tmp1=sum[u];
while(!q.empty())
q.pop();
q.push(tr[rt[u]]);
while(!q.empty())
{
node tmp=q.top();
q.pop();
if(tmp1<=c)
break;
tmp1-=tmp.val;
cnt--;
if(tmp.l)
q.push(tr[tmp.l]);
if(tmp.r)
q.push(tr[tmp.r]);
}
// cout <<cnt<<" "<<u<<endl;
ans=max(ans,1ll * l[u]*cnt);
}
int read() {
int sum = 0;
char c = getchar();
while(!isdigit(c))
c = getchar();
while(isdigit(c))
sum = sum * 10 + c - '0', c = getchar();
return sum;
}
signed main()
{
n = read(), c = read();
for(int i=1;i<=n;i++)
{
f[i] = read(), cost[i] = read(), l[i] = read();
e[f[i]].push_back(i);
}
dfs(1);
cout <<ans<<endl;
return 0;
}
/*
*/