Submission #888454

# Submission time Handle Problem Language Result Execution time Memory
888454 2023-12-17T12:50:51 Z rorro Cat Exercise (JOI23_ho_t4) C++17
100 / 100
332 ms 54632 KB
#pragma GCC optimize("O3,unroll-loops") 
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#include <bits/stdc++.h>

#define ll long long
#define lld long double
#define st first
#define nd second
#define mp make_pair
#define ii pair<int, int>
#define vll vector<ll>
#define vi vector<int>
#define vvi vector<vector<int> >
#define pb push_back
#define eb emplace_back
#define remax(a, b) a = max(a, b);
#define remin(a, b) a = min(a, b);
#define all(x) x.begin(), x.end()
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define bit(n, x) (n & (1 << x))
#define rep(a, b) for(int a = 0; a < b; ++a)

using namespace std;

const int maxn = 2e5;
const int maxlog = 19;
vvi up(maxn, vi(maxlog));
vvi g(maxn);
vi par(maxn);
vi sz(maxn, 1);
vi depth(maxn);
vll ans(maxn, 0);
vi h(maxn);
vi mx(maxn); //vertex, h[mx[find_(v)]] to max h w spojnej z v

void dfs(int cur, int pare) {
	depth[cur] = depth[pare]+1;
	up[cur][0] = pare;
	for (int i = 1; i < maxlog; ++i) up[cur][i] = up[up[cur][i-1]][i-1];
	for (int u : g[cur]) {
		if (u != pare) {
			dfs(u, cur);
		}
	}
}

int dist(int u, int v) {
	if (depth[u] < depth[v]) swap(u, v); //u jest glebiej niz v
	int dt = depth[u]-depth[v];
	for (int i = maxlog-1; i >= 0; --i) {
		if (depth[up[u][i]] >= depth[v]) u = up[u][i]; //{cout << "hi " << u << endl; u = up[u][i];}
	}
	//teraz sa tak samo gleboko
	if (u == v) return dt;
	//teraz sa rozne, i na tej samej glebokosci
	for (int i = maxlog-1; i >= 0; --i) {
		if (up[u][i] != up[v][i]) {
			u = up[u][i], v = up[v][i];
			dt += (1<<(1+i));
		}
	}
	return dt + 2;
}

int find_(int a) {
	if (a == par[a]) return a;
	return par[a] = find_(par[a]);
}

void join_(int a, int b) {
	a = find_(a), b = find_(b);
	if (a == b) return;
	if (sz[a] < sz[b]) swap(a, b);
	sz[a] += sz[b];
	//remax(mx[a], h[mx[b]]);
	if (h[mx[a]] < h[mx[b]]) mx[a] = mx[b];
	remax(ans[a], ans[b]);
	par[b] = a;
}

int main() {
	fastio();
	iota(all(par), 0), iota(all(mx), 0);
	int n;
	cin >> n;
	vector<ii> q(n);
	for (int i = 0; i < n; ++i) {
		cin >> h[i];
		q[i] = mp(h[i], i);
	}
	sort(all(q));
	for (int i = 1; i < n; ++i) {
		int a, b;
		cin >> a >> b;
		--a;--b;
		g[a].eb(b);
		g[b].eb(a);
	}
	depth[0] = -1;
	dfs(0, 0);
	/*rep(i, n) {
		rep(j, n) {
			cout << "i: " << i << " j: " << j << " d[i]: " << depth[i] << " d[j]: " << depth[j] << endl;
			cout << "dist: " << dist(i, j) << endl;
		}
	}*/
	for (int i = 0; i < n; ++i) {
		const int cur = q[i].nd, curh = q[i].st;
		for (int u : g[cur]) {
			if (h[u] < curh) {
				u = find_(u);
				//remax(ans[u], ans[u]+dist(mx[u], cur));
				ans[u] += dist(mx[u], cur);
				join_(cur, u);
			}
		}
	}
	cout << ans[find_(0)] << '\n'; //+dist(n-1, q[n-1].nd) << '\n';


	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 17 ms 34136 KB Output is correct
2 Correct 17 ms 33880 KB Output is correct
3 Correct 18 ms 33884 KB Output is correct
4 Correct 17 ms 34140 KB Output is correct
5 Correct 17 ms 34028 KB Output is correct
6 Correct 17 ms 34036 KB Output is correct
7 Correct 17 ms 34140 KB Output is correct
8 Correct 17 ms 34140 KB Output is correct
9 Correct 18 ms 33884 KB Output is correct
10 Correct 17 ms 34140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 34136 KB Output is correct
2 Correct 17 ms 33880 KB Output is correct
3 Correct 18 ms 33884 KB Output is correct
4 Correct 17 ms 34140 KB Output is correct
5 Correct 17 ms 34028 KB Output is correct
6 Correct 17 ms 34036 KB Output is correct
7 Correct 17 ms 34140 KB Output is correct
8 Correct 17 ms 34140 KB Output is correct
9 Correct 18 ms 33884 KB Output is correct
10 Correct 17 ms 34140 KB Output is correct
11 Correct 17 ms 34140 KB Output is correct
12 Correct 18 ms 33960 KB Output is correct
13 Correct 17 ms 34140 KB Output is correct
14 Correct 18 ms 34100 KB Output is correct
15 Correct 17 ms 34140 KB Output is correct
16 Correct 18 ms 34140 KB Output is correct
17 Correct 18 ms 34392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 34136 KB Output is correct
2 Correct 17 ms 33880 KB Output is correct
3 Correct 18 ms 33884 KB Output is correct
4 Correct 17 ms 34140 KB Output is correct
5 Correct 17 ms 34028 KB Output is correct
6 Correct 17 ms 34036 KB Output is correct
7 Correct 17 ms 34140 KB Output is correct
8 Correct 17 ms 34140 KB Output is correct
9 Correct 18 ms 33884 KB Output is correct
10 Correct 17 ms 34140 KB Output is correct
11 Correct 17 ms 34140 KB Output is correct
12 Correct 18 ms 33960 KB Output is correct
13 Correct 17 ms 34140 KB Output is correct
14 Correct 18 ms 34100 KB Output is correct
15 Correct 17 ms 34140 KB Output is correct
16 Correct 18 ms 34140 KB Output is correct
17 Correct 18 ms 34392 KB Output is correct
18 Correct 20 ms 34396 KB Output is correct
19 Correct 20 ms 34444 KB Output is correct
20 Correct 20 ms 34396 KB Output is correct
21 Correct 20 ms 34476 KB Output is correct
22 Correct 20 ms 34396 KB Output is correct
23 Correct 20 ms 34396 KB Output is correct
24 Correct 20 ms 34396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 34136 KB Output is correct
2 Correct 17 ms 33880 KB Output is correct
3 Correct 18 ms 33884 KB Output is correct
4 Correct 17 ms 34140 KB Output is correct
5 Correct 17 ms 34028 KB Output is correct
6 Correct 17 ms 34036 KB Output is correct
7 Correct 17 ms 34140 KB Output is correct
8 Correct 17 ms 34140 KB Output is correct
9 Correct 18 ms 33884 KB Output is correct
10 Correct 17 ms 34140 KB Output is correct
11 Correct 17 ms 34140 KB Output is correct
12 Correct 18 ms 33960 KB Output is correct
13 Correct 17 ms 34140 KB Output is correct
14 Correct 18 ms 34100 KB Output is correct
15 Correct 17 ms 34140 KB Output is correct
16 Correct 18 ms 34140 KB Output is correct
17 Correct 18 ms 34392 KB Output is correct
18 Correct 20 ms 34396 KB Output is correct
19 Correct 20 ms 34444 KB Output is correct
20 Correct 20 ms 34396 KB Output is correct
21 Correct 20 ms 34476 KB Output is correct
22 Correct 20 ms 34396 KB Output is correct
23 Correct 20 ms 34396 KB Output is correct
24 Correct 20 ms 34396 KB Output is correct
25 Correct 18 ms 33884 KB Output is correct
26 Correct 19 ms 34348 KB Output is correct
27 Correct 21 ms 34392 KB Output is correct
28 Correct 20 ms 34392 KB Output is correct
29 Correct 20 ms 34392 KB Output is correct
30 Correct 21 ms 34140 KB Output is correct
31 Correct 20 ms 34140 KB Output is correct
32 Correct 20 ms 34140 KB Output is correct
33 Correct 19 ms 34136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 34136 KB Output is correct
2 Correct 17 ms 33880 KB Output is correct
3 Correct 18 ms 33884 KB Output is correct
4 Correct 17 ms 34140 KB Output is correct
5 Correct 17 ms 34028 KB Output is correct
6 Correct 17 ms 34036 KB Output is correct
7 Correct 17 ms 34140 KB Output is correct
8 Correct 17 ms 34140 KB Output is correct
9 Correct 18 ms 33884 KB Output is correct
10 Correct 17 ms 34140 KB Output is correct
11 Correct 17 ms 34140 KB Output is correct
12 Correct 18 ms 33960 KB Output is correct
13 Correct 17 ms 34140 KB Output is correct
14 Correct 18 ms 34100 KB Output is correct
15 Correct 17 ms 34140 KB Output is correct
16 Correct 18 ms 34140 KB Output is correct
17 Correct 18 ms 34392 KB Output is correct
18 Correct 20 ms 34396 KB Output is correct
19 Correct 20 ms 34444 KB Output is correct
20 Correct 20 ms 34396 KB Output is correct
21 Correct 20 ms 34476 KB Output is correct
22 Correct 20 ms 34396 KB Output is correct
23 Correct 20 ms 34396 KB Output is correct
24 Correct 20 ms 34396 KB Output is correct
25 Correct 144 ms 51284 KB Output is correct
26 Correct 137 ms 54412 KB Output is correct
27 Correct 138 ms 54232 KB Output is correct
28 Correct 204 ms 54632 KB Output is correct
29 Correct 202 ms 54416 KB Output is correct
30 Correct 196 ms 54344 KB Output is correct
31 Correct 198 ms 54248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 34140 KB Output is correct
2 Correct 17 ms 34140 KB Output is correct
3 Correct 198 ms 41944 KB Output is correct
4 Correct 198 ms 41956 KB Output is correct
5 Correct 196 ms 41808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 34136 KB Output is correct
2 Correct 17 ms 33880 KB Output is correct
3 Correct 18 ms 33884 KB Output is correct
4 Correct 17 ms 34140 KB Output is correct
5 Correct 17 ms 34028 KB Output is correct
6 Correct 17 ms 34036 KB Output is correct
7 Correct 17 ms 34140 KB Output is correct
8 Correct 17 ms 34140 KB Output is correct
9 Correct 18 ms 33884 KB Output is correct
10 Correct 17 ms 34140 KB Output is correct
11 Correct 17 ms 34140 KB Output is correct
12 Correct 18 ms 33960 KB Output is correct
13 Correct 17 ms 34140 KB Output is correct
14 Correct 18 ms 34100 KB Output is correct
15 Correct 17 ms 34140 KB Output is correct
16 Correct 18 ms 34140 KB Output is correct
17 Correct 18 ms 34392 KB Output is correct
18 Correct 20 ms 34396 KB Output is correct
19 Correct 20 ms 34444 KB Output is correct
20 Correct 20 ms 34396 KB Output is correct
21 Correct 20 ms 34476 KB Output is correct
22 Correct 20 ms 34396 KB Output is correct
23 Correct 20 ms 34396 KB Output is correct
24 Correct 20 ms 34396 KB Output is correct
25 Correct 18 ms 33884 KB Output is correct
26 Correct 19 ms 34348 KB Output is correct
27 Correct 21 ms 34392 KB Output is correct
28 Correct 20 ms 34392 KB Output is correct
29 Correct 20 ms 34392 KB Output is correct
30 Correct 21 ms 34140 KB Output is correct
31 Correct 20 ms 34140 KB Output is correct
32 Correct 20 ms 34140 KB Output is correct
33 Correct 19 ms 34136 KB Output is correct
34 Correct 144 ms 51284 KB Output is correct
35 Correct 137 ms 54412 KB Output is correct
36 Correct 138 ms 54232 KB Output is correct
37 Correct 204 ms 54632 KB Output is correct
38 Correct 202 ms 54416 KB Output is correct
39 Correct 196 ms 54344 KB Output is correct
40 Correct 198 ms 54248 KB Output is correct
41 Correct 17 ms 34140 KB Output is correct
42 Correct 17 ms 34140 KB Output is correct
43 Correct 198 ms 41944 KB Output is correct
44 Correct 198 ms 41956 KB Output is correct
45 Correct 196 ms 41808 KB Output is correct
46 Correct 317 ms 50144 KB Output is correct
47 Correct 323 ms 49592 KB Output is correct
48 Correct 294 ms 50516 KB Output is correct
49 Correct 332 ms 49460 KB Output is correct
50 Correct 257 ms 45124 KB Output is correct
51 Correct 253 ms 45008 KB Output is correct
52 Correct 271 ms 45268 KB Output is correct
53 Correct 267 ms 44884 KB Output is correct