BZOJ 3672 [Noi2014]购票【点分+斜率优化

先扔到序列上看看……

dp式子写出来一眼斜率优化……

dp[i] =

……因为有个l……所以决策看起来好像不单调啊……斜率也不单调……cdq啊稳啊


分块之后先处理前面那段,然后用前面的结果更新后面的;

反正都分治了,就把需要被更新的点按照 dis[i] - l[i] 从大到小排个序,然后把左边用来更新的dp值……从右往左把可以用来更新的值加进去,维护个凸包;

【第一次挂是没想到从右往左加点…………嗯…我怀疑之前的Flaze不会斜率优化】


cdq上树就……点分啊……

然后……就码到天荒地老【x


===== 我想想都错了些啥【又气又急】 =====

· 有个……就是…………某个地方…………没开long long…………【躺平】

· 对拍的时候……发现自己拍不出错………………然后……好像datamaker少了一行 srand(time(0)) ; ………………

· 在……找祖先的时候……for写反了

· 改的时候不小心删掉了……某个dfs里面的……if ( vis[aim] ) continue ;

· 终于发现自己斜率优化……推式子的时候…………少写了个负号…………于是upper_bound那儿查的是 rec[i].k …………GG


哦对……当时用小数据和毛神还有po姐拍…………然后……嗯……就是…………好像……那个……跑出来不一样的时候………………就是……可能……我用手跑出来的结果…………好像和自己的一样………………嗯……

#include
#define MAXN 200005
using namespace std ;	int n , read_t ;

inline int read() {
	register int ch = getchar() ;
	while (!isdigit(ch))	ch = getchar() ;
	register int rtn = 0 ;
	while (isdigit(ch))	rtn = rtn*10 + ch - '0' , ch = getchar() ;
	return rtn ;
}
inline long long read_ll() {
	register int ch = getchar() ;
	while (!isdigit(ch))	ch = getchar() ;
	register long long rtn = 0 ;
	while (isdigit(ch))	rtn = rtn*10 + ch - '0' , ch = getchar() ;
	return rtn ;
}

struct t1 {
	int to,nxt ;
	long long lth ;
}edge[MAXN<<1] ;	int cnt_edge ;
int fst[MAXN] ;
inline void addedge(int x, int y, long long lth) {
	edge[++cnt_edge].to = y ;
	edge[cnt_edge].nxt = fst[x] ;
	edge[cnt_edge].lth = lth ;
	fst[x] = cnt_edge ;
	
	edge[++cnt_edge].to = x ;
	edge[cnt_edge].nxt = fst[y] ;
	edge[cnt_edge].lth = lth ;
	fst[y] = cnt_edge ;
}

long long dp[MAXN] ;

long long read_lth ;
int fth[MAXN] ;
long long p[MAXN], q[MAXN], l[MAXN] ;

long long dpt[MAXN], dis[MAXN] ;
int siz[MAXN], f[MAXN] ;
int COG, siz_now ;

int vis[MAXN] ;

void dfs0(int now) {
	siz[now] = 1 ;
	for (int tmp = fst[now]; tmp; tmp = edge[tmp].nxt) {
		int aim = edge[tmp].to ;
		if (aim == fth[now])	continue ;
		dpt[aim] = dpt[now] + 1 ;
		dis[aim] = dis[now] + edge[tmp].lth ;
		dfs0(aim) ;
		siz[now] += siz[aim] ;
		f[now] = max ( f[now] , siz[aim] ) ;
	}
	f[now] = max ( f[now] , n - siz[now] ) ;
	if ( f[now] < f[COG] )	COG = now ;
}

int root_now , dpt_now ;
void dfs(int now, int fa) {
	siz[now] = 1 ;
	f[now] = 0 ;
	if ( dpt[now] < dpt_now )	root_now = now, dpt_now = dpt[now] ;
	for (int tmp = fst[now]; tmp; tmp = edge[tmp].nxt) {
		int aim = edge[tmp].to ;
		if ( aim == fa || vis[aim] )	continue ;
		dfs(aim, now) ;
		siz[now] += siz[aim] ;
		f[now] = max( f[now], siz[aim]) ;
	}
	f[now] = max ( f[now] , siz_now - siz[now] ) ;
	if ( f[now] < f[COG] )	COG = now ;
}

struct T1{
	int id ;
	long long left , k ;
	T1(){}
	T1(int id, long long left, long long k) : id(id), left(left), k(k) {}
	bool operator < (const T1 &ano) const {
		return left > ano.left ;
	}
}rec[MAXN] ;	int cnt_rec = 0 ;

void dfs_(int now, int fa) {
	rec[ ++cnt_rec ] = T1(now, dis[now] - l[now], p[now]) ;
	for (int tmp = fst[now]; tmp; tmp = edge[tmp].nxt) {
		int aim = edge[tmp].to ;
		if ( aim == fa || vis[aim] )	continue ;
		dfs_(aim, now) ;
	}
}

int pAth[MAXN], cnt_path ;
int stk[MAXN], top ;
double KK[MAXN] ;

inline double get_K(int a, int b) {
	return ((double)( dp[a]-dp[b] ))
		/ ((double)(dis[a]-dis[b])) ;
}

inline void insert( int x ) {
	while ( top > 1 && get_K( stk[top-1], stk[top] ) < get_K( stk[top], x) )	--top ;
	stk[ ++top ] = x , KK[top] = - get_K( stk[top-1], x) ;
}

inline void DP(int now, int rt) {
	cnt_path = 0 , top = 0 ;
	for (int i=now; i^fth[rt]; i = fth[i])
		pAth[ ++cnt_path ] = i ;
	
//	printf("now = %d\n",now) ;
//	for (int i=1; i<=cnt_path; ++i)	printf("%d ",pAth[i]) ;
//	puts("") ;
	
	for (int i=1, j=1; i<=cnt_rec; ++i) {
		int x = rec[i].id ;
		for (; j<=cnt_path && dis[ pAth[j] ] >= rec[i].left; ++j)
			insert( pAth[j] ) ;
		if (top == 1) {
			int o = stk[top] ;
			if ( rec[i].left <= dis[ o ] )
				dp[ x ] = min ( dp[ x ], dp[o] + p[ x ] * ( dis[x] - dis[ o ] ) + q[ x ] ) ;
		} else {
			if (!top)	continue ;
			int pos = min ( top*1ll, 1ll* (upper_bound( KK+2, KK+top+1, - rec[i].k ) - KK - 1) ) ;
			int o = stk[pos] ;
			if ( rec[i].left <= dis[ o ] )
				dp[ x ] = min ( dp[ x ], dp[ o ] + p[ x ] * ( dis[x] - dis[o] ) + q[ x ] ) ;
		}
	}
	for (int i=1; i<=cnt_rec; ++i) {
		int x = rec[i].id ;
		if ( x != now && rec[i].left <= dis[now] )
			dp[ x ] = min ( dp[x], dp[now] + p[x] * ( dis[x] - dis[now] ) + q[x] ) ;
	}
}

void solve(int now) {
	vis[now] = 1 ;
	dpt_now = 0x3f3f3f3f ;
	
	dfs(now,0) ;
	
	int rt = root_now ;
	if (fth[now] && !vis[fth[now]]) {
		siz_now = siz[fth[now]] ;
		COG = 0 ;
		dfs(fth[now],now) ;
		solve(COG) ;
	}

	rec[ cnt_rec = 1 ] = T1 ( now, dis[now] - l[now] , p[now] ) ;
	for (int tmp = fst[now]; tmp; tmp = edge[tmp].nxt)
		if ( edge[tmp].to != fth[now] )
			dfs_( edge[tmp].to, now ) ;

	sort( rec+1, rec+cnt_rec+1 ) ;

	DP( now , rt ) ;
	
	for (int tmp = fst[now]; tmp; tmp = edge[tmp].nxt) {
		int aim = edge[tmp].to ;
		if ( vis[aim] )	continue ;
		siz_now = siz[aim] ;
		COG = 0 , dfs(aim,now), solve(COG) ;
	}

}

int main() {
	//freopen("1.in","r",stdin) ;
//	freopen("1.out","w",stdout) ;

	n = read() , read_t = read() ;
	for (int i=1; i<=n; ++i)	dp[i] = 1ll<<60 ;
	dp[1] = 0 ;
	for (int i=2; i<=n; ++i)
		fth[i] = read() , read_lth = read_ll() , p[i] = read_ll() , q[i] = read_ll() , l[i] = read_ll() ,
		addedge(fth[i], i, read_lth) ;

	dpt[1] = 1 ;
	
	f[0] = 0x3f3f3f3f ;
	dfs0(1) , solve(COG) ;

	for (int i=2; i<=n; ++i)
		printf("%lld\n",dp[i]) ;

	return 0 ;
}


你可能感兴趣的:(OI,BZOJ,dp,斜率优化)