Submission #372973

# Submission time Handle Problem Language Result Execution time Memory
372973 2021-03-02T19:33:30 Z atoiz Construction of Highway (JOI18_construction) C++14
100 / 100
275 ms 5608 KB
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>

using namespace std;

const int MAXN = 100007;
int N, par[MAXN], ch[MAXN][2], dep[MAXN], val[MAXN], ft[MAXN];

bool isRoot(int u) { return u != ch[par[u]][0] && u != ch[par[u]][1]; }
bool dir(int u) { return u == ch[par[u]][1]; }
void rot(int u) {
	int p = par[u], g = par[p], k = dir(u);
	if (!isRoot(p)) ch[g][dir(p)] = u;
	par[u] = g;
	par[ch[p][k] = ch[u][!k]] = p;
	par[ch[u][!k] = p] = u;
}
void splay(int u) {
	for (; !isRoot(u); rot(u)) {
		if (!isRoot(par[u])) rot(dir(u) == dir(par[u]) ? par[u] : u);
	}
}
int64_t access(int u) {
	vector<tuple<int, int, int>> segs;
	for (int v = 0; u; v = u, u = par[v]) {
		splay(u);
		
		int x = ch[u][1];
		ch[u][1] = v;

		while (ch[u][0]) u = ch[u][0];
		splay(u);

		if (x) {
			while (ch[x][0]) x = ch[x][0];
			splay(x);
			val[x] = val[u];
		}

		if (v) { // ignore first segment ({B_j})
			segs.emplace_back(val[u], dep[u] + 1, dep[v] - dep[u]);
		}
	}

	int64_t cost = 0;
	sort(segs.begin(), segs.end());
	reverse(segs.begin(), segs.end());
	for (auto tmp : segs) {
		int p, x;
		tie(ignore, p, x) = tmp;
		for (int i = p; i > 0; i -= i & (-i)) cost += (int64_t) ft[i] * x;
		for (int i = p; i <= N; i += i & (-i)) ft[i] += x;
	}

	for (auto tmp : segs) {
		int p, x;
		tie(ignore, p, x) = tmp;
		for (int i = p; i <= N; i += i & (-i)) ft[i] -= x;
	}

	return cost;
}

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin >> N;
	for (int u = 1; u <= N; ++u) {
		cin >> val[u];
	}
	for (int i = 0; i < N - 1; ++i) {
		int u, v;
		cin >> u >> v;
		dep[v] = dep[u] + 1, par[v] = u;
		cout << access(v) << '\n';
		val[1] = val[v];
	}
}


# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 492 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 1 ms 384 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 1 ms 364 KB Output is correct
41 Correct 1 ms 364 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 492 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 1 ms 384 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 1 ms 364 KB Output is correct
41 Correct 1 ms 364 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 2 ms 364 KB Output is correct
46 Correct 5 ms 492 KB Output is correct
47 Correct 5 ms 492 KB Output is correct
48 Correct 5 ms 492 KB Output is correct
49 Correct 3 ms 492 KB Output is correct
50 Correct 3 ms 492 KB Output is correct
51 Correct 4 ms 492 KB Output is correct
52 Correct 3 ms 620 KB Output is correct
53 Correct 3 ms 620 KB Output is correct
54 Correct 3 ms 620 KB Output is correct
55 Correct 3 ms 620 KB Output is correct
56 Correct 3 ms 492 KB Output is correct
57 Correct 5 ms 492 KB Output is correct
58 Correct 8 ms 492 KB Output is correct
59 Correct 6 ms 492 KB Output is correct
60 Correct 6 ms 492 KB Output is correct
61 Correct 5 ms 492 KB Output is correct
62 Correct 5 ms 492 KB Output is correct
63 Correct 4 ms 492 KB Output is correct
64 Correct 4 ms 492 KB Output is correct
65 Correct 4 ms 492 KB Output is correct
66 Correct 5 ms 540 KB Output is correct
67 Correct 4 ms 492 KB Output is correct
68 Correct 3 ms 492 KB Output is correct
69 Correct 3 ms 620 KB Output is correct
70 Correct 3 ms 620 KB Output is correct
71 Correct 3 ms 492 KB Output is correct
72 Correct 5 ms 492 KB Output is correct
73 Correct 7 ms 492 KB Output is correct
74 Correct 4 ms 492 KB Output is correct
75 Correct 5 ms 492 KB Output is correct
76 Correct 5 ms 492 KB Output is correct
77 Correct 3 ms 492 KB Output is correct
78 Correct 3 ms 492 KB Output is correct
79 Correct 3 ms 620 KB Output is correct
80 Correct 3 ms 492 KB Output is correct
81 Correct 5 ms 492 KB Output is correct
82 Correct 4 ms 492 KB Output is correct
83 Correct 4 ms 492 KB Output is correct
84 Correct 4 ms 492 KB Output is correct
85 Correct 4 ms 492 KB Output is correct
86 Correct 4 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 492 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 1 ms 384 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 1 ms 364 KB Output is correct
41 Correct 1 ms 364 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 2 ms 364 KB Output is correct
46 Correct 5 ms 492 KB Output is correct
47 Correct 5 ms 492 KB Output is correct
48 Correct 5 ms 492 KB Output is correct
49 Correct 3 ms 492 KB Output is correct
50 Correct 3 ms 492 KB Output is correct
51 Correct 4 ms 492 KB Output is correct
52 Correct 3 ms 620 KB Output is correct
53 Correct 3 ms 620 KB Output is correct
54 Correct 3 ms 620 KB Output is correct
55 Correct 3 ms 620 KB Output is correct
56 Correct 3 ms 492 KB Output is correct
57 Correct 5 ms 492 KB Output is correct
58 Correct 8 ms 492 KB Output is correct
59 Correct 6 ms 492 KB Output is correct
60 Correct 6 ms 492 KB Output is correct
61 Correct 5 ms 492 KB Output is correct
62 Correct 5 ms 492 KB Output is correct
63 Correct 4 ms 492 KB Output is correct
64 Correct 4 ms 492 KB Output is correct
65 Correct 4 ms 492 KB Output is correct
66 Correct 5 ms 540 KB Output is correct
67 Correct 4 ms 492 KB Output is correct
68 Correct 3 ms 492 KB Output is correct
69 Correct 3 ms 620 KB Output is correct
70 Correct 3 ms 620 KB Output is correct
71 Correct 3 ms 492 KB Output is correct
72 Correct 5 ms 492 KB Output is correct
73 Correct 7 ms 492 KB Output is correct
74 Correct 4 ms 492 KB Output is correct
75 Correct 5 ms 492 KB Output is correct
76 Correct 5 ms 492 KB Output is correct
77 Correct 3 ms 492 KB Output is correct
78 Correct 3 ms 492 KB Output is correct
79 Correct 3 ms 620 KB Output is correct
80 Correct 3 ms 492 KB Output is correct
81 Correct 5 ms 492 KB Output is correct
82 Correct 4 ms 492 KB Output is correct
83 Correct 4 ms 492 KB Output is correct
84 Correct 4 ms 492 KB Output is correct
85 Correct 4 ms 492 KB Output is correct
86 Correct 4 ms 492 KB Output is correct
87 Correct 12 ms 748 KB Output is correct
88 Correct 38 ms 1644 KB Output is correct
89 Correct 156 ms 4736 KB Output is correct
90 Correct 159 ms 4716 KB Output is correct
91 Correct 168 ms 4716 KB Output is correct
92 Correct 81 ms 4716 KB Output is correct
93 Correct 65 ms 4716 KB Output is correct
94 Correct 66 ms 4716 KB Output is correct
95 Correct 72 ms 5224 KB Output is correct
96 Correct 96 ms 5608 KB Output is correct
97 Correct 81 ms 5608 KB Output is correct
98 Correct 84 ms 5608 KB Output is correct
99 Correct 86 ms 4972 KB Output is correct
100 Correct 209 ms 4460 KB Output is correct
101 Correct 275 ms 4716 KB Output is correct
102 Correct 242 ms 4868 KB Output is correct
103 Correct 242 ms 4844 KB Output is correct
104 Correct 143 ms 5100 KB Output is correct
105 Correct 157 ms 5104 KB Output is correct
106 Correct 147 ms 5228 KB Output is correct
107 Correct 166 ms 3948 KB Output is correct
108 Correct 171 ms 4204 KB Output is correct
109 Correct 168 ms 4332 KB Output is correct
110 Correct 65 ms 4076 KB Output is correct
111 Correct 68 ms 5224 KB Output is correct
112 Correct 83 ms 5096 KB Output is correct
113 Correct 92 ms 4356 KB Output is correct
114 Correct 203 ms 4460 KB Output is correct
115 Correct 231 ms 4240 KB Output is correct
116 Correct 143 ms 4332 KB Output is correct
117 Correct 87 ms 4588 KB Output is correct
118 Correct 89 ms 4588 KB Output is correct
119 Correct 91 ms 4588 KB Output is correct
120 Correct 76 ms 4204 KB Output is correct
121 Correct 104 ms 4204 KB Output is correct
122 Correct 78 ms 4204 KB Output is correct
123 Correct 141 ms 4588 KB Output is correct
124 Correct 149 ms 4716 KB Output is correct
125 Correct 142 ms 4588 KB Output is correct
126 Correct 143 ms 4332 KB Output is correct
127 Correct 137 ms 4204 KB Output is correct
128 Correct 142 ms 4204 KB Output is correct