QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#28074#674. Ascending TreeenhWA 2ms11852kbC++2.6kb2022-04-11 22:00:472022-04-29 08:42:57

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-04-29 08:42:57]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:11852kb
  • [2022-04-11 22:00:47]
  • 提交

answer

#include <iostream>
#include <vector>
using namespace std ;

#define int long long
const int N = 100005 ;
int n , a[N] , t[N][2] , fa[N] , dep[N] , top[N] , clr[N] , ans ;

inline int Abs ( int x ) {
	if ( x < 0 ) return -x ;
	return x ;
}

struct Edge {
	int nxt , to ;
} edge[N] ;

int cnt , head[N] ;
void insert ( int u , int v ) {
	edge [ ++ cnt ] = { head [ u ] , v } ;
	head [ u ] = cnt ;
}

void dfs1 ( int x , int d ) {
	a [ x ] += d ;
	for ( int i = head [ x ] ; i ; i = edge [ i ] .nxt ) {
		int y = edge [ i ] .to ;
		fa [ y ] = x ;
		dfs1 ( y , d + 1 ) ;
	}
}

struct Node {
	int rt , siz , sz , val ;
} st[N] ;
//siz: the number of nodes in the left-weight tree
//sz: the number of nodes in the together set

int merge ( int x , int y ) {
	if ( ! x || ! y )
		return x | y ;
	if ( a [ x ] > a [ y ] )
		swap ( x , y ) ;
	t [ x ] [ 1 ] = merge ( t [ x ] [ 1 ] , y ) ;
	if ( dep [ t [ x ] [ 1 ] ] > dep [ t [ x ] [ 0 ] ] )
		swap ( t [ x ] [ 0 ] , t [ x ] [ 1 ] ) ;
	dep [ x ] = dep [ t [ x ] [ 0 ] ] + 1 ;
	return x ;
}

int find ( int x ) {
	if ( clr [ x ] == x )
		return x ;
	return clr [ x ] = find ( clr [ x ] ) ;
}

void updata ( int x ) {
	if ( top [ x ] == 1 ) return ;
	int y = fa [ top [ x ] ] ;
	if ( st [ find ( x ) ] .val > st [ find ( y ) ] .val ) {
		st [ find ( y ) ] .rt = merge ( st [ find ( x ) ] .rt , st [ find ( y ) ] .rt ) ;
		st [ find ( y ) ] .siz += st [ find ( x ) ] .siz ;
		st [ find ( y ) ] .sz += st [ find ( x ) ] .sz ;
		clr [ find ( x ) ] = find ( y ) ;
		while ( st [ find ( x ) ] .siz > st [ find ( x ) ] .sz / 2 + 1 ) {
			-- st [ find ( x ) ] .siz ;
			int u = st [ find ( x ) ] .rt ;
			st [ find ( x ) ] .rt = merge ( t [ u ] [ 0 ] , t [ u ] [ 1 ] ) ;
			st [ find ( x ) ] .val = a [ st [ find ( x ) ] .rt ] ;
		}
		top [ x ] = top [ y ] ;
	}
}

void dfs ( int x , int la ) {
	st [ x ] = Node { x , 1 , 1 , a [ x ] } ;
	top [ x ] = x ;
	clr [ x ] = x ;
	updata ( x ) ;
	for ( int i = head [ x ] ; i ; i = edge [ i ] .nxt ) {
		int y = edge [ i ] .to ;
		dfs ( y , x ) ;
		updata ( x ) ;
	}
}

signed main ( ) {
	cin >> n >> a [ 1 ] ;
	for ( int i = 2 ; i <= n ; ++ i ) {
		int u ;
		cin >> u >> a [ i ] ;
		insert ( u , i ) ;
	}
	dfs1 ( 1 , 0 ) ;
//	cout << "   a : " ;
//	for ( int i = 1 ; i <= n ; ++ i )
//		cout << a [ i ] << "  " ;
//	cout << "\n" ;
	dfs ( 1 , 0 ) ;
//	cout << " val : " ;
//	for ( int i = 1 ; i <= n ; ++ i )
//		cout << st [ find ( i ) ] .val << "  " ;
//	cout << "\n" ;
	for ( int i = 1 ; i <= n ; ++ i )
		ans += Abs ( a [ i ] - st [ find ( i ) ] .val ) ;
	cout << ans << "\n" ;
	return 0 ;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 11852kb

input:

21 -28
1 -21
2 -7
3 -3
4 -44
4 21
4 -39
4 42
2 -49
9 16
8 -22
5 -49
11 -8
1 3
11 28
3 8
8 -38
16 34
4 -45
7 -43
2 7

output:

295

result:

wrong answer 1st lines differ - expected: '290', found: '295'