Submission #873848

# Submission time Handle Problem Language Result Execution time Memory
873848 2023-11-16T00:54:36 Z NK_ Construction of Highway (JOI18_construction) C++17
100 / 100
184 ms 18912 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define pb push_back

#define f first
#define s second
#define mp make_pair

#define sz(x) int(x.size())

using pi = pair<int, int>;
using ll = long long;

template<class T> using V = vector<T>;

using vi = V<int>;
using vpi = V<pi>;

const int LG = 18;

struct BIT {
	vi data; int N; void init(int n) { N = n; data = vi(N, 0); }
	void add(int p, int x) { for(++p; p <= N; p += p & -p) data[p - 1] += x; }
	int sum(int l, int r) { return sum(r + 1) - sum(l); }
	int sum(int r) { int s = 0; for(; r; r -= r & -r) s += data[r - 1]; return s; }
};

int main() {
	cin.tie(0)->sync_with_stdio(0);

	int N; cin >> N;
	vi A(N); for(auto& x : A) cin >> x;

	{
		vi C = A; sort(begin(C), end(C));
		for(auto& x : A) x = lower_bound(begin(C), end(C), x) - begin(C);
	}

	V<vi> adj(N);
	vi Q(N-1);
	for(int i = 0; i < N-1; i++) {
		int u, v; cin >> u >> v; --u, --v;
		adj[u].pb(v);
		Q[i] = v;
	}



	vi nxt(N), par(N), dep(N, 0), siz(N); V<vpi> st(N); 

	function<void(int)> gen = [&](int u) {
		for(auto& v : adj[u]) {
			dep[v] = dep[u] + 1;
			par[v] = u;
			gen(v);
			siz[u] += siz[v];
		}
	};


	function<void(int, int)> dfs = [&](int u, int h) {
		nxt[u] = h;

		int hvy = -1;
		for(auto& v : adj[u]) {
			if (hvy == -1 || siz[hvy] < siz[v]) hvy = v;
		}

		if (hvy == -1) return;

		dfs(hvy, h);
		for(auto& v : adj[u]) if (v != hvy) dfs(v, v);
	};

	gen(0);
	par[0] = -1; dfs(0, 0);

	BIT B; B.init(N);

	auto answer = [&](vpi &X) {
		ll ans = 0;

		for(auto& x : X) {
			// cout << x.f << " => " << x.s << endl;
			ans += x.s * 1LL * B.sum(x.f);
			B.add(x.f, x.s);
		}

		for(auto& x : X) B.add(x.f, -x.s);

		cout << ans << nl;
	};

	for(auto u : Q) {
		vpi X; int c = A[u];
 	
		while(u != -1) {
			// cout << "U: " << u << endl;
			int d = dep[u]; vpi R;

			u = nxt[u]; int t = dep[u] - 1; // cout << "C: " << u << endl;
			while(sz(st[u]) && st[u].back().s <= d) {
				pi bk = st[u].back(); // cout << bk.f << " -> " << bk.s << endl;
				R.pb(mp(bk.f, bk.s - t)); t = bk.s;
				st[u].pop_back();
			}

			if (sz(st[u])) R.pb(mp(st[u].back().f, d - t));

			st[u].pb(mp(c, d));

			reverse(begin(R), end(R));
			X.insert(end(X), begin(R), end(R));

			u = par[u];
		}


		answer(X);
		// cout << endl << endl << endl;
	}




	exit(0-0);
}	
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 508 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 600 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 344 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 508 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 600 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 344 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 4 ms 860 KB Output is correct
47 Correct 4 ms 856 KB Output is correct
48 Correct 4 ms 860 KB Output is correct
49 Correct 2 ms 1116 KB Output is correct
50 Correct 2 ms 1100 KB Output is correct
51 Correct 2 ms 1116 KB Output is correct
52 Correct 2 ms 860 KB Output is correct
53 Correct 2 ms 860 KB Output is correct
54 Correct 3 ms 860 KB Output is correct
55 Correct 2 ms 860 KB Output is correct
56 Correct 2 ms 860 KB Output is correct
57 Correct 4 ms 860 KB Output is correct
58 Correct 5 ms 860 KB Output is correct
59 Correct 5 ms 892 KB Output is correct
60 Correct 6 ms 1116 KB Output is correct
61 Correct 2 ms 860 KB Output is correct
62 Correct 3 ms 856 KB Output is correct
63 Correct 3 ms 860 KB Output is correct
64 Correct 4 ms 860 KB Output is correct
65 Correct 4 ms 860 KB Output is correct
66 Correct 4 ms 860 KB Output is correct
67 Correct 4 ms 860 KB Output is correct
68 Correct 2 ms 1116 KB Output is correct
69 Correct 2 ms 972 KB Output is correct
70 Correct 2 ms 860 KB Output is correct
71 Correct 2 ms 860 KB Output is correct
72 Correct 4 ms 860 KB Output is correct
73 Correct 5 ms 860 KB Output is correct
74 Correct 2 ms 860 KB Output is correct
75 Correct 3 ms 860 KB Output is correct
76 Correct 3 ms 860 KB Output is correct
77 Correct 3 ms 860 KB Output is correct
78 Correct 2 ms 860 KB Output is correct
79 Correct 3 ms 900 KB Output is correct
80 Correct 2 ms 860 KB Output is correct
81 Correct 3 ms 860 KB Output is correct
82 Correct 3 ms 860 KB Output is correct
83 Correct 3 ms 676 KB Output is correct
84 Correct 3 ms 860 KB Output is correct
85 Correct 3 ms 856 KB Output is correct
86 Correct 3 ms 748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 508 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 600 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 344 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 4 ms 860 KB Output is correct
47 Correct 4 ms 856 KB Output is correct
48 Correct 4 ms 860 KB Output is correct
49 Correct 2 ms 1116 KB Output is correct
50 Correct 2 ms 1100 KB Output is correct
51 Correct 2 ms 1116 KB Output is correct
52 Correct 2 ms 860 KB Output is correct
53 Correct 2 ms 860 KB Output is correct
54 Correct 3 ms 860 KB Output is correct
55 Correct 2 ms 860 KB Output is correct
56 Correct 2 ms 860 KB Output is correct
57 Correct 4 ms 860 KB Output is correct
58 Correct 5 ms 860 KB Output is correct
59 Correct 5 ms 892 KB Output is correct
60 Correct 6 ms 1116 KB Output is correct
61 Correct 2 ms 860 KB Output is correct
62 Correct 3 ms 856 KB Output is correct
63 Correct 3 ms 860 KB Output is correct
64 Correct 4 ms 860 KB Output is correct
65 Correct 4 ms 860 KB Output is correct
66 Correct 4 ms 860 KB Output is correct
67 Correct 4 ms 860 KB Output is correct
68 Correct 2 ms 1116 KB Output is correct
69 Correct 2 ms 972 KB Output is correct
70 Correct 2 ms 860 KB Output is correct
71 Correct 2 ms 860 KB Output is correct
72 Correct 4 ms 860 KB Output is correct
73 Correct 5 ms 860 KB Output is correct
74 Correct 2 ms 860 KB Output is correct
75 Correct 3 ms 860 KB Output is correct
76 Correct 3 ms 860 KB Output is correct
77 Correct 3 ms 860 KB Output is correct
78 Correct 2 ms 860 KB Output is correct
79 Correct 3 ms 900 KB Output is correct
80 Correct 2 ms 860 KB Output is correct
81 Correct 3 ms 860 KB Output is correct
82 Correct 3 ms 860 KB Output is correct
83 Correct 3 ms 676 KB Output is correct
84 Correct 3 ms 860 KB Output is correct
85 Correct 3 ms 856 KB Output is correct
86 Correct 3 ms 748 KB Output is correct
87 Correct 12 ms 1372 KB Output is correct
88 Correct 39 ms 3524 KB Output is correct
89 Correct 161 ms 11216 KB Output is correct
90 Correct 164 ms 11396 KB Output is correct
91 Correct 156 ms 11348 KB Output is correct
92 Correct 69 ms 18912 KB Output is correct
93 Correct 66 ms 18892 KB Output is correct
94 Correct 65 ms 18888 KB Output is correct
95 Correct 67 ms 16328 KB Output is correct
96 Correct 70 ms 16264 KB Output is correct
97 Correct 66 ms 16332 KB Output is correct
98 Correct 70 ms 16612 KB Output is correct
99 Correct 71 ms 15196 KB Output is correct
100 Correct 165 ms 11464 KB Output is correct
101 Correct 181 ms 11412 KB Output is correct
102 Correct 184 ms 11348 KB Output is correct
103 Correct 184 ms 11464 KB Output is correct
104 Correct 71 ms 15256 KB Output is correct
105 Correct 72 ms 15048 KB Output is correct
106 Correct 72 ms 15048 KB Output is correct
107 Correct 138 ms 11316 KB Output is correct
108 Correct 148 ms 11464 KB Output is correct
109 Correct 163 ms 11460 KB Output is correct
110 Correct 64 ms 18892 KB Output is correct
111 Correct 64 ms 16292 KB Output is correct
112 Correct 61 ms 16332 KB Output is correct
113 Correct 61 ms 15052 KB Output is correct
114 Correct 158 ms 11812 KB Output is correct
115 Correct 175 ms 11464 KB Output is correct
116 Correct 66 ms 15048 KB Output is correct
117 Correct 70 ms 13052 KB Output is correct
118 Correct 71 ms 12492 KB Output is correct
119 Correct 73 ms 12236 KB Output is correct
120 Correct 66 ms 13156 KB Output is correct
121 Correct 67 ms 12492 KB Output is correct
122 Correct 69 ms 12232 KB Output is correct
123 Correct 76 ms 13204 KB Output is correct
124 Correct 78 ms 12488 KB Output is correct
125 Correct 78 ms 12228 KB Output is correct
126 Correct 73 ms 13260 KB Output is correct
127 Correct 73 ms 12596 KB Output is correct
128 Correct 75 ms 12116 KB Output is correct