【HDU】5044 Tree 树链剖分

传送门:【HDU】5044 Tree


题目分析:如果不看卡常数这回事的话。。这就是一个很裸的树链剖分。。然后就可以用线段树维护。

但是!这样对于本题是一定会超时的!因为出题人特意想卡。。。

于是我换成树状数组+输入优化卡过。。。


但这题还有更好的方法!我们可以在树链剖分上用标记法,每次对连续区间的位置L标记+v,位置R+1标记-v,最后扫一遍结果就出来了。。


O(nlog^2n)代码如下:


#include 
#include 
#include 
#include 
using namespace std ;

typedef long long LL ;

#pragma comment(linker, "/STACK:16777216")
#define rep( i , a , b ) for ( int i = a ; i < b ; ++ i )
#define For( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define rev( i , a , b ) for ( int i = a ; i >= b ; -- i )
#define travel( e , H , u ) for ( Edge* e = H[u] ; e ; e = e -> next )
#define clr( a , x ) memset ( a , x , sizeof a )
#define cpy( a , x ) memcpy ( a , x , sizeof a )
#define ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define lson ls , l , m
#define rson rs , m + 1 , r
#define mid ( ( l + r ) >> 1 )
#define root 1 , 1 , n

const int MAXN = 100005 ;
const int MAXE = 200005 ;

struct Edge {
	int v ;
	Edge* next ;
} E[MAXE] , *H[MAXN] , *edge ;

struct Seg {
	int u , v ;
	Seg () {}
	Seg ( int u , int v ) : u ( u ) , v ( v ) {}
} seg[MAXN] ;

int siz[MAXN] ;
int top[MAXN] ;
int pre[MAXN] ;
int pos[MAXN] ;
int dep[MAXN] ;
int son[MAXN] ;
int tree_idx ;
int ans1[MAXN] , ans2[MAXN] ;
int idx[MAXN] ;
int seg_idx[MAXN] ;
LL T[2][MAXN] ;
LL addv[2][MAXN << 2] ;
int n , q ;

void clear () {
	edge = E ;
	tree_idx = 0 ;
	pre[1] = 0 ;
	siz[0] = 0 ;
	clr ( H , 0 ) ;
	clr ( T , 0 ) ;
}

void addedge ( int u , int v ) {
	edge -> v = v ;
	edge -> next = H[u] ;
	H[u] = edge ++ ;
}

void dfs ( int u ) {
	siz[u] = 1 ;
	son[u] = 0 ;
	travel ( e , H , u ) {
		int v = e -> v ;
		if ( v != pre[u] ) {
			pre[v] = u ;
			dep[v] = dep[u] + 1 ;
			dfs ( v ) ;
			siz[u] += siz[v] ;
			if ( siz[v] > siz[son[u]] ) son[u] = v ;
		}
	}
}

void rewrite ( int u , int top_element ) {
	top[u] = top_element ;
	pos[u] = ++ tree_idx ;
	idx[tree_idx] = u ;
	if ( son[u] ) rewrite ( son[u] , top_element ) ;
	travel ( e , H , u ) {
		int v = e -> v ;
		if ( v != son[u] && v != pre[u]) rewrite ( v , v ) ;
	}
}

void add ( int x , int o , int v ) {
	while ( x ) {
		T[o][x] += v ;
		x -= x & -x ;
	}
}

LL sum ( int x , int o , LL ans = 0 ) {
	while ( x <= n ) {
		ans += T[o][x] ;
		x += x & -x ;
	}
	return ans ;
}

void pushdown ( int x , int o ) {
	if ( addv[x][o] ) {
		addv[x][ls] += addv[x][o] ;
		addv[x][rs] += addv[x][o] ;
		addv[x][o] = 0 ;
	}
}

void sub_update ( int L , int R , int x , int v , int o , int l , int r ) {
	if ( L <= l && r <= R ) {
		addv[x][o] += v ;
		return ;
	}
	int m = mid ;
	pushdown ( x , o ) ;
	if ( L <= m ) sub_update ( L , R , x , v , lson ) ;
	if ( m <  R ) sub_update ( L , R , x , v , rson ) ;
}

void update ( int x , int y , int o , int v ) {
	while ( top[x] != top[y] ) {
		if ( dep[top[x]] > dep[top[y]] ) {
			add ( pos[x] , o , v ) ;
			add ( pos[top[x]] - 1 , o , -v ) ;
			//sub_update ( pos[top[x]] , pos[x] , o , v , root ) ;
			x = pre[top[x]] ;
		} else {
			add ( pos[y] , o , v ) ;
			add ( pos[top[y]] - 1 , o , -v ) ;
			//sub_update ( pos[top[y]] , pos[y] , o , v , root ) ;
			y = pre[top[y]] ;
		}
	}
	if ( o && x == y ) return ;
	if ( dep[x] > dep[y] ) {
		add ( pos[x] , o , v ) ;
		add ( pos[y] + o - 1 , o , -v ) ;
		//sub_update ( pos[y] + o , pos[x] , o , v , root ) ;
	} else {
		add ( pos[y] , o , v ) ;
		add ( pos[x] + o - 1 , o , -v ) ;
		//sub_update ( pos[x] + o , pos[y] , o , v , root ) ;
	}
}

void down ( int o , int l , int r ) {
	if ( l == r ) {
		ans1[idx[l]] = addv[0][o] ;
		ans2[seg_idx[l]] = addv[1][o] ;
		return ;
	}
	int m = mid ;
	pushdown ( 0 , o ) ;
	pushdown ( 1 , o ) ;
	down ( lson ) ;
	down ( rson ) ;
}

void scanf ( int& x , char c = 0 , int flag = 0 ) {
	while ( ( c = getchar () ) != '-' && ( c < '0' || c > '9' ) ) ;
	if ( c == '-' ) flag = 1 , x = 0 ;
	else x = c - '0' ;
	while ( ( c = getchar () ) >= '0' && c <= '9' ) x = x * 10 + c - '0' ;
	if ( flag ) x = -x ;
}

void solve () {
	char buf[10] ;
	int u , v , w ;
	clear () ;
	scanf ( "%d%d" , &n , &q ) ;
	rep ( i , 1 , n ) {
		scanf ( u ) , scanf ( v ) ;
		seg[i] = Seg ( u , v ) ;
		addedge ( u , v ) ;
		addedge ( v , u ) ;
	}
	dfs ( 1 ) ;
	rewrite ( 1 , 1 ) ;
	rep ( i , 1 , n ) {
		u = seg[i].u ;
		v = seg[i].v ;
		if ( dep[u] < dep[v] ) seg_idx[pos[v]] = i ;
		else seg_idx[pos[u]] = i ;
	}
	while ( q -- ) {
		scanf ( "%s" , buf ) ;
		scanf ( u ) , scanf ( v ) , scanf ( w ) ;
		if ( buf[3] == '1' ) update ( u , v , 0 , w ) ;
		else update ( u , v , 1 , w ) ;
	}
	For ( i , 1 , n ) ans1[idx[i]] = sum ( i , 0 ) ;
	For ( i , 2 , n ) ans2[seg_idx[i]] = sum ( i , 1 ) ;
	//down ( root ) ;
	For ( i , 1 , n ) printf ( "%d%s" , ans1[i] , i < n ? " " : "" ) ;
	printf ( "\n" ) ;
	rep ( i , 1 , n ) printf ( "%d%s" , ans2[i] , i < n - 1 ? " " : "" ) ;
	printf ( "\n" ) ;
}

int main () {
	int T , cas = 0 ;
	scanf ( "%d", &T ) ;
	while ( T -- ) {
		printf ( "Case #%d:\n" , ++ cas ) ;
		solve () ;
	}
	return 0 ;
}

O(nlogn + n)代码如下:


#include 
#include 
#include 
#include 
using namespace std ;

typedef long long LL ;

#pragma comment(linker, "/STACK:16777216")
#define rep( i , a , b ) for ( int i = a ; i < b ; ++ i )
#define For( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define rev( i , a , b ) for ( int i = a ; i >= b ; -- i )
#define travel( e , H , u ) for ( Edge* e = H[u] ; e ; e = e -> next )
#define clr( a , x ) memset ( a , x , sizeof a )
#define cpy( a , x ) memcpy ( a , x , sizeof a )
#define ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define lson ls , l , m
#define rson rs , m + 1 , r
#define mid ( ( l + r ) >> 1 )
#define root 1 , 1 , n

const int MAXN = 100005 ;
const int MAXE = 200005 ;

struct Edge {
	int v ;
	Edge* next ;
} E[MAXE] , *H[MAXN] , *edge ;

struct Seg {
	int u , v ;
	Seg () {}
	Seg ( int u , int v ) : u ( u ) , v ( v ) {}
} seg[MAXN] ;

int siz[MAXN] ;
int top[MAXN] ;
int pre[MAXN] ;
int pos[MAXN] ;
int dep[MAXN] ;
int son[MAXN] ;
int tree_idx ;
LL ans1[MAXN] , ans2[MAXN] ;
int idx[MAXN] ;
int seg_idx[MAXN] ;
LL a[2][MAXN] ;
int n , q ;

void clear () {
	edge = E ;
	tree_idx = 0 ;
	pre[1] = 0 ;
	siz[0] = 0 ;
	clr ( H , 0 ) ;
	clr ( a , 0 ) ;
}

void addedge ( int u , int v ) {
	edge -> v = v ;
	edge -> next = H[u] ;
	H[u] = edge ++ ;
}

void dfs ( int u ) {
	siz[u] = 1 ;
	son[u] = 0 ;
	travel ( e , H , u ) {
		int v = e -> v ;
		if ( v != pre[u] ) {
			pre[v] = u ;
			dep[v] = dep[u] + 1 ;
			dfs ( v ) ;
			siz[u] += siz[v] ;
			if ( siz[v] > siz[son[u]] ) son[u] = v ;
		}
	}
}

void rewrite ( int u , int top_element ) {
	top[u] = top_element ;
	pos[u] = ++ tree_idx ;
	idx[tree_idx] = u ;
	if ( son[u] ) rewrite ( son[u] , top_element ) ;
	travel ( e , H , u ) {
		int v = e -> v ;
		if ( v != son[u] && v != pre[u]) rewrite ( v , v ) ;
	}
}

void update ( int x , int y , int o , int v ) {
	while ( top[x] != top[y] ) {
		if ( dep[top[x]] > dep[top[y]] ) {
			a[o][pos[top[x]]] += v ;
			a[o][pos[x] + 1] -= v ;
			x = pre[top[x]] ;
		} else {
			a[o][pos[top[y]]] += v ;
			a[o][pos[y] + 1] -= v ;
			y = pre[top[y]] ;
		}
	}
	if ( o && x == y ) return ;
	if ( dep[x] > dep[y] ) {
		a[o][pos[y] + o] += v ;
		a[o][pos[x] + 1] -= v ;
	} else {
		a[o][pos[x] + o] += v ;
		a[o][pos[y] + 1] -= v ;
	}
}

void scanf ( int& x , char c = 0 , int flag = 0 ) {
	while ( ( c = getchar () ) != '-' && ( c < '0' || c > '9' ) ) ;
	if ( c == '-' ) flag = 1 , x = 0 ;
	else x = c - '0' ;
	while ( ( c = getchar () ) >= '0' && c <= '9' ) x = x * 10 + c - '0' ;
	if ( flag ) x = -x ;
}

void solve () {
	char buf[10] ;
	int u , v , w ;
	clear () ;
	scanf ( "%d%d" , &n , &q ) ;
	rep ( i , 1 , n ) {
		scanf ( u ) , scanf ( v ) ;
		seg[i] = Seg ( u , v ) ;
		addedge ( u , v ) ;
		addedge ( v , u ) ;
	}
	dfs ( 1 ) ;
	rewrite ( 1 , 1 ) ;
	rep ( i , 1 , n ) {
		u = seg[i].u ;
		v = seg[i].v ;
		if ( dep[u] < dep[v] ) seg_idx[pos[v]] = i ;
		else                   seg_idx[pos[u]] = i ;
	}
	while ( q -- ) {
		scanf ( "%s" , buf ) ;
		scanf ( u ) , scanf ( v ) , scanf ( w ) ;
		if ( buf[3] == '1' ) update ( u , v , 0 , w ) ;
		else update ( u , v , 1 , w ) ;
	}
	LL x = 0 , y = 0 ;
	For ( i , 1 , n ) {
		x += a[0][i] ;
		ans1[idx[i]] = x ;
	}
	For ( i , 2 , n ) {
		y += a[1][i] ;
		ans2[seg_idx[i]] = y ;
	}
	For ( i , 1 , n ) printf ( "%I64d%s" , ans1[i] , i < n ? " " : "" ) ;
	printf ( "\n" ) ;
	rep ( i , 1 , n ) printf ( "%I64d%s" , ans2[i] , i < n - 1 ? " " : "" ) ;
	printf ( "\n" ) ;
}

int main () {
	int T , cas = 0 ;
	scanf ( "%d", &T ) ;
	while ( T -- ) {
		printf ( "Case #%d:\n" , ++ cas ) ;
		solve () ;
	}
	return 0 ;
}


你可能感兴趣的:(树链剖分)