WA 90pts 这里的最值是用二分来求解的没有任何数学基础
查看原帖
WA 90pts 这里的最值是用二分来求解的没有任何数学基础
1212433
WAWA_QWQ楼主2024/8/13 10:09
#include<bits/stdc++.h>
#define ___ __int128
//#define ___ unsigned long long
#define int long long
using namespace std;
const ___ maxn=100005,maxe=200005;
___ n;
___ lnk[maxn],son[maxe],nxt[maxe],cnt,f[maxn];
struct DHC{
	___ a,b,c;
}pos[maxn];
___ read(){
	char ch=getchar();___ ret=0,f=1;
	while(ch<'0'||ch>'9'){if(ch=='-') f=-f;ch=getchar();}
	while(ch>='0'&&ch<='9') ret=ret*10+(ch&15),ch=getchar();
	return ret*f;
}
void add_edge(___ x,___ y){son[++cnt]=y,nxt[cnt]=lnk[x],lnk[x]=cnt;}
bool pd1(___ i,___ D,___ x){
	___ a=pos[i].a,b=pos[i].b,c=pos[i].c;
	if((D-x+1)*b+c*(x+D)*(D-x+1)/2>=a) return 1;
	return 0;
}
bool pd2(___ i,___ D,___ x){
	___ a=pos[i].a,b=pos[i].b,c=pos[i].c;
	if((D-x+1)*b>=a) return 1;
	return 0;
}
bool pd3(___ i,___ D,___ x){
	___ a=pos[i].a,b=pos[i].b,c=-pos[i].c,las;
	if(b%c) las=b/c+1;else las=b/c;
	___ now=0;
	if(x>=las){
		now+=D-x+1;
		if(now>=a) return 1;
		return 0;
	}
	now=D-las+1;
	las--;
	now+=(las-x+1)*b-c*(x+las)*(las-x+1)/2;
	if(now>=a) return 1;
	return 0;
}
___ DFS(___ x,___ fa){
	___ now=f[x];
	for(___ j=lnk[x];j;j=nxt[j])
	  if(son[j]!=fa) now=min(now,DFS(son[j],x)-1);
	return now;
}
bool check(___ D){
	memset(f,0,sizeof f);
	for(___ i=1;i<=n;i++){
		if(pos[i].c>0){
			___ L=0,R=1000000001,mid,ans=0;
			while(L<=R){
				mid=L+(R-L)/2;
				if(pd1(i,D,mid)) ans=mid,L=mid+1;else R=mid-1;
			}
			if(ans==-1) return 0;
			f[i]=ans;
		}
		if(pos[i].c==0){
			___ L=0,R=1000000001,mid,ans=-1;
			while(L<=R){
				mid=L+(R-L)/2;
				if(pd2(i,D,mid)) ans=mid,L=mid+1;else R=mid-1;
			}
			if(ans==-1) return 0;
			f[i]=ans;
		}
		if(pos[i].c<0){
			___ L=0,R=1000000001,mid,ans=0;
			while(L<=R){
				mid=L+(R-L)/2;
				if(pd2(i,D,mid)) ans=mid,L=mid+1;else R=mid-1;
			}
			if(ans==0) return 0;
			f[i]=ans;
		}
	}
	if(DFS(1,-1)>=1) return 1;
	return 0;
}
void print(___ now){
	if(!now) return;
	print(now/10);putchar(now%10+'0');
}
signed main(){ 
	n=read();
	for(___ i=1;i<=n;i++){
		___ r1=read(),r2=read(),r3=read();
		pos[i]=(DHC){r1,r2,r3};
	}
	for(___ i=1;i<n;i++){
		___ x=read(),y=read();
		add_edge(x,y);add_edge(y,x);
	}
	___ L=0,R=1000000001,mid,ans=0;
	while(L<=R){
		mid=L+(R-L)/2;
		if(check(mid)) ans=mid,R=mid-1;else L=mid+1;
	}
    printf("%lld\n",ans);
	return 0;
}
2024/8/13 10:09
加载中...