90pts,求调
查看原帖
90pts,求调
484751
ssl_lwz楼主2023/11/2 18:35
#include<bits/stdc++.h>
#define int __int128
using namespace std;
const int N = 1e5 + 10;
int a[N],b[N],c[N],n,d[N];

vector<int> v[N];
int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')  f=-1;ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0';ch=getchar(); 
	}
	return x*f;
}
void print(int p){
	if(p>9)  print(p/10);
	putchar(p%10+'0');
}
inline void dfs(int ti,int p,int fa){
	d[p]=ti;
	for(int i:v[p]){
		if(i==fa)  continue;
		dfs(ti+1,i,p);
	}
}
bool chek(int x,int p){
	//sum=b[p]+i*c[p] - >  len*(b[p]+(st+i)*c[p]/2)
	int sum=b[p]+x*c[p];
	return sum>=1;
	// b[p]+x*c[p]
}
int getd(int st,int ed,int p){
	//return max((st+ed)*(ed-st+1)/2*c[p]+b[p]*(ed-st+1),ed-st+1); 
	int sum;
	if(!c[p])  return b[p]*(ed-st+1);
//	for(int i=st;i<=ed;i++)
//	  sum+=max(b[p]+i*c[p],q);
	//sum=b[p]+i*c[p] - >  len(b[p]+c[p]*(st+ed))
	int z=st,y=ed;
	while(z<y){
		int mid=(z+y+1)>>1;
		if(chek(mid,p))  z=mid;
		else y=mid-1;
	}
	//if(p==4)  print(z);
	if(b[p]+st*c[p]<1){
		return ed-st+1;
	}
	else if(b[p]+ed*c[p]>=1){
		return b[p]*(ed-st+1)+(st+ed)*(ed-st+1)/2*c[p];
	}
	else if(b[p]+ed*c[p]==0)  z--;
	sum=(ed-z)+(st+z)*(z-st+1)/2*c[p]+b[p]*(z-st+1);
	return sum;
}
bool chk(int p){
	for(int i=1;i<=n;i++)
	  if(getd(d[i],p,i)<a[i])
	    return false;
	return true;
}
signed main(){

    n=read();
    for(int i=1;i<=n;i++){
    	a[i]=read();b[i]=read();c[i]=read();
	}
    for(int i=1,x,y;i<n;i++){
    	x=read();y=read();
    	v[x].push_back(y);v[y].push_back(x);
	}
	dfs(1,1,1);
	
//	cout<<getd(3,5,4);
	//for(int i=1;i<=n;i++)  cout<<getd(d[i],5,i)<<endl;
	int z=0,y=1e9;
	while(z<y){
	//	cout<<z<<" "<<y<<endl;
		int mid=(z+y)>>1;
		if(chk(mid))  y=mid;
		else z=mid+1;
	}
	print(z);
	return 0;
}
2023/11/2 18:35
加载中...