感撅问题出在check 求调pls
其中 ck2(i,l,r) 表示 p 点的树,从 l 开始种,到 r 时是否满足(这个和题解对过了 感觉无锅),t 为二分的每个点最晚种树时间。upd 函数是在某点种树向上跳的函数。
void upd(int k) { if(fk1[k]) return ; fk1[k]=1; tp.pb(k); upd(fa[k]); return ; }
bool ck(ll yu)
{
For(i,0,n) fk1[i]=0,vis[i]=0;
fk1[0]=1;
For(i,1,n)
{
ll l=1,r=yu,ans=-1;
while(l<=r)
{
ll mid=(l+r)/2;
if(ck2(mid,yu,i)) l=mid+1,ans=mid;
else r=mid-1;
}
t[i].se=ans; t[i].fi=i;
}
sort(t+1,t+1+n,cmp);
For(i,1,n) { if(fk1[t[i].fi]) { C; } tp.clear(); upd(t[i].fi); int qaq=vis[fa[tp.back()]]; while(!tp.empty()) { vis[tp.back()]=++qaq; if(qaq>t[i].se) { return 0; } tp.pk(); } }
return 1;
}
Genshin code
// "那你既然选择了这条路 就坚持走下去吧"
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
//#define int long long
//#define ull unsigned long long
#define ull __int128
#define ll long long
#define MAXN 100019
#define WA cerr<<"CCF\n";
#define eps 1e-5
#define none -1145141919
#define pii pair<int,int>
#define Y cout<<"Yes\n"
#define N cout<<"No\n"
#define H cout<<"\n"
#define WH cerr<<"\n"
#define fi first
#define se second
#define C continue
#define For(i,a,b) for(int i=a;i<=b;i++)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define Rof(i,a,b) for(int i=a;i>=b;--i)
#define ROF(i,a,b) for(int i=a;i>b;--i)
#define G(i,p) for(auto i:pq[p])
#define VG(i,p) for(int i=0;i<pq[p].size();i++)
#define pb push_back
#define pk pop_back
const int MOD=1e9+7;
int n;
ll a[MAXN];
int b[MAXN],c[MAXN];
#define K cout<<" "
ull clac1(ll l,ll r,int p)//b_p+x*c_p
{
ull qwql=l,qwqr=r;
return ((r-l+1)*b[p])+(ull)((l+r)*(ull)(r-l+1)/2*c[p]);
}
bool ck2(ll l,ll r,int p)
{
ull qwq=0;
//cout<<a[p]<<" "<<l<<" "<<r<<" "<<b[p]<<"\n"; pr((clac2(l,r,p)));cout<<"\n";
if(c[p]>=0) { qwq=(clac1(l,r,p)); }
else
{
ll lj=((1-b[p])/(c[p]));
if(lj<l) { qwq+=(ull)(r-l+1); }
else if(lj>r) { qwq=clac1(l,r,p); }
else
{ qwq=clac1(l,lj,p); qwq+=(r-lj); }
}
return qwq>=a[p];
}
vector<int> pq[MAXN];
ll vis[MAXN];
int fa[MAXN];
pair<int,ll> t[MAXN];
bool cmp(pair<int,ll> x,pair<int,ll> y)
{
return x.se<y.se;
}
bool fk1[MAXN];
void init_dfs(int p,int f_) { fa[p]=f_; G(i,p) { if(i!=f_) { init_dfs(i,p); } } return ; }
vector<int> tp;
void upd(int k) { if(fk1[k]) return ; fk1[k]=1; tp.pb(k); upd(fa[k]); return ; }
bool ck(ll yu)
{
For(i,0,n) fk1[i]=0,vis[i]=0;
fk1[0]=1;
For(i,1,n)
{
ll l=1,r=yu,ans=-1;
while(l<=r)
{
ll mid=(l+r)/2;
if(ck2(mid,yu,i)) l=mid+1,ans=mid;
else r=mid-1;
}
t[i].se=ans; t[i].fi=i;
}
sort(t+1,t+1+n,cmp);
For(i,1,n) { if(fk1[t[i].fi]) { C; } tp.clear(); upd(t[i].fi); int qaq=vis[fa[tp.back()]]; while(!tp.empty()) { vis[tp.back()]=++qaq; if(qaq>t[i].se) { return 0; } tp.pk(); } }
return 1;
}
main()
{
//ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n; //getchar();
For(i,1,n) { cin>>a[i]>>b[i]>>c[i]; }
FOR(i,1,n) { int u,v; cin>>u>>v; pq[u].pb(v); pq[v].pb(u); }
init_dfs(1,0);
ll l=1,r=1e9;
ll ans=0;
while(l<=r)
{
ll mid=(l+r)/2;
if(ck(mid)) ans=mid,r=mid-1;
else l=mid+1;
}
cout<<ans<<"\n";
return 0;
}
/*
Need check before submit
1.INF=1e9 check if the val>=1e9 and only 0x3f3f3f3f and 0 can be memset!!!
2.Don't using getchar() unless is NESSASARY!!!
3.Enough size for the array!!!
4.Notice double's precision
5.CHECK UR LL OR ULL!!!
6.READ THE PROBLEM AGAIN AND AGAIN!!!
7.Check if for() is over int
*/