Submission #873847

# Submission time Handle Problem Language Result Execution time Memory
873847 2023-11-16T00:47:39 Z NK_ Construction of Highway (JOI18_construction) C++17
100 / 100
310 ms 21120 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 << endl;
	};

	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 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 476 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 2 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 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 456 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 2 ms 348 KB Output is correct
31 Correct 1 ms 460 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 520 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 460 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 476 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 2 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 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 456 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 2 ms 348 KB Output is correct
31 Correct 1 ms 460 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 520 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 460 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 504 KB Output is correct
45 Correct 2 ms 344 KB Output is correct
46 Correct 9 ms 860 KB Output is correct
47 Correct 9 ms 860 KB Output is correct
48 Correct 9 ms 860 KB Output is correct
49 Correct 7 ms 1112 KB Output is correct
50 Correct 7 ms 1116 KB Output is correct
51 Correct 7 ms 1116 KB Output is correct
52 Correct 7 ms 1116 KB Output is correct
53 Correct 9 ms 1112 KB Output is correct
54 Correct 7 ms 1116 KB Output is correct
55 Correct 7 ms 1112 KB Output is correct
56 Correct 7 ms 1116 KB Output is correct
57 Correct 9 ms 800 KB Output is correct
58 Correct 10 ms 968 KB Output is correct
59 Correct 9 ms 936 KB Output is correct
60 Correct 9 ms 1112 KB Output is correct
61 Correct 7 ms 1116 KB Output is correct
62 Correct 7 ms 1112 KB Output is correct
63 Correct 7 ms 1116 KB Output is correct
64 Correct 8 ms 1172 KB Output is correct
65 Correct 8 ms 860 KB Output is correct
66 Correct 9 ms 728 KB Output is correct
67 Correct 8 ms 860 KB Output is correct
68 Correct 6 ms 1112 KB Output is correct
69 Correct 7 ms 980 KB Output is correct
70 Correct 7 ms 1020 KB Output is correct
71 Correct 7 ms 860 KB Output is correct
72 Correct 9 ms 892 KB Output is correct
73 Correct 9 ms 884 KB Output is correct
74 Correct 7 ms 860 KB Output is correct
75 Correct 7 ms 980 KB Output is correct
76 Correct 7 ms 860 KB Output is correct
77 Correct 7 ms 860 KB Output is correct
78 Correct 7 ms 940 KB Output is correct
79 Correct 9 ms 956 KB Output is correct
80 Correct 7 ms 860 KB Output is correct
81 Correct 7 ms 860 KB Output is correct
82 Correct 7 ms 1248 KB Output is correct
83 Correct 7 ms 860 KB Output is correct
84 Correct 7 ms 860 KB Output is correct
85 Correct 7 ms 860 KB Output is correct
86 Correct 7 ms 960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 476 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 2 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 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 456 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 2 ms 348 KB Output is correct
31 Correct 1 ms 460 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 520 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 460 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 504 KB Output is correct
45 Correct 2 ms 344 KB Output is correct
46 Correct 9 ms 860 KB Output is correct
47 Correct 9 ms 860 KB Output is correct
48 Correct 9 ms 860 KB Output is correct
49 Correct 7 ms 1112 KB Output is correct
50 Correct 7 ms 1116 KB Output is correct
51 Correct 7 ms 1116 KB Output is correct
52 Correct 7 ms 1116 KB Output is correct
53 Correct 9 ms 1112 KB Output is correct
54 Correct 7 ms 1116 KB Output is correct
55 Correct 7 ms 1112 KB Output is correct
56 Correct 7 ms 1116 KB Output is correct
57 Correct 9 ms 800 KB Output is correct
58 Correct 10 ms 968 KB Output is correct
59 Correct 9 ms 936 KB Output is correct
60 Correct 9 ms 1112 KB Output is correct
61 Correct 7 ms 1116 KB Output is correct
62 Correct 7 ms 1112 KB Output is correct
63 Correct 7 ms 1116 KB Output is correct
64 Correct 8 ms 1172 KB Output is correct
65 Correct 8 ms 860 KB Output is correct
66 Correct 9 ms 728 KB Output is correct
67 Correct 8 ms 860 KB Output is correct
68 Correct 6 ms 1112 KB Output is correct
69 Correct 7 ms 980 KB Output is correct
70 Correct 7 ms 1020 KB Output is correct
71 Correct 7 ms 860 KB Output is correct
72 Correct 9 ms 892 KB Output is correct
73 Correct 9 ms 884 KB Output is correct
74 Correct 7 ms 860 KB Output is correct
75 Correct 7 ms 980 KB Output is correct
76 Correct 7 ms 860 KB Output is correct
77 Correct 7 ms 860 KB Output is correct
78 Correct 7 ms 940 KB Output is correct
79 Correct 9 ms 956 KB Output is correct
80 Correct 7 ms 860 KB Output is correct
81 Correct 7 ms 860 KB Output is correct
82 Correct 7 ms 1248 KB Output is correct
83 Correct 7 ms 860 KB Output is correct
84 Correct 7 ms 860 KB Output is correct
85 Correct 7 ms 860 KB Output is correct
86 Correct 7 ms 960 KB Output is correct
87 Correct 23 ms 1628 KB Output is correct
88 Correct 73 ms 4364 KB Output is correct
89 Correct 269 ms 13372 KB Output is correct
90 Correct 274 ms 13552 KB Output is correct
91 Correct 278 ms 13512 KB Output is correct
92 Correct 171 ms 21120 KB Output is correct
93 Correct 174 ms 21024 KB Output is correct
94 Correct 181 ms 21092 KB Output is correct
95 Correct 181 ms 17988 KB Output is correct
96 Correct 175 ms 18296 KB Output is correct
97 Correct 188 ms 18532 KB Output is correct
98 Correct 179 ms 18484 KB Output is correct
99 Correct 187 ms 17332 KB Output is correct
100 Correct 284 ms 13252 KB Output is correct
101 Correct 308 ms 13528 KB Output is correct
102 Correct 299 ms 13564 KB Output is correct
103 Correct 310 ms 13776 KB Output is correct
104 Correct 182 ms 17156 KB Output is correct
105 Correct 183 ms 17188 KB Output is correct
106 Correct 193 ms 17336 KB Output is correct
107 Correct 251 ms 12744 KB Output is correct
108 Correct 261 ms 12792 KB Output is correct
109 Correct 272 ms 13000 KB Output is correct
110 Correct 168 ms 20368 KB Output is correct
111 Correct 175 ms 17972 KB Output is correct
112 Correct 174 ms 17904 KB Output is correct
113 Correct 170 ms 16772 KB Output is correct
114 Correct 287 ms 13128 KB Output is correct
115 Correct 294 ms 13036 KB Output is correct
116 Correct 187 ms 16592 KB Output is correct
117 Correct 177 ms 15048 KB Output is correct
118 Correct 194 ms 14532 KB Output is correct
119 Correct 190 ms 13968 KB Output is correct
120 Correct 170 ms 14540 KB Output is correct
121 Correct 196 ms 14132 KB Output is correct
122 Correct 174 ms 13768 KB Output is correct
123 Correct 192 ms 15064 KB Output is correct
124 Correct 208 ms 14452 KB Output is correct
125 Correct 193 ms 14028 KB Output is correct
126 Correct 186 ms 14836 KB Output is correct
127 Correct 182 ms 14028 KB Output is correct
128 Correct 188 ms 13768 KB Output is correct