Submission #727414

# Submission time Handle Problem Language Result Execution time Memory
727414 2023-04-20T15:49:07 Z model_code Passport (JOI23_passport) C++17
100 / 100
531 ms 49224 KB
/*
	FULL-SCORE SOLUTION OF "PASSPORT"
	- The way of implementation is a bit different from official review
		- It does not create graph explicitly
		- It uses Dijkstra's algorithm instead of BFS, but the time complexity does not change in this implementation
	- Time Complexity: O(N log N)
*/

#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

class state {
public:
	int pos, cost;
	state() : pos(-1), cost(0) {}
	state(int pos_, int cost_) : pos(pos_), cost(cost_) {}
	bool operator<(const state& s) const {
		return cost > s.cost;
	}
};

vector<int> solve(int N, int Q, const vector<int>& L, const vector<int>& R, const vector<int>& X) {
	const int INF = 1012345678;

	// step #1. do segment-tree-like precalculation
	int sz = 1;
	while (sz < N) {
		sz *= 2;
	}
	vector<vector<int> > dat(sz * 2);
	for (int i = 0; i < N; i++) {
		int cl = L[i] + sz;
		int cr = R[i] + sz;
		while (cl != cr) {
			if (cl & 1) {
				dat[cl++].push_back(i);
			}
			if (cr & 1) {
				dat[--cr].push_back(i);
			}
			cl >>= 1;
			cr >>= 1;
		}
	}

	// step #2. function to calculate distance
	auto calc = [&](const vector<int>& initial_dist) {
		vector<int> dist = initial_dist;
		priority_queue<state> que;
		for (int i = 0; i < N; i++) {
			if (dist[i] != INF) {
				que.push(state(i, dist[i]));
			}
		}
		vector<bool> vis(N, false);
		vector<bool> used(sz * 2, false);
		while (!que.empty()) {
			int u = que.top().pos;
			que.pop();
			if (!vis[u]) {
				vis[u] = true;
				int idx = u + sz;
				while (idx >= 1 && !used[idx]) {
					for (int j : dat[idx]) {
						if (dist[j] > dist[u] + 1) {
							dist[j] = dist[u] + 1;
							que.push(state(j, dist[j]));
						}
					}
					used[idx] = true;
					idx >>= 1;
				}
			}
		}
		return dist;
	};

	// step #3. main part
	vector<int> ini1(N, INF); ini1[0] = 0;
	vector<int> ini2(N, INF); ini2[N - 1] = 0;
	vector<int> d1 = calc(ini1); d1[0] = 1;
	vector<int> d2 = calc(ini2); d2[N - 1] = 1;
	vector<int> ini3(N);
	for (int i = 0; i < N; i++) {
		ini3[i] = min(d1[i] + d2[i], INF);
	}
	vector<int> d3 = calc(ini3);
	vector<int> answer(Q);
	for (int i = 0; i < Q; i++) {
		answer[i] = (d3[X[i]] != INF ? d3[X[i]] - 1 : -1);
	}

	return answer;
}

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int N;
	cin >> N;
	vector<int> L(N), R(N);
	for (int i = 0; i < N; i++) {
		cin >> L[i] >> R[i];
		L[i] -= 1;
	}
	int Q;
	cin >> Q;
	vector<int> X(Q);
	for (int i = 0; i < Q; i++) {
		cin >> X[i];
		X[i] -= 1;
	}
	vector<int> res = solve(N, Q, L, R, X);
	for (int i = 0; i < Q; i++) {
		cout << res[i] << '\n';
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 511 ms 44984 KB Output is correct
5 Correct 209 ms 33832 KB Output is correct
6 Correct 133 ms 32960 KB Output is correct
7 Correct 177 ms 32480 KB Output is correct
8 Correct 112 ms 29040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 3 ms 752 KB Output is correct
17 Correct 3 ms 724 KB Output is correct
18 Correct 6 ms 852 KB Output is correct
19 Correct 3 ms 852 KB Output is correct
20 Correct 2 ms 752 KB Output is correct
21 Correct 2 ms 716 KB Output is correct
22 Correct 2 ms 724 KB Output is correct
23 Correct 2 ms 724 KB Output is correct
24 Correct 3 ms 752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 3 ms 752 KB Output is correct
17 Correct 3 ms 724 KB Output is correct
18 Correct 6 ms 852 KB Output is correct
19 Correct 3 ms 852 KB Output is correct
20 Correct 2 ms 752 KB Output is correct
21 Correct 2 ms 716 KB Output is correct
22 Correct 2 ms 724 KB Output is correct
23 Correct 2 ms 724 KB Output is correct
24 Correct 3 ms 752 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 4 ms 760 KB Output is correct
28 Correct 4 ms 724 KB Output is correct
29 Correct 2 ms 724 KB Output is correct
30 Correct 6 ms 724 KB Output is correct
31 Correct 3 ms 736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 511 ms 44984 KB Output is correct
5 Correct 209 ms 33832 KB Output is correct
6 Correct 133 ms 32960 KB Output is correct
7 Correct 177 ms 32480 KB Output is correct
8 Correct 112 ms 29040 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 320 KB Output is correct
24 Correct 3 ms 752 KB Output is correct
25 Correct 3 ms 724 KB Output is correct
26 Correct 6 ms 852 KB Output is correct
27 Correct 3 ms 852 KB Output is correct
28 Correct 2 ms 752 KB Output is correct
29 Correct 2 ms 716 KB Output is correct
30 Correct 2 ms 724 KB Output is correct
31 Correct 2 ms 724 KB Output is correct
32 Correct 3 ms 752 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 4 ms 760 KB Output is correct
36 Correct 4 ms 724 KB Output is correct
37 Correct 2 ms 724 KB Output is correct
38 Correct 6 ms 724 KB Output is correct
39 Correct 3 ms 736 KB Output is correct
40 Correct 531 ms 49224 KB Output is correct
41 Correct 255 ms 35948 KB Output is correct
42 Correct 422 ms 45768 KB Output is correct
43 Correct 278 ms 46536 KB Output is correct
44 Correct 142 ms 34984 KB Output is correct
45 Correct 175 ms 36580 KB Output is correct
46 Correct 116 ms 15880 KB Output is correct
47 Correct 288 ms 39920 KB Output is correct
48 Correct 241 ms 37648 KB Output is correct