답안 #595660

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
595660 2022-07-13T23:46:22 Z Bobaa Construction of Highway (JOI18_construction) C++17
100 / 100
443 ms 93876 KB
#include <bits/stdc++.h>
using namespace std; 

const int maxn = 1e5 + 5; 

int lv[maxn], sz[maxn], par[maxn], tree[maxn], in[maxn], out[maxn], tin, sizeofchain[maxn]; 
map<int, int> mp; 
vector<int> v[maxn], tocmp; 
deque<pair<int, int>> vec[maxn];
int bigchild[maxn], chain[maxn], n, a[maxn], b[maxn], c[maxn]; 

void upd(int idx, int val) {
	for (int i = idx; i < maxn; i+= i & (-i)) tree[i] += val; 
} 

int solve(int idx) {
	int pas = 0; 
	for (int i = idx; i > 0; i -= i & (-i)) pas += tree[i]; 
	return pas;
}

void dfs1(int a, int p) {
	sz[a] = 1; 
	lv[a] = lv[p] + 1; 
	par[a] = p; 
	for (int i = 0; i < v[a].size(); i++) {
		int to = v[a][i]; 
		if (to == p) continue; 
		dfs1(to, a); 
		sz[a] += sz[to]; 
		if (!bigchild[a] || sz[bigchild[a]] < sz[to]) bigchild[a] = to; 
	}
}

void dfs2(int a, int p) {
	tin++; 
	in[a] = tin; 
	if (bigchild[a]) dfs2(bigchild[a], a); 
	for (int i = 0; i < v[a].size(); i++) {
		int to = v[a][i]; 
		if (to == p || to == bigchild[a]) continue; 
		dfs2(to, a); 
	}
	out[a] = tin; 
}

void chain_dfs(int a, int p) {
	if (bigchild[a]) chain[bigchild[a]] = chain[a]; 
	sizeofchain[a]++; 
	if (!vec[chain[a]].size()) vec[chain[a]].push_back({c[a], 1}); 
	else {
		if (vec[chain[a]].back().first == c[a]) vec[chain[a]].back().second++; 
		else vec[chain[a]].push_back({c[a], 1}); 
	}
	for (int i = 0; i < v[a].size(); i++) {
		int to = v[a][i]; 
		if (to == p) continue; 
		chain_dfs(to, a); 
	}
}

int solve(int a, int b) {
	int res = 0; 
	int lst = chain[a]; 
	int cur = a; 
	vector<pair<int, int>> pas; 
	while (true) {
		lst = chain[cur]; 
		int x = lv[cur] - lv[lst] + 1; 
		vector<pair<int, int>> vv; 
		int sum = 0; 
		for (int i = 0; i < vec[lst].size(); i++) {
			if (vec[lst][i].second + sum > x) {
				vv.push_back({vec[lst][i].first, (x - sum)}); 
				break; 
			}
			sum += vec[lst][i].second; 
			vv.push_back({vec[lst][i].first, vec[lst][i].second}); 
			if (sum == x) break; 
		}
		reverse(vv.begin(), vv.end()); 
		for (int i = 0; i < vv.size(); i++) pas.push_back({vv[i].first, vv[i].second}); 
		cur = par[lst]; 
		if (cur == 0) break; 
	}
	for (int i = 0; i < pas.size(); i++) {
		res += solve(pas[i].first - 1) * pas[i].second; 
		upd(pas[i].first, pas[i].second); 
	}
	for (int i = 0; i < pas.size(); i++) upd(pas[i].first, -pas[i].second); 
	return res; 
}

void toadd(int a, int b) {
	int lst = chain[a]; 
	int cur = a;
	while (true) {
		lst = chain[cur]; 
		int x = lv[cur] - lv[lst] + 1; 
		int sum = 0; 
		vector<pair<int, int>> vv; 
		while (vec[lst].size()) {
			int i = 0; 
			if (vec[lst][i].second + sum > x) {
				vec[lst][i].second = vec[lst][i].second - (x - sum); 
				break; 
			}
			sum += vec[lst][i].second; 
			vec[lst].pop_front(); 
			if (sum == x) break; 
		}
		vec[lst].push_front({c[b], x}); 
		cur = par[lst]; 
		if (cur == 0) break; 
	}
}

int main() {
	ios_base::sync_with_stdio(false); 
	cin.tie(nullptr); 
	cin >> n; 
	for (int i = 1; i <= n; i++) {
		cin >> c[i]; 
		tocmp.push_back(c[i]); 
	}
	sort(tocmp.begin(), tocmp.end()); 
	int cnt = 1; 
	mp[tocmp[0]] = 1; 
	for (int i = 1; i < tocmp.size(); i++) {
		if (tocmp[i] != tocmp[i - 1]) cnt++; 
		mp[tocmp[i]] = cnt; 
	}
	for (int i = 1; i <= n; i++) c[i] = mp[c[i]]; 
	for (int i = 1; i <= n - 1; i++) {
		cin >> a[i] >> b[i]; 
		v[a[i]].push_back(b[i]); 
		v[b[i]].push_back(a[i]); 
	}
	for (int i = 1; i <= n; i++) chain[i] = i; 
	dfs1(1, 0); 
	dfs2(1, 0); 
	chain_dfs(1, 0); 
	for (int i = 1; i <= n - 1; i++) {
		int ans = solve(a[i], b[i]); 
		toadd(a[i], b[i]); 
		cout << ans << '\n'; 
	}
}

Compilation message

construction.cpp: In function 'void dfs1(int, int)':
construction.cpp:26:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |  for (int i = 0; i < v[a].size(); i++) {
      |                  ~~^~~~~~~~~~~~~
construction.cpp: In function 'void dfs2(int, int)':
construction.cpp:39:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |  for (int i = 0; i < v[a].size(); i++) {
      |                  ~~^~~~~~~~~~~~~
construction.cpp: In function 'void chain_dfs(int, int)':
construction.cpp:55:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |  for (int i = 0; i < v[a].size(); i++) {
      |                  ~~^~~~~~~~~~~~~
construction.cpp: In function 'int solve(int, int)':
construction.cpp:72:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |   for (int i = 0; i < vec[lst].size(); i++) {
      |                   ~~^~~~~~~~~~~~~~~~~
construction.cpp:82:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |   for (int i = 0; i < vv.size(); i++) pas.push_back({vv[i].first, vv[i].second});
      |                   ~~^~~~~~~~~~~
construction.cpp:86:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |  for (int i = 0; i < pas.size(); i++) {
      |                  ~~^~~~~~~~~~~~
construction.cpp:90:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |  for (int i = 0; i < pas.size(); i++) upd(pas[i].first, -pas[i].second);
      |                  ~~^~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:129:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |  for (int i = 1; i < tocmp.size(); i++) {
      |                  ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 70076 KB Output is correct
2 Correct 41 ms 69988 KB Output is correct
3 Correct 41 ms 70100 KB Output is correct
4 Correct 41 ms 70096 KB Output is correct
5 Correct 42 ms 70128 KB Output is correct
6 Correct 44 ms 70100 KB Output is correct
7 Correct 42 ms 70072 KB Output is correct
8 Correct 43 ms 70188 KB Output is correct
9 Correct 42 ms 70148 KB Output is correct
10 Correct 41 ms 70092 KB Output is correct
11 Correct 43 ms 70184 KB Output is correct
12 Correct 41 ms 70168 KB Output is correct
13 Correct 41 ms 70048 KB Output is correct
14 Correct 41 ms 70108 KB Output is correct
15 Correct 42 ms 70092 KB Output is correct
16 Correct 43 ms 70116 KB Output is correct
17 Correct 45 ms 70148 KB Output is correct
18 Correct 42 ms 70140 KB Output is correct
19 Correct 42 ms 70164 KB Output is correct
20 Correct 41 ms 70100 KB Output is correct
21 Correct 42 ms 70092 KB Output is correct
22 Correct 45 ms 70020 KB Output is correct
23 Correct 47 ms 70124 KB Output is correct
24 Correct 48 ms 70096 KB Output is correct
25 Correct 53 ms 70204 KB Output is correct
26 Correct 42 ms 70148 KB Output is correct
27 Correct 42 ms 70116 KB Output is correct
28 Correct 41 ms 70084 KB Output is correct
29 Correct 41 ms 70136 KB Output is correct
30 Correct 43 ms 70144 KB Output is correct
31 Correct 43 ms 70124 KB Output is correct
32 Correct 47 ms 70052 KB Output is correct
33 Correct 52 ms 70132 KB Output is correct
34 Correct 41 ms 70088 KB Output is correct
35 Correct 43 ms 70140 KB Output is correct
36 Correct 41 ms 70020 KB Output is correct
37 Correct 41 ms 70092 KB Output is correct
38 Correct 49 ms 70032 KB Output is correct
39 Correct 45 ms 70140 KB Output is correct
40 Correct 44 ms 70220 KB Output is correct
41 Correct 43 ms 70140 KB Output is correct
42 Correct 43 ms 70056 KB Output is correct
43 Correct 43 ms 70012 KB Output is correct
44 Correct 47 ms 70092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 70076 KB Output is correct
2 Correct 41 ms 69988 KB Output is correct
3 Correct 41 ms 70100 KB Output is correct
4 Correct 41 ms 70096 KB Output is correct
5 Correct 42 ms 70128 KB Output is correct
6 Correct 44 ms 70100 KB Output is correct
7 Correct 42 ms 70072 KB Output is correct
8 Correct 43 ms 70188 KB Output is correct
9 Correct 42 ms 70148 KB Output is correct
10 Correct 41 ms 70092 KB Output is correct
11 Correct 43 ms 70184 KB Output is correct
12 Correct 41 ms 70168 KB Output is correct
13 Correct 41 ms 70048 KB Output is correct
14 Correct 41 ms 70108 KB Output is correct
15 Correct 42 ms 70092 KB Output is correct
16 Correct 43 ms 70116 KB Output is correct
17 Correct 45 ms 70148 KB Output is correct
18 Correct 42 ms 70140 KB Output is correct
19 Correct 42 ms 70164 KB Output is correct
20 Correct 41 ms 70100 KB Output is correct
21 Correct 42 ms 70092 KB Output is correct
22 Correct 45 ms 70020 KB Output is correct
23 Correct 47 ms 70124 KB Output is correct
24 Correct 48 ms 70096 KB Output is correct
25 Correct 53 ms 70204 KB Output is correct
26 Correct 42 ms 70148 KB Output is correct
27 Correct 42 ms 70116 KB Output is correct
28 Correct 41 ms 70084 KB Output is correct
29 Correct 41 ms 70136 KB Output is correct
30 Correct 43 ms 70144 KB Output is correct
31 Correct 43 ms 70124 KB Output is correct
32 Correct 47 ms 70052 KB Output is correct
33 Correct 52 ms 70132 KB Output is correct
34 Correct 41 ms 70088 KB Output is correct
35 Correct 43 ms 70140 KB Output is correct
36 Correct 41 ms 70020 KB Output is correct
37 Correct 41 ms 70092 KB Output is correct
38 Correct 49 ms 70032 KB Output is correct
39 Correct 45 ms 70140 KB Output is correct
40 Correct 44 ms 70220 KB Output is correct
41 Correct 43 ms 70140 KB Output is correct
42 Correct 43 ms 70056 KB Output is correct
43 Correct 43 ms 70012 KB Output is correct
44 Correct 47 ms 70092 KB Output is correct
45 Correct 46 ms 70212 KB Output is correct
46 Correct 48 ms 70604 KB Output is correct
47 Correct 51 ms 70636 KB Output is correct
48 Correct 49 ms 70612 KB Output is correct
49 Correct 46 ms 70908 KB Output is correct
50 Correct 46 ms 70896 KB Output is correct
51 Correct 46 ms 70996 KB Output is correct
52 Correct 47 ms 70776 KB Output is correct
53 Correct 51 ms 70964 KB Output is correct
54 Correct 52 ms 70980 KB Output is correct
55 Correct 46 ms 70860 KB Output is correct
56 Correct 46 ms 70732 KB Output is correct
57 Correct 49 ms 70644 KB Output is correct
58 Correct 51 ms 70660 KB Output is correct
59 Correct 50 ms 70616 KB Output is correct
60 Correct 55 ms 70708 KB Output is correct
61 Correct 49 ms 70776 KB Output is correct
62 Correct 44 ms 70804 KB Output is correct
63 Correct 47 ms 70812 KB Output is correct
64 Correct 50 ms 70604 KB Output is correct
65 Correct 51 ms 70452 KB Output is correct
66 Correct 52 ms 70396 KB Output is correct
67 Correct 47 ms 70476 KB Output is correct
68 Correct 45 ms 70776 KB Output is correct
69 Correct 47 ms 70796 KB Output is correct
70 Correct 45 ms 70576 KB Output is correct
71 Correct 45 ms 70524 KB Output is correct
72 Correct 49 ms 70632 KB Output is correct
73 Correct 57 ms 70444 KB Output is correct
74 Correct 50 ms 70492 KB Output is correct
75 Correct 46 ms 70608 KB Output is correct
76 Correct 47 ms 70708 KB Output is correct
77 Correct 48 ms 70692 KB Output is correct
78 Correct 48 ms 70496 KB Output is correct
79 Correct 50 ms 70500 KB Output is correct
80 Correct 46 ms 70484 KB Output is correct
81 Correct 47 ms 70660 KB Output is correct
82 Correct 47 ms 70660 KB Output is correct
83 Correct 49 ms 70604 KB Output is correct
84 Correct 47 ms 70448 KB Output is correct
85 Correct 46 ms 70476 KB Output is correct
86 Correct 46 ms 70472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 70076 KB Output is correct
2 Correct 41 ms 69988 KB Output is correct
3 Correct 41 ms 70100 KB Output is correct
4 Correct 41 ms 70096 KB Output is correct
5 Correct 42 ms 70128 KB Output is correct
6 Correct 44 ms 70100 KB Output is correct
7 Correct 42 ms 70072 KB Output is correct
8 Correct 43 ms 70188 KB Output is correct
9 Correct 42 ms 70148 KB Output is correct
10 Correct 41 ms 70092 KB Output is correct
11 Correct 43 ms 70184 KB Output is correct
12 Correct 41 ms 70168 KB Output is correct
13 Correct 41 ms 70048 KB Output is correct
14 Correct 41 ms 70108 KB Output is correct
15 Correct 42 ms 70092 KB Output is correct
16 Correct 43 ms 70116 KB Output is correct
17 Correct 45 ms 70148 KB Output is correct
18 Correct 42 ms 70140 KB Output is correct
19 Correct 42 ms 70164 KB Output is correct
20 Correct 41 ms 70100 KB Output is correct
21 Correct 42 ms 70092 KB Output is correct
22 Correct 45 ms 70020 KB Output is correct
23 Correct 47 ms 70124 KB Output is correct
24 Correct 48 ms 70096 KB Output is correct
25 Correct 53 ms 70204 KB Output is correct
26 Correct 42 ms 70148 KB Output is correct
27 Correct 42 ms 70116 KB Output is correct
28 Correct 41 ms 70084 KB Output is correct
29 Correct 41 ms 70136 KB Output is correct
30 Correct 43 ms 70144 KB Output is correct
31 Correct 43 ms 70124 KB Output is correct
32 Correct 47 ms 70052 KB Output is correct
33 Correct 52 ms 70132 KB Output is correct
34 Correct 41 ms 70088 KB Output is correct
35 Correct 43 ms 70140 KB Output is correct
36 Correct 41 ms 70020 KB Output is correct
37 Correct 41 ms 70092 KB Output is correct
38 Correct 49 ms 70032 KB Output is correct
39 Correct 45 ms 70140 KB Output is correct
40 Correct 44 ms 70220 KB Output is correct
41 Correct 43 ms 70140 KB Output is correct
42 Correct 43 ms 70056 KB Output is correct
43 Correct 43 ms 70012 KB Output is correct
44 Correct 47 ms 70092 KB Output is correct
45 Correct 46 ms 70212 KB Output is correct
46 Correct 48 ms 70604 KB Output is correct
47 Correct 51 ms 70636 KB Output is correct
48 Correct 49 ms 70612 KB Output is correct
49 Correct 46 ms 70908 KB Output is correct
50 Correct 46 ms 70896 KB Output is correct
51 Correct 46 ms 70996 KB Output is correct
52 Correct 47 ms 70776 KB Output is correct
53 Correct 51 ms 70964 KB Output is correct
54 Correct 52 ms 70980 KB Output is correct
55 Correct 46 ms 70860 KB Output is correct
56 Correct 46 ms 70732 KB Output is correct
57 Correct 49 ms 70644 KB Output is correct
58 Correct 51 ms 70660 KB Output is correct
59 Correct 50 ms 70616 KB Output is correct
60 Correct 55 ms 70708 KB Output is correct
61 Correct 49 ms 70776 KB Output is correct
62 Correct 44 ms 70804 KB Output is correct
63 Correct 47 ms 70812 KB Output is correct
64 Correct 50 ms 70604 KB Output is correct
65 Correct 51 ms 70452 KB Output is correct
66 Correct 52 ms 70396 KB Output is correct
67 Correct 47 ms 70476 KB Output is correct
68 Correct 45 ms 70776 KB Output is correct
69 Correct 47 ms 70796 KB Output is correct
70 Correct 45 ms 70576 KB Output is correct
71 Correct 45 ms 70524 KB Output is correct
72 Correct 49 ms 70632 KB Output is correct
73 Correct 57 ms 70444 KB Output is correct
74 Correct 50 ms 70492 KB Output is correct
75 Correct 46 ms 70608 KB Output is correct
76 Correct 47 ms 70708 KB Output is correct
77 Correct 48 ms 70692 KB Output is correct
78 Correct 48 ms 70496 KB Output is correct
79 Correct 50 ms 70500 KB Output is correct
80 Correct 46 ms 70484 KB Output is correct
81 Correct 47 ms 70660 KB Output is correct
82 Correct 47 ms 70660 KB Output is correct
83 Correct 49 ms 70604 KB Output is correct
84 Correct 47 ms 70448 KB Output is correct
85 Correct 46 ms 70476 KB Output is correct
86 Correct 46 ms 70472 KB Output is correct
87 Correct 64 ms 71536 KB Output is correct
88 Correct 113 ms 74620 KB Output is correct
89 Correct 272 ms 85392 KB Output is correct
90 Correct 325 ms 85300 KB Output is correct
91 Correct 272 ms 85320 KB Output is correct
92 Correct 178 ms 93876 KB Output is correct
93 Correct 189 ms 93780 KB Output is correct
94 Correct 179 ms 93852 KB Output is correct
95 Correct 175 ms 90240 KB Output is correct
96 Correct 185 ms 90688 KB Output is correct
97 Correct 179 ms 90648 KB Output is correct
98 Correct 185 ms 90680 KB Output is correct
99 Correct 230 ms 89636 KB Output is correct
100 Correct 350 ms 84904 KB Output is correct
101 Correct 415 ms 85412 KB Output is correct
102 Correct 375 ms 85280 KB Output is correct
103 Correct 443 ms 85336 KB Output is correct
104 Correct 195 ms 89608 KB Output is correct
105 Correct 210 ms 89624 KB Output is correct
106 Correct 202 ms 89688 KB Output is correct
107 Correct 249 ms 84108 KB Output is correct
108 Correct 266 ms 79828 KB Output is correct
109 Correct 282 ms 81440 KB Output is correct
110 Correct 137 ms 88188 KB Output is correct
111 Correct 214 ms 90324 KB Output is correct
112 Correct 139 ms 85032 KB Output is correct
113 Correct 148 ms 84044 KB Output is correct
114 Correct 344 ms 84928 KB Output is correct
115 Correct 342 ms 79772 KB Output is correct
116 Correct 176 ms 84040 KB Output is correct
117 Correct 241 ms 87124 KB Output is correct
118 Correct 188 ms 86528 KB Output is correct
119 Correct 190 ms 86044 KB Output is correct
120 Correct 159 ms 81956 KB Output is correct
121 Correct 154 ms 81344 KB Output is correct
122 Correct 171 ms 80944 KB Output is correct
123 Correct 245 ms 87180 KB Output is correct
124 Correct 205 ms 86576 KB Output is correct
125 Correct 253 ms 86156 KB Output is correct
126 Correct 167 ms 82112 KB Output is correct
127 Correct 225 ms 81352 KB Output is correct
128 Correct 159 ms 81056 KB Output is correct