답안 #346703

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
346703 2021-01-10T18:37:38 Z CaroLinda Construction of Highway (JOI18_construction) C++14
100 / 100
310 ms 21092 KB
#include <bits/stdc++.h>

#define ll long long
#define sz(x) x.size()
#define all(x) x.begin(),x.end()

const int MAXN = 1e5+10 ;

using namespace std ;

struct Bit
{

	int bit[MAXN] ;

	void upd(int i, int val) { for(; i < MAXN ; i += i &-i ) bit[i] += val ; }
	int qry(int i )
	{
		int tot = 0 ;
		for(; i > 0 ; i -= i &-i ) tot += bit[i] ;
		return tot ;
	}

} bit ;

int N ;
int C[MAXN] ;
int sub[MAXN] , lvl[MAXN] ;
int parent[MAXN] , myChain[MAXN] , chainHead[MAXN] ;
int chainInd[MAXN] ;
vector<int> adj[MAXN] ;
vector< vector<pair<int,int> > > chain ;

void dfs(int x)
{
	sub[x] = 1 ;
	for(auto e : adj[x] )
	{
		parent[e] = x ;
		lvl[e] = lvl[x] + 1 ;
		dfs(e) ;
		sub[x] += sub[e] ;
	}
}

int currChain = 1 ;
void dfsHld(int x)
{
	
	int bc = -1 ;

	for(auto e : adj[x] )
		if( bc == -1 || sub[bc] < sub[e] ) bc = e ;
	
	if( bc != -1 )
	{
		myChain[bc] = currChain ;
		chainInd[bc] = chainInd[x] + 1 ;
		dfsHld( bc ) ;
	}

	for(auto e : adj[x] )
	{
		if( e == bc ) continue ;

		myChain[e] = ++currChain ;
		chainHead[ currChain ] = e ;
	   chainInd[e] = 1 ;
	   chain.push_back( vector<pair<int,int> >() ) ;

		dfsHld(e) ;

	}
	
}

int main()
{
	vector<int> p ;

	scanf("%d", &N ) ;
	for(int i = 1 ; i <= N ; i++ ) 
	{
		scanf("%d", &C[i] ) ;
		p.push_back(C[i] ) ;
	}

	sort(all(p) ) ;
	p.erase(unique(all(p)) , p.end() ) ;

	for(int i = 1 ; i <= N ; i++ ) C[i] = lower_bound( all(p) , C[i] ) - p.begin() + 1 ;

	vector<int> orderOfInsertion ;

	for(int i = 1 , a , b ; i < N ; i++ )
	{
		scanf("%d %d", &a, &b ) ;
		
		orderOfInsertion.push_back(b) ;

		adj[a].push_back(b) ;

	}
	
	dfs(1) ;

	chain.push_back( vector<pair<int,int> >() ) ;
	chain.push_back( vector<pair<int,int> >() ) ;
	chainHead[1] = myChain[1] = chainInd[1] = 1 ;

	chain[1].push_back( make_pair(1, C[1] ) ) ;

	dfsHld(1) ;

	/*for(int i = 1 ; i <= N ; i++ ) cout << myChain[i] << " " << i<< endl ;
	for(int i = 1 ; i <= currChain ; i++ ) cout << i << " " << chainHead[i] << endl ; */

	for(auto B : orderOfInsertion )
	{
		int A = parent[B] ;

		vector< pair<int,int > > v ;

		ll ans = 0LL ;

		while(A)
		{
			int id = myChain[A] ;
			int went = 0 ;

			int x = sz(v) ;

			for(int i = sz(chain[id] )-1 ; went < chainInd[A] ; i-- )
			{
				if( went + chain[id][i].first > chainInd[A] )
				{
					v.push_back( make_pair(chainInd[A] - went, chain[id][i].second ) ) ;
					chain[id][i].first -= (chainInd[A] - went ) ;

				}
				else
				{
					v.push_back( chain[id][i] ) ;
					chain[id].pop_back() ;
				}

				went += v.back().first ;

			}

			reverse( v.begin() + x , v.end() ) ;
			chain[id].push_back(make_pair(chainInd[A] + (id == myChain[B] ? 1 : 0 ), C[B] ) ) ;

			A = parent[ chainHead[id] ] ;

		}

		if( chainInd[B] == 1 ) chain[ myChain[B] ].push_back( make_pair(1, C[B] ) ) ;

		for(auto e : v )
		{
			ans += (ll)e.first * bit.qry( e.second - 1 ) ;
			bit.upd( e.second , e.first ) ;
		}
		for(auto e : v ) bit.upd(e.second , -e.first ) ;

		printf("%lld\n", ans ) ;

	}	

}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:81:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   81 |  scanf("%d", &N ) ;
      |  ~~~~~^~~~~~~~~~~
construction.cpp:84:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   84 |   scanf("%d", &C[i] ) ;
      |   ~~~~~^~~~~~~~~~~~~~
construction.cpp:97:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   97 |   scanf("%d %d", &a, &b ) ;
      |   ~~~~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2796 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 2 ms 2796 KB Output is correct
4 Correct 2 ms 2796 KB Output is correct
5 Correct 3 ms 2796 KB Output is correct
6 Correct 3 ms 2796 KB Output is correct
7 Correct 3 ms 2796 KB Output is correct
8 Correct 2 ms 2796 KB Output is correct
9 Correct 2 ms 2796 KB Output is correct
10 Correct 3 ms 2796 KB Output is correct
11 Correct 3 ms 2796 KB Output is correct
12 Correct 3 ms 2796 KB Output is correct
13 Correct 3 ms 2796 KB Output is correct
14 Correct 3 ms 2796 KB Output is correct
15 Correct 3 ms 2796 KB Output is correct
16 Correct 3 ms 2796 KB Output is correct
17 Correct 3 ms 2796 KB Output is correct
18 Correct 3 ms 2796 KB Output is correct
19 Correct 2 ms 2796 KB Output is correct
20 Correct 3 ms 2796 KB Output is correct
21 Correct 3 ms 2796 KB Output is correct
22 Correct 3 ms 2796 KB Output is correct
23 Correct 3 ms 2796 KB Output is correct
24 Correct 3 ms 2796 KB Output is correct
25 Correct 3 ms 2796 KB Output is correct
26 Correct 2 ms 2796 KB Output is correct
27 Correct 3 ms 2796 KB Output is correct
28 Correct 2 ms 2796 KB Output is correct
29 Correct 2 ms 2796 KB Output is correct
30 Correct 3 ms 2796 KB Output is correct
31 Correct 3 ms 2796 KB Output is correct
32 Correct 2 ms 2796 KB Output is correct
33 Correct 2 ms 2796 KB Output is correct
34 Correct 3 ms 2796 KB Output is correct
35 Correct 3 ms 2796 KB Output is correct
36 Correct 3 ms 2796 KB Output is correct
37 Correct 2 ms 2796 KB Output is correct
38 Correct 3 ms 2796 KB Output is correct
39 Correct 2 ms 2796 KB Output is correct
40 Correct 2 ms 2796 KB Output is correct
41 Correct 3 ms 2796 KB Output is correct
42 Correct 3 ms 2796 KB Output is correct
43 Correct 3 ms 2796 KB Output is correct
44 Correct 2 ms 2796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2796 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 2 ms 2796 KB Output is correct
4 Correct 2 ms 2796 KB Output is correct
5 Correct 3 ms 2796 KB Output is correct
6 Correct 3 ms 2796 KB Output is correct
7 Correct 3 ms 2796 KB Output is correct
8 Correct 2 ms 2796 KB Output is correct
9 Correct 2 ms 2796 KB Output is correct
10 Correct 3 ms 2796 KB Output is correct
11 Correct 3 ms 2796 KB Output is correct
12 Correct 3 ms 2796 KB Output is correct
13 Correct 3 ms 2796 KB Output is correct
14 Correct 3 ms 2796 KB Output is correct
15 Correct 3 ms 2796 KB Output is correct
16 Correct 3 ms 2796 KB Output is correct
17 Correct 3 ms 2796 KB Output is correct
18 Correct 3 ms 2796 KB Output is correct
19 Correct 2 ms 2796 KB Output is correct
20 Correct 3 ms 2796 KB Output is correct
21 Correct 3 ms 2796 KB Output is correct
22 Correct 3 ms 2796 KB Output is correct
23 Correct 3 ms 2796 KB Output is correct
24 Correct 3 ms 2796 KB Output is correct
25 Correct 3 ms 2796 KB Output is correct
26 Correct 2 ms 2796 KB Output is correct
27 Correct 3 ms 2796 KB Output is correct
28 Correct 2 ms 2796 KB Output is correct
29 Correct 2 ms 2796 KB Output is correct
30 Correct 3 ms 2796 KB Output is correct
31 Correct 3 ms 2796 KB Output is correct
32 Correct 2 ms 2796 KB Output is correct
33 Correct 2 ms 2796 KB Output is correct
34 Correct 3 ms 2796 KB Output is correct
35 Correct 3 ms 2796 KB Output is correct
36 Correct 3 ms 2796 KB Output is correct
37 Correct 2 ms 2796 KB Output is correct
38 Correct 3 ms 2796 KB Output is correct
39 Correct 2 ms 2796 KB Output is correct
40 Correct 2 ms 2796 KB Output is correct
41 Correct 3 ms 2796 KB Output is correct
42 Correct 3 ms 2796 KB Output is correct
43 Correct 3 ms 2796 KB Output is correct
44 Correct 2 ms 2796 KB Output is correct
45 Correct 3 ms 2796 KB Output is correct
46 Correct 8 ms 3180 KB Output is correct
47 Correct 8 ms 3180 KB Output is correct
48 Correct 8 ms 3180 KB Output is correct
49 Correct 6 ms 3436 KB Output is correct
50 Correct 6 ms 3436 KB Output is correct
51 Correct 6 ms 3436 KB Output is correct
52 Correct 6 ms 3308 KB Output is correct
53 Correct 6 ms 3436 KB Output is correct
54 Correct 6 ms 3436 KB Output is correct
55 Correct 6 ms 3436 KB Output is correct
56 Correct 6 ms 3308 KB Output is correct
57 Correct 9 ms 3180 KB Output is correct
58 Correct 9 ms 3180 KB Output is correct
59 Correct 9 ms 3180 KB Output is correct
60 Correct 9 ms 3180 KB Output is correct
61 Correct 6 ms 3308 KB Output is correct
62 Correct 6 ms 3308 KB Output is correct
63 Correct 6 ms 3308 KB Output is correct
64 Correct 7 ms 3180 KB Output is correct
65 Correct 9 ms 3180 KB Output is correct
66 Correct 8 ms 3180 KB Output is correct
67 Correct 8 ms 3180 KB Output is correct
68 Correct 5 ms 3436 KB Output is correct
69 Correct 6 ms 3308 KB Output is correct
70 Correct 5 ms 3308 KB Output is correct
71 Correct 5 ms 3328 KB Output is correct
72 Correct 9 ms 3180 KB Output is correct
73 Correct 9 ms 3180 KB Output is correct
74 Correct 5 ms 3308 KB Output is correct
75 Correct 7 ms 3180 KB Output is correct
76 Correct 6 ms 3180 KB Output is correct
77 Correct 6 ms 3180 KB Output is correct
78 Correct 6 ms 3180 KB Output is correct
79 Correct 6 ms 3180 KB Output is correct
80 Correct 6 ms 3180 KB Output is correct
81 Correct 7 ms 3180 KB Output is correct
82 Correct 7 ms 3180 KB Output is correct
83 Correct 7 ms 3180 KB Output is correct
84 Correct 7 ms 3180 KB Output is correct
85 Correct 6 ms 3180 KB Output is correct
86 Correct 6 ms 3180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2796 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 2 ms 2796 KB Output is correct
4 Correct 2 ms 2796 KB Output is correct
5 Correct 3 ms 2796 KB Output is correct
6 Correct 3 ms 2796 KB Output is correct
7 Correct 3 ms 2796 KB Output is correct
8 Correct 2 ms 2796 KB Output is correct
9 Correct 2 ms 2796 KB Output is correct
10 Correct 3 ms 2796 KB Output is correct
11 Correct 3 ms 2796 KB Output is correct
12 Correct 3 ms 2796 KB Output is correct
13 Correct 3 ms 2796 KB Output is correct
14 Correct 3 ms 2796 KB Output is correct
15 Correct 3 ms 2796 KB Output is correct
16 Correct 3 ms 2796 KB Output is correct
17 Correct 3 ms 2796 KB Output is correct
18 Correct 3 ms 2796 KB Output is correct
19 Correct 2 ms 2796 KB Output is correct
20 Correct 3 ms 2796 KB Output is correct
21 Correct 3 ms 2796 KB Output is correct
22 Correct 3 ms 2796 KB Output is correct
23 Correct 3 ms 2796 KB Output is correct
24 Correct 3 ms 2796 KB Output is correct
25 Correct 3 ms 2796 KB Output is correct
26 Correct 2 ms 2796 KB Output is correct
27 Correct 3 ms 2796 KB Output is correct
28 Correct 2 ms 2796 KB Output is correct
29 Correct 2 ms 2796 KB Output is correct
30 Correct 3 ms 2796 KB Output is correct
31 Correct 3 ms 2796 KB Output is correct
32 Correct 2 ms 2796 KB Output is correct
33 Correct 2 ms 2796 KB Output is correct
34 Correct 3 ms 2796 KB Output is correct
35 Correct 3 ms 2796 KB Output is correct
36 Correct 3 ms 2796 KB Output is correct
37 Correct 2 ms 2796 KB Output is correct
38 Correct 3 ms 2796 KB Output is correct
39 Correct 2 ms 2796 KB Output is correct
40 Correct 2 ms 2796 KB Output is correct
41 Correct 3 ms 2796 KB Output is correct
42 Correct 3 ms 2796 KB Output is correct
43 Correct 3 ms 2796 KB Output is correct
44 Correct 2 ms 2796 KB Output is correct
45 Correct 3 ms 2796 KB Output is correct
46 Correct 8 ms 3180 KB Output is correct
47 Correct 8 ms 3180 KB Output is correct
48 Correct 8 ms 3180 KB Output is correct
49 Correct 6 ms 3436 KB Output is correct
50 Correct 6 ms 3436 KB Output is correct
51 Correct 6 ms 3436 KB Output is correct
52 Correct 6 ms 3308 KB Output is correct
53 Correct 6 ms 3436 KB Output is correct
54 Correct 6 ms 3436 KB Output is correct
55 Correct 6 ms 3436 KB Output is correct
56 Correct 6 ms 3308 KB Output is correct
57 Correct 9 ms 3180 KB Output is correct
58 Correct 9 ms 3180 KB Output is correct
59 Correct 9 ms 3180 KB Output is correct
60 Correct 9 ms 3180 KB Output is correct
61 Correct 6 ms 3308 KB Output is correct
62 Correct 6 ms 3308 KB Output is correct
63 Correct 6 ms 3308 KB Output is correct
64 Correct 7 ms 3180 KB Output is correct
65 Correct 9 ms 3180 KB Output is correct
66 Correct 8 ms 3180 KB Output is correct
67 Correct 8 ms 3180 KB Output is correct
68 Correct 5 ms 3436 KB Output is correct
69 Correct 6 ms 3308 KB Output is correct
70 Correct 5 ms 3308 KB Output is correct
71 Correct 5 ms 3328 KB Output is correct
72 Correct 9 ms 3180 KB Output is correct
73 Correct 9 ms 3180 KB Output is correct
74 Correct 5 ms 3308 KB Output is correct
75 Correct 7 ms 3180 KB Output is correct
76 Correct 6 ms 3180 KB Output is correct
77 Correct 6 ms 3180 KB Output is correct
78 Correct 6 ms 3180 KB Output is correct
79 Correct 6 ms 3180 KB Output is correct
80 Correct 6 ms 3180 KB Output is correct
81 Correct 7 ms 3180 KB Output is correct
82 Correct 7 ms 3180 KB Output is correct
83 Correct 7 ms 3180 KB Output is correct
84 Correct 7 ms 3180 KB Output is correct
85 Correct 6 ms 3180 KB Output is correct
86 Correct 6 ms 3180 KB Output is correct
87 Correct 18 ms 3756 KB Output is correct
88 Correct 53 ms 5868 KB Output is correct
89 Correct 249 ms 13404 KB Output is correct
90 Correct 218 ms 13276 KB Output is correct
91 Correct 216 ms 13276 KB Output is correct
92 Correct 116 ms 21092 KB Output is correct
93 Correct 116 ms 21092 KB Output is correct
94 Correct 118 ms 21092 KB Output is correct
95 Correct 124 ms 18652 KB Output is correct
96 Correct 123 ms 19164 KB Output is correct
97 Correct 120 ms 19036 KB Output is correct
98 Correct 120 ms 19036 KB Output is correct
99 Correct 123 ms 17884 KB Output is correct
100 Correct 258 ms 12636 KB Output is correct
101 Correct 306 ms 13116 KB Output is correct
102 Correct 310 ms 12920 KB Output is correct
103 Correct 282 ms 12892 KB Output is correct
104 Correct 144 ms 18012 KB Output is correct
105 Correct 142 ms 17884 KB Output is correct
106 Correct 141 ms 17884 KB Output is correct
107 Correct 203 ms 12252 KB Output is correct
108 Correct 201 ms 12252 KB Output is correct
109 Correct 236 ms 12636 KB Output is correct
110 Correct 105 ms 20068 KB Output is correct
111 Correct 117 ms 18652 KB Output is correct
112 Correct 107 ms 18012 KB Output is correct
113 Correct 111 ms 16860 KB Output is correct
114 Correct 263 ms 12636 KB Output is correct
115 Correct 284 ms 11996 KB Output is correct
116 Correct 152 ms 17028 KB Output is correct
117 Correct 124 ms 15324 KB Output is correct
118 Correct 127 ms 14556 KB Output is correct
119 Correct 134 ms 14044 KB Output is correct
120 Correct 122 ms 14556 KB Output is correct
121 Correct 112 ms 13660 KB Output is correct
122 Correct 111 ms 13276 KB Output is correct
123 Correct 145 ms 15196 KB Output is correct
124 Correct 155 ms 14428 KB Output is correct
125 Correct 146 ms 14172 KB Output is correct
126 Correct 139 ms 14728 KB Output is correct
127 Correct 131 ms 13788 KB Output is correct
128 Correct 144 ms 13404 KB Output is correct