Submission #795759

# Submission time Handle Problem Language Result Execution time Memory
795759 2023-07-27T14:27:11 Z hugo_pm Passport (JOI23_passport) C++17
100 / 100
963 ms 87616 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))

template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }

using pii = pair<int, int>;
using vi = vector<int>;

string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
	bool first = true;
	string res = "[";
	for (const auto &x : v) {
		if (!first)
			res += ", ";
		first = false;
		res += to_string(x);
	}
	res += "]";
	return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
	cout << ' ' << to_string(H);
	dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

const int MAX_N = 1 << 18;
const int INF = 1e9;
pii itvs[MAX_N];
vector<int> tree[2*MAX_N];

// inclus
void addItv(int node, int leftNode, int rightNode, int idItv) {
	const auto [leftAdd, rightAdd] = itvs[idItv];
	if (rightNode < leftAdd || rightAdd < leftNode) {
		return;
	}
	if (leftAdd <= leftNode && rightNode <= rightAdd) {
		// dbg(idItv, "added"s, node);
		tree[node].push_back(idItv);
		return;
	}
	if (leftNode < rightNode) {
		int mid = (leftNode + rightNode) / 2;
		addItv(2*node, leftNode, mid, idItv);
		addItv(2*node+1, mid+1, rightNode, idItv);
	}
}

void popItv(int node, int leftNode, int rightNode, int leaf, vector<int> &acc) {
	// dbg(node, leftNode, rightNode, tree[node]);
	for (int x : tree[node]) {
		acc.push_back(x);
	}
	tree[node].clear(), tree[node].shrink_to_fit();
	if (leftNode < rightNode) {
		int mid = (leftNode + rightNode) / 2;
		if (leaf <= mid) {
			popItv(2*node, leftNode, mid, leaf, acc);
		} else {
			popItv(2*node+1, mid+1, rightNode, leaf, acc);
		}
	}
}

int N, Q;
vector<int> calcDist(int source) {
	rep(node, 0, N) {
		addItv(1, 0, N-1, node);
	}
	vector<int> dist(N, INF);
	dist[source] = 0;
	queue<int> bfs;
	bfs.push(source);
	while (!bfs.empty()) {
		int cur = bfs.front();
		bfs.pop();
		vector<int> todo;
		popItv(1, 0, N-1, cur, todo);
		for (int voisin : todo) if (dist[voisin] == INF) {
			dist[voisin] = dist[cur]+1;
			bfs.push(voisin);
		}
	}
	return dist;
}
vector<int> d0, dN;
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> N;
	rep(i, 0, N) {
		cin >> itvs[i].first >> itvs[i].second;
		--itvs[i].first, --itvs[i].second;
	}
	d0 = calcDist(0);
	dbg(d0);
	dN = calcDist(N-1);
	dbg(dN);
	d0[0]++, dN[N-1]++;

	priority_queue<pii, vector<pii>, greater<pii>> pq;
	vector<int> dist(N, INF);
	rep(node, 0, N) {
		addItv(1, 0, N-1, node);
		dist[node] = d0[node] + dN[node] - 1;
		if (dist[node] < INF)
			pq.push({dist[node], node});
	}
	while (!pq.empty()) {
		auto [cd, node] = pq.top();
		pq.pop();
		if (cd != dist[node]) continue;
		vector<int> todo;
		popItv(1, 0, N-1, node, todo);
		for (int voisin : todo) if (dist[voisin] > dist[node]+1) {
			dist[voisin] = dist[node]+1;
			pq.push({dist[voisin], voisin});
		}		
	}
	for (int &x : dist) if (x >= INF) {
		x = -1;
	}
	dbg(dist);
	cin >> Q;
	rep(iReq, 0, Q) {
		int node; cin >> node;
		--node;
		cout << dist[node] << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12628 KB Output is correct
2 Correct 6 ms 12628 KB Output is correct
3 Correct 6 ms 12524 KB Output is correct
4 Correct 936 ms 61888 KB Output is correct
5 Correct 445 ms 40188 KB Output is correct
6 Correct 267 ms 36472 KB Output is correct
7 Correct 110 ms 31364 KB Output is correct
8 Correct 120 ms 50924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12640 KB Output is correct
2 Correct 6 ms 12628 KB Output is correct
3 Correct 6 ms 12640 KB Output is correct
4 Correct 8 ms 12628 KB Output is correct
5 Correct 7 ms 12628 KB Output is correct
6 Correct 6 ms 12636 KB Output is correct
7 Correct 6 ms 12640 KB Output is correct
8 Correct 7 ms 12628 KB Output is correct
9 Correct 7 ms 12628 KB Output is correct
10 Correct 8 ms 12636 KB Output is correct
11 Correct 7 ms 12644 KB Output is correct
12 Correct 7 ms 12640 KB Output is correct
13 Correct 6 ms 12628 KB Output is correct
14 Correct 8 ms 12644 KB Output is correct
15 Correct 7 ms 12644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12640 KB Output is correct
2 Correct 6 ms 12628 KB Output is correct
3 Correct 6 ms 12640 KB Output is correct
4 Correct 8 ms 12628 KB Output is correct
5 Correct 7 ms 12628 KB Output is correct
6 Correct 6 ms 12636 KB Output is correct
7 Correct 6 ms 12640 KB Output is correct
8 Correct 7 ms 12628 KB Output is correct
9 Correct 7 ms 12628 KB Output is correct
10 Correct 8 ms 12636 KB Output is correct
11 Correct 7 ms 12644 KB Output is correct
12 Correct 7 ms 12640 KB Output is correct
13 Correct 6 ms 12628 KB Output is correct
14 Correct 8 ms 12644 KB Output is correct
15 Correct 7 ms 12644 KB Output is correct
16 Correct 11 ms 13044 KB Output is correct
17 Correct 12 ms 12904 KB Output is correct
18 Correct 14 ms 13164 KB Output is correct
19 Correct 11 ms 13140 KB Output is correct
20 Correct 10 ms 12884 KB Output is correct
21 Correct 12 ms 12912 KB Output is correct
22 Correct 9 ms 12756 KB Output is correct
23 Correct 11 ms 13012 KB Output is correct
24 Correct 8 ms 13288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12640 KB Output is correct
2 Correct 6 ms 12628 KB Output is correct
3 Correct 6 ms 12640 KB Output is correct
4 Correct 8 ms 12628 KB Output is correct
5 Correct 7 ms 12628 KB Output is correct
6 Correct 6 ms 12636 KB Output is correct
7 Correct 6 ms 12640 KB Output is correct
8 Correct 7 ms 12628 KB Output is correct
9 Correct 7 ms 12628 KB Output is correct
10 Correct 8 ms 12636 KB Output is correct
11 Correct 7 ms 12644 KB Output is correct
12 Correct 7 ms 12640 KB Output is correct
13 Correct 6 ms 12628 KB Output is correct
14 Correct 8 ms 12644 KB Output is correct
15 Correct 7 ms 12644 KB Output is correct
16 Correct 11 ms 13044 KB Output is correct
17 Correct 12 ms 12904 KB Output is correct
18 Correct 14 ms 13164 KB Output is correct
19 Correct 11 ms 13140 KB Output is correct
20 Correct 10 ms 12884 KB Output is correct
21 Correct 12 ms 12912 KB Output is correct
22 Correct 9 ms 12756 KB Output is correct
23 Correct 11 ms 13012 KB Output is correct
24 Correct 8 ms 13288 KB Output is correct
25 Correct 6 ms 12628 KB Output is correct
26 Correct 6 ms 12628 KB Output is correct
27 Correct 11 ms 13140 KB Output is correct
28 Correct 11 ms 12908 KB Output is correct
29 Correct 10 ms 12956 KB Output is correct
30 Correct 9 ms 12884 KB Output is correct
31 Correct 10 ms 13012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12628 KB Output is correct
2 Correct 6 ms 12628 KB Output is correct
3 Correct 6 ms 12524 KB Output is correct
4 Correct 936 ms 61888 KB Output is correct
5 Correct 445 ms 40188 KB Output is correct
6 Correct 267 ms 36472 KB Output is correct
7 Correct 110 ms 31364 KB Output is correct
8 Correct 120 ms 50924 KB Output is correct
9 Correct 7 ms 12640 KB Output is correct
10 Correct 6 ms 12628 KB Output is correct
11 Correct 6 ms 12640 KB Output is correct
12 Correct 8 ms 12628 KB Output is correct
13 Correct 7 ms 12628 KB Output is correct
14 Correct 6 ms 12636 KB Output is correct
15 Correct 6 ms 12640 KB Output is correct
16 Correct 7 ms 12628 KB Output is correct
17 Correct 7 ms 12628 KB Output is correct
18 Correct 8 ms 12636 KB Output is correct
19 Correct 7 ms 12644 KB Output is correct
20 Correct 7 ms 12640 KB Output is correct
21 Correct 6 ms 12628 KB Output is correct
22 Correct 8 ms 12644 KB Output is correct
23 Correct 7 ms 12644 KB Output is correct
24 Correct 11 ms 13044 KB Output is correct
25 Correct 12 ms 12904 KB Output is correct
26 Correct 14 ms 13164 KB Output is correct
27 Correct 11 ms 13140 KB Output is correct
28 Correct 10 ms 12884 KB Output is correct
29 Correct 12 ms 12912 KB Output is correct
30 Correct 9 ms 12756 KB Output is correct
31 Correct 11 ms 13012 KB Output is correct
32 Correct 8 ms 13288 KB Output is correct
33 Correct 6 ms 12628 KB Output is correct
34 Correct 6 ms 12628 KB Output is correct
35 Correct 11 ms 13140 KB Output is correct
36 Correct 11 ms 12908 KB Output is correct
37 Correct 10 ms 12956 KB Output is correct
38 Correct 9 ms 12884 KB Output is correct
39 Correct 10 ms 13012 KB Output is correct
40 Correct 963 ms 63732 KB Output is correct
41 Correct 444 ms 41920 KB Output is correct
42 Correct 567 ms 66420 KB Output is correct
43 Correct 544 ms 66200 KB Output is correct
44 Correct 308 ms 39164 KB Output is correct
45 Correct 326 ms 42788 KB Output is correct
46 Correct 139 ms 23712 KB Output is correct
47 Correct 408 ms 45592 KB Output is correct
48 Correct 430 ms 87616 KB Output is correct