답안 #709724

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
709724 2023-03-14T09:08:58 Z Radin_Zahedi2 Construction of Highway (JOI18_construction) C++17
100 / 100
1836 ms 22448 KB
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
using ll = long long;
using ld = long double;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)x.size()
#define endl '\n'
const int mod = 1e9 + 7;
const int inf = 2e9 + 5;
const ll linf = 9e18 + 5;


int n;
const int N = 1e5 + 5;
vector<int> adj[N];
int c[N];

int a[N];
int b[N];

int len[N];
int up[N];
int par[N];

int dtime = 0;
int st[N];
set<vector<int>> colors;


void dfssort(int u) {
	len[u] = 1;
	for (auto v : adj[u]) {
		dfssort(v);
		len[u] += len[v];
	}

	for (int i = 0; i < sz(adj[u]); i++) {
		if (len[adj[u][0]] < len[adj[u][i]]) {
			swap(adj[u][0], adj[u][i]);
		}
	}
}

void dfsuppar(int u) {
	if (adj[u].empty()) {
		return;
	}

	for (int i = 1; i < sz(adj[u]); i++) {
		int v = adj[u][i];

		up[v] = v;
		par[v] = u;
		dfsuppar(v);
	}
	
	up[adj[u][0]] = up[u];
	par[adj[u][0]] = u;
	dfsuppar(adj[u][0]);
}

void dfsst(int u) {
	st[u] = dtime;
	dtime++;

	for (auto v : adj[u]) {
		dfsst(v);
	}
}

void init() {
}

void input() {
	cin >> n;

	for (int i = 1; i <= n; i++) {
		cin >> c[i];
	}

	for (int i = 1; i < n; i++) {
		cin >> a[i] >> b[i];
	}


	for (int i = 1; i < n; i++) {
		adj[a[i]].pb(b[i]);
	}
}

vector<int> cover(int x) {
	return *prev(colors.upper_bound({x, inf, inf}));
}

void cut(int x) {
	vector<int> v = cover(x);
	colors.erase(v);
	
	colors.insert({v[0], x, v[2]});

	if (v[1] >= x + 1) {
		colors.insert({x + 1, v[1], v[2]});
	}
}

ll mrg(vector<pair<int, int>> &v, int l, int r, int m) {
	ll ans = 0;
	int sum = 0;

	int i = l;
	int j = m + 1;

	int k = 0;
	pair<int, int> v2[r - l + 1];

	while (i <= m && j <= r) {
		if (v[i].fi > v[j].fi) {
			v2[k] = v[j];
			sum += v[j].se;

			j++;
			k++;
		}
		else {
			v2[k] = v[i];
			ans += 1ll * sum * v[i].se;

			i++;
			k++;
		}
	}
	
	while (i <= m) {
		v2[k] = v[i];
		ans += 1ll * sum * v[i].se;

		i++;
		k++;
	}

	while (j <= r) {
		v2[k] = v[j];
		sum += v[j].se;

		j++;
		k++;
	}


	for (int i = 0; i < (r - l + 1); i++) {
		v[l + i] = v2[i];
	}

	return ans;
}

ll calcinvs(vector<pair<int, int>> &v, int l, int r) {
	if (l == r) {
		return 0;
	}

	int m = (l + r) / 2;

	ll ans = 0;
	ans += calcinvs(v, l, m);
	ans += calcinvs(v, m + 1, r);
	ans += mrg(v, l, r, m);

	return ans;
}

ll calc(int vert) {
	vector<pair<int, int>> cols;

	int u = vert;
	while (u) {
		int v = up[u];

		cut(st[v] - 1);
		cut(st[u]);


		int now = st[u];
		while (st[v] <= now) {
			vector<int> vect = cover(now);
			cols.pb(mp(vect[2], vect[1] - vect[0] + 1));
			colors.erase(vect);

			now = vect[0] - 1;
		}

		colors.insert({st[v], st[u], c[vert]});

		u = par[up[u]];
	}


	reverse(cols.begin(), cols.end());

	return calcinvs(cols, 0, sz(cols) - 1);
}

void solve() {
	dfssort(1);
	
	up[1] = 1;
	dfsuppar(1);

	dfsst(1);

	colors.insert({-1, n, inf});
	calc(1);
	for (int i = 1; i < n; i++) {
		cout << calc(b[i]) << endl;
	}
}

void output() {
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int number_of_testcases = 1;
	//cin >> number_of_testcases;
	while (number_of_testcases--) {
		init();

		input();

		solve();

		output();
	}

	return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2672 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 3 ms 2644 KB Output is correct
4 Correct 3 ms 2772 KB Output is correct
5 Correct 4 ms 2644 KB Output is correct
6 Correct 4 ms 2684 KB Output is correct
7 Correct 4 ms 2644 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 3 ms 2772 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 3 ms 2772 KB Output is correct
14 Correct 4 ms 2684 KB Output is correct
15 Correct 5 ms 2676 KB Output is correct
16 Correct 7 ms 2692 KB Output is correct
17 Correct 7 ms 2684 KB Output is correct
18 Correct 5 ms 2684 KB Output is correct
19 Correct 3 ms 2644 KB Output is correct
20 Correct 3 ms 2644 KB Output is correct
21 Correct 3 ms 2684 KB Output is correct
22 Correct 4 ms 2644 KB Output is correct
23 Correct 4 ms 2644 KB Output is correct
24 Correct 4 ms 2644 KB Output is correct
25 Correct 4 ms 2760 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 3 ms 2772 KB Output is correct
28 Correct 3 ms 2772 KB Output is correct
29 Correct 3 ms 2680 KB Output is correct
30 Correct 5 ms 2772 KB Output is correct
31 Correct 6 ms 2644 KB Output is correct
32 Correct 3 ms 2676 KB Output is correct
33 Correct 3 ms 2672 KB Output is correct
34 Correct 3 ms 2644 KB Output is correct
35 Correct 3 ms 2644 KB Output is correct
36 Correct 3 ms 2644 KB Output is correct
37 Correct 3 ms 2644 KB Output is correct
38 Correct 3 ms 2676 KB Output is correct
39 Correct 3 ms 2644 KB Output is correct
40 Correct 3 ms 2644 KB Output is correct
41 Correct 3 ms 2644 KB Output is correct
42 Correct 3 ms 2648 KB Output is correct
43 Correct 3 ms 2644 KB Output is correct
44 Correct 3 ms 2680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2672 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 3 ms 2644 KB Output is correct
4 Correct 3 ms 2772 KB Output is correct
5 Correct 4 ms 2644 KB Output is correct
6 Correct 4 ms 2684 KB Output is correct
7 Correct 4 ms 2644 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 3 ms 2772 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 3 ms 2772 KB Output is correct
14 Correct 4 ms 2684 KB Output is correct
15 Correct 5 ms 2676 KB Output is correct
16 Correct 7 ms 2692 KB Output is correct
17 Correct 7 ms 2684 KB Output is correct
18 Correct 5 ms 2684 KB Output is correct
19 Correct 3 ms 2644 KB Output is correct
20 Correct 3 ms 2644 KB Output is correct
21 Correct 3 ms 2684 KB Output is correct
22 Correct 4 ms 2644 KB Output is correct
23 Correct 4 ms 2644 KB Output is correct
24 Correct 4 ms 2644 KB Output is correct
25 Correct 4 ms 2760 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 3 ms 2772 KB Output is correct
28 Correct 3 ms 2772 KB Output is correct
29 Correct 3 ms 2680 KB Output is correct
30 Correct 5 ms 2772 KB Output is correct
31 Correct 6 ms 2644 KB Output is correct
32 Correct 3 ms 2676 KB Output is correct
33 Correct 3 ms 2672 KB Output is correct
34 Correct 3 ms 2644 KB Output is correct
35 Correct 3 ms 2644 KB Output is correct
36 Correct 3 ms 2644 KB Output is correct
37 Correct 3 ms 2644 KB Output is correct
38 Correct 3 ms 2676 KB Output is correct
39 Correct 3 ms 2644 KB Output is correct
40 Correct 3 ms 2644 KB Output is correct
41 Correct 3 ms 2644 KB Output is correct
42 Correct 3 ms 2648 KB Output is correct
43 Correct 3 ms 2644 KB Output is correct
44 Correct 3 ms 2680 KB Output is correct
45 Correct 7 ms 2816 KB Output is correct
46 Correct 29 ms 3168 KB Output is correct
47 Correct 26 ms 3156 KB Output is correct
48 Correct 26 ms 3124 KB Output is correct
49 Correct 7 ms 3156 KB Output is correct
50 Correct 6 ms 3196 KB Output is correct
51 Correct 6 ms 3140 KB Output is correct
52 Correct 14 ms 3416 KB Output is correct
53 Correct 11 ms 3456 KB Output is correct
54 Correct 10 ms 3472 KB Output is correct
55 Correct 13 ms 3412 KB Output is correct
56 Correct 10 ms 3156 KB Output is correct
57 Correct 41 ms 3196 KB Output is correct
58 Correct 41 ms 3232 KB Output is correct
59 Correct 39 ms 3228 KB Output is correct
60 Correct 43 ms 3180 KB Output is correct
61 Correct 11 ms 3156 KB Output is correct
62 Correct 11 ms 3260 KB Output is correct
63 Correct 11 ms 3272 KB Output is correct
64 Correct 27 ms 3164 KB Output is correct
65 Correct 25 ms 3164 KB Output is correct
66 Correct 24 ms 3164 KB Output is correct
67 Correct 25 ms 3156 KB Output is correct
68 Correct 6 ms 3156 KB Output is correct
69 Correct 11 ms 3464 KB Output is correct
70 Correct 10 ms 3416 KB Output is correct
71 Correct 11 ms 3200 KB Output is correct
72 Correct 42 ms 3208 KB Output is correct
73 Correct 40 ms 3188 KB Output is correct
74 Correct 10 ms 3156 KB Output is correct
75 Correct 11 ms 3180 KB Output is correct
76 Correct 12 ms 3128 KB Output is correct
77 Correct 13 ms 3084 KB Output is correct
78 Correct 12 ms 3072 KB Output is correct
79 Correct 13 ms 3028 KB Output is correct
80 Correct 13 ms 3028 KB Output is correct
81 Correct 13 ms 3184 KB Output is correct
82 Correct 14 ms 3104 KB Output is correct
83 Correct 13 ms 3156 KB Output is correct
84 Correct 14 ms 3060 KB Output is correct
85 Correct 14 ms 3072 KB Output is correct
86 Correct 13 ms 3072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2672 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 3 ms 2644 KB Output is correct
4 Correct 3 ms 2772 KB Output is correct
5 Correct 4 ms 2644 KB Output is correct
6 Correct 4 ms 2684 KB Output is correct
7 Correct 4 ms 2644 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 3 ms 2772 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 3 ms 2772 KB Output is correct
14 Correct 4 ms 2684 KB Output is correct
15 Correct 5 ms 2676 KB Output is correct
16 Correct 7 ms 2692 KB Output is correct
17 Correct 7 ms 2684 KB Output is correct
18 Correct 5 ms 2684 KB Output is correct
19 Correct 3 ms 2644 KB Output is correct
20 Correct 3 ms 2644 KB Output is correct
21 Correct 3 ms 2684 KB Output is correct
22 Correct 4 ms 2644 KB Output is correct
23 Correct 4 ms 2644 KB Output is correct
24 Correct 4 ms 2644 KB Output is correct
25 Correct 4 ms 2760 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 3 ms 2772 KB Output is correct
28 Correct 3 ms 2772 KB Output is correct
29 Correct 3 ms 2680 KB Output is correct
30 Correct 5 ms 2772 KB Output is correct
31 Correct 6 ms 2644 KB Output is correct
32 Correct 3 ms 2676 KB Output is correct
33 Correct 3 ms 2672 KB Output is correct
34 Correct 3 ms 2644 KB Output is correct
35 Correct 3 ms 2644 KB Output is correct
36 Correct 3 ms 2644 KB Output is correct
37 Correct 3 ms 2644 KB Output is correct
38 Correct 3 ms 2676 KB Output is correct
39 Correct 3 ms 2644 KB Output is correct
40 Correct 3 ms 2644 KB Output is correct
41 Correct 3 ms 2644 KB Output is correct
42 Correct 3 ms 2648 KB Output is correct
43 Correct 3 ms 2644 KB Output is correct
44 Correct 3 ms 2680 KB Output is correct
45 Correct 7 ms 2816 KB Output is correct
46 Correct 29 ms 3168 KB Output is correct
47 Correct 26 ms 3156 KB Output is correct
48 Correct 26 ms 3124 KB Output is correct
49 Correct 7 ms 3156 KB Output is correct
50 Correct 6 ms 3196 KB Output is correct
51 Correct 6 ms 3140 KB Output is correct
52 Correct 14 ms 3416 KB Output is correct
53 Correct 11 ms 3456 KB Output is correct
54 Correct 10 ms 3472 KB Output is correct
55 Correct 13 ms 3412 KB Output is correct
56 Correct 10 ms 3156 KB Output is correct
57 Correct 41 ms 3196 KB Output is correct
58 Correct 41 ms 3232 KB Output is correct
59 Correct 39 ms 3228 KB Output is correct
60 Correct 43 ms 3180 KB Output is correct
61 Correct 11 ms 3156 KB Output is correct
62 Correct 11 ms 3260 KB Output is correct
63 Correct 11 ms 3272 KB Output is correct
64 Correct 27 ms 3164 KB Output is correct
65 Correct 25 ms 3164 KB Output is correct
66 Correct 24 ms 3164 KB Output is correct
67 Correct 25 ms 3156 KB Output is correct
68 Correct 6 ms 3156 KB Output is correct
69 Correct 11 ms 3464 KB Output is correct
70 Correct 10 ms 3416 KB Output is correct
71 Correct 11 ms 3200 KB Output is correct
72 Correct 42 ms 3208 KB Output is correct
73 Correct 40 ms 3188 KB Output is correct
74 Correct 10 ms 3156 KB Output is correct
75 Correct 11 ms 3180 KB Output is correct
76 Correct 12 ms 3128 KB Output is correct
77 Correct 13 ms 3084 KB Output is correct
78 Correct 12 ms 3072 KB Output is correct
79 Correct 13 ms 3028 KB Output is correct
80 Correct 13 ms 3028 KB Output is correct
81 Correct 13 ms 3184 KB Output is correct
82 Correct 14 ms 3104 KB Output is correct
83 Correct 13 ms 3156 KB Output is correct
84 Correct 14 ms 3060 KB Output is correct
85 Correct 14 ms 3072 KB Output is correct
86 Correct 13 ms 3072 KB Output is correct
87 Correct 72 ms 3916 KB Output is correct
88 Correct 255 ms 6508 KB Output is correct
89 Correct 1115 ms 14380 KB Output is correct
90 Correct 1008 ms 14176 KB Output is correct
91 Correct 1063 ms 14296 KB Output is correct
92 Correct 118 ms 15884 KB Output is correct
93 Correct 140 ms 15832 KB Output is correct
94 Correct 169 ms 15840 KB Output is correct
95 Correct 322 ms 21092 KB Output is correct
96 Correct 357 ms 21104 KB Output is correct
97 Correct 329 ms 22380 KB Output is correct
98 Correct 314 ms 22448 KB Output is correct
99 Correct 305 ms 17252 KB Output is correct
100 Correct 1836 ms 18696 KB Output is correct
101 Correct 1831 ms 19032 KB Output is correct
102 Correct 1740 ms 19068 KB Output is correct
103 Correct 1718 ms 18996 KB Output is correct
104 Correct 275 ms 17236 KB Output is correct
105 Correct 281 ms 17236 KB Output is correct
106 Correct 282 ms 17252 KB Output is correct
107 Correct 1036 ms 14672 KB Output is correct
108 Correct 1064 ms 15084 KB Output is correct
109 Correct 1059 ms 15260 KB Output is correct
110 Correct 117 ms 16480 KB Output is correct
111 Correct 327 ms 22064 KB Output is correct
112 Correct 310 ms 21860 KB Output is correct
113 Correct 292 ms 16552 KB Output is correct
114 Correct 1759 ms 18708 KB Output is correct
115 Correct 1762 ms 18388 KB Output is correct
116 Correct 274 ms 16592 KB Output is correct
117 Correct 327 ms 15304 KB Output is correct
118 Correct 334 ms 14712 KB Output is correct
119 Correct 339 ms 14580 KB Output is correct
120 Correct 329 ms 14928 KB Output is correct
121 Correct 318 ms 14656 KB Output is correct
122 Correct 334 ms 14128 KB Output is correct
123 Correct 349 ms 15332 KB Output is correct
124 Correct 379 ms 14932 KB Output is correct
125 Correct 355 ms 14612 KB Output is correct
126 Correct 318 ms 15064 KB Output is correct
127 Correct 340 ms 14540 KB Output is correct
128 Correct 339 ms 14288 KB Output is correct