Submission #1049685

# Submission time Handle Problem Language Result Execution time Memory
1049685 2024-08-09T03:58:30 Z Gromp15 Ancient Books (IOI17_books) C++17
100 / 100
1448 ms 363364 KB
#include <bits/stdc++.h>
#define ll long long
#include "books.h"
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define ar array
using namespace std;
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

struct seg {
	int N; vector<vector<ar<int, 2>>> adj;
	seg(int n) : N(1<<(__lg(n)+1)), adj(2*N) {
		for (int i = 2; i < 2*N; i++) adj[i].push_back({i >> 1, 0});
	}
	void add_edge(int x, int y) {
		adj[x+N].push_back({y+N, 1});
	}
	void add_edge(int node, int nl, int nr, int ql, int qr, int x) { // l and r go to x
		if (ql > nr || qr < nl) return;
		if (ql <= nl && nr <= qr) {
			adj[node].push_back({x + N, 0});
			return;
		}
		int mid = (nl+nr)/2;
		add_edge(node * 2, nl, mid, ql, qr, x);
		add_edge(node * 2 + 1, mid + 1, nr, ql, qr, x);
	}
	void bfs(vector<int>& dist, vector<int> nodes) {
		vector<int> dist2(2 * N, 1e9);
		for (int i = 0; i < sz(dist); i++) dist2[i + N] = dist[i];
		deque<int> q;
		vector<int> idx(sz(dist));
		iota(all(idx), 0);
		sort(all(idx), [&](int x, int y) { return dist[x] < dist[y]; });
		int on = 1;
		q.emplace_back(idx[0] + N);
		while (q.size()) {
			int v = q[0]; q.pop_front();
			for (auto [u, w] : adj[v]) { 
				if (ckmin(dist2[u], dist2[v] + w)) { 
					if (!w) q.emplace_front(u);
					else q.emplace_back(u);
				}
			}
			if (on < sz(dist) && (q.empty() || dist[idx[on]] <= dist2[q[0]])) {
				q.emplace_front(idx[on++] + N);
			}
		}
		assert(on == sz(dist));
		for (int i = 0; i < sz(dist); i++) dist[i] = dist2[i + N];
	}
};

long long minimum_walk(std::vector<int> p, int s) {
	int n = p.size();
	int l = s, r = s;
	bool f = 0;
	for (int i = s; i >= 0; i--) if (i != p[i]) l = i, f = 1;
	for (int i = s; i < n; i++) if (i != p[i]) r = i, f = 1;
	if (!f) {
		return 0;
	}
	seg st(n);
	for (int i = 0; i < n; i++) {
		if (i) st.add_edge(i, i-1);
		if (i+1 < n) st.add_edge(i, i+1);
	}
	vector<bool> vis(n);
	ll ans = 0;
	for (int i = 0; i < n; i++) if (!vis[i]) {
		int L = i, R = i, tmp = i;
		vector<int> pth;
		while (!vis[tmp]) { 
			ans += abs(tmp - p[tmp]);
			ckmax(R, tmp), vis[tmp] = 1;
			pth.emplace_back(tmp);
			tmp = p[tmp];
		}
		for (int v : pth) st.add_edge(1, 0, st.N-1, L, R, v);
	}
	vector<int> d1(n, 1e9), d2(n, 1e9);
	d1[l] = 0, d2[r] = 0;
	st.bfs(d1, {l});
	st.bfs(d2, {r});
	vector<int> d3(n);
	for (int i = 0; i < n; i++) d3[i] = d1[i] + d2[i];
	vector<int> tot(n);
	iota(all(tot), 0);
	st.bfs(d3, tot);
	return ans + 2 * d3[s];
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 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 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 604 KB Output is correct
23 Correct 0 ms 604 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 676 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 604 KB Output is correct
23 Correct 0 ms 604 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 676 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1148 ms 303584 KB Output is correct
31 Correct 1264 ms 363364 KB Output is correct
32 Correct 51 ms 8020 KB Output is correct
33 Correct 568 ms 189444 KB Output is correct
34 Correct 572 ms 189444 KB Output is correct
35 Correct 662 ms 197376 KB Output is correct
36 Correct 751 ms 219152 KB Output is correct
37 Correct 759 ms 261884 KB Output is correct
38 Correct 777 ms 284068 KB Output is correct
39 Correct 776 ms 284420 KB Output is correct
40 Correct 795 ms 303972 KB Output is correct
41 Correct 999 ms 315628 KB Output is correct
42 Correct 977 ms 358844 KB Output is correct
43 Correct 1446 ms 215040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 620 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 604 KB Output is correct
23 Correct 0 ms 604 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 676 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1148 ms 303584 KB Output is correct
31 Correct 1264 ms 363364 KB Output is correct
32 Correct 51 ms 8020 KB Output is correct
33 Correct 568 ms 189444 KB Output is correct
34 Correct 572 ms 189444 KB Output is correct
35 Correct 662 ms 197376 KB Output is correct
36 Correct 751 ms 219152 KB Output is correct
37 Correct 759 ms 261884 KB Output is correct
38 Correct 777 ms 284068 KB Output is correct
39 Correct 776 ms 284420 KB Output is correct
40 Correct 795 ms 303972 KB Output is correct
41 Correct 999 ms 315628 KB Output is correct
42 Correct 977 ms 358844 KB Output is correct
43 Correct 1446 ms 215040 KB Output is correct
44 Correct 1 ms 600 KB Output is correct
45 Correct 1 ms 604 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 0 ms 604 KB Output is correct
48 Correct 1 ms 604 KB Output is correct
49 Correct 1 ms 604 KB Output is correct
50 Correct 1 ms 604 KB Output is correct
51 Correct 1 ms 604 KB Output is correct
52 Correct 1 ms 604 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 1 ms 604 KB Output is correct
55 Correct 1 ms 604 KB Output is correct
56 Correct 1 ms 604 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 1 ms 620 KB Output is correct
59 Correct 1 ms 604 KB Output is correct
60 Correct 1 ms 604 KB Output is correct
61 Correct 1 ms 604 KB Output is correct
62 Correct 1 ms 604 KB Output is correct
63 Correct 1 ms 604 KB Output is correct
64 Correct 937 ms 189732 KB Output is correct
65 Correct 968 ms 184120 KB Output is correct
66 Correct 907 ms 263576 KB Output is correct
67 Correct 915 ms 271876 KB Output is correct
68 Correct 63 ms 25800 KB Output is correct
69 Correct 66 ms 27336 KB Output is correct
70 Correct 66 ms 25356 KB Output is correct
71 Correct 68 ms 22964 KB Output is correct
72 Correct 56 ms 21444 KB Output is correct
73 Correct 68 ms 34292 KB Output is correct
74 Correct 575 ms 189440 KB Output is correct
75 Correct 571 ms 189476 KB Output is correct
76 Correct 1441 ms 209048 KB Output is correct
77 Correct 1448 ms 212120 KB Output is correct
78 Correct 1274 ms 209560 KB Output is correct
79 Correct 1310 ms 208852 KB Output is correct
80 Correct 57 ms 8016 KB Output is correct
81 Correct 1058 ms 349688 KB Output is correct
82 Correct 1076 ms 349776 KB Output is correct
83 Correct 956 ms 215936 KB Output is correct
84 Correct 969 ms 220412 KB Output is correct
85 Correct 898 ms 227332 KB Output is correct
86 Correct 861 ms 246532 KB Output is correct
87 Correct 882 ms 243784 KB Output is correct
88 Correct 935 ms 223584 KB Output is correct
89 Correct 972 ms 214124 KB Output is correct
90 Correct 906 ms 272384 KB Output is correct
91 Correct 874 ms 264192 KB Output is correct
92 Correct 897 ms 302164 KB Output is correct