Submission #766501

# Submission time Handle Problem Language Result Execution time Memory
766501 2023-06-25T17:28:12 Z MetalPower Passport (JOI23_passport) C++14
100 / 100
419 ms 41404 KB
#include <bits/stdc++.h>
using namespace std;

#define pii pair<int, int>
#define fi first
#define se second

const int INF = 1e9 + 7;
const int MX = 2e5 + 10;

int N, Q, l[MX], r[MX], dist[MX][4];
bool vis[MX];

struct segtree{
	vector<int> st[MX << 1];

	void reset(){
		for(int i = 0; i < (MX << 1); i++) st[i].clear();
	}

	void upd(int l, int r, int v){
		for(l += MX, r += MX + 1; l < r; l >>= 1, r >>= 1){
			if(l & 1){
				st[l].push_back(v);
				l++;
			}
			if(r & 1){
				--r;
				st[r].push_back(v);
			}
		}
	}

	vector<int> que(int p){
		vector<int> res;
		for(p += MX; p > 0; p >>= 1){
			for(int x : st[p]){
				if(!vis[x]){
					vis[x] = true;
					res.push_back(x);
				}
			}
			st[p].clear();
		}
		return res;
	}
} S;

void bfs1(){

	queue<int> q;
	dist[0][0] = 0; vis[0] = 1; q.push(0);

	for(; !q.empty(); ){
		int cd = q.front(); q.pop();

		vector<int> adj = S.que(cd);

		/*
		cout << "next of " << cd << " := ";
		for(int nx : adj) cout << nx << " ";
		cout << '\n';
		*/

		for(int nx : adj){
			dist[nx][0] = dist[cd][0] + 1;
			q.push(nx);
		}
	}
}

void bfs2(){

	queue<int> q;
	dist[N - 1][1] = 0; vis[N - 1] = 1; q.push(N - 1);

	for(; !q.empty(); ){
		int cd = q.front(); q.pop();

		vector<int> adj = S.que(cd);

		/*
		cout << "next of " << cd << " := ";
		for(int nx : adj) cout << nx << " ";
		cout << '\n';
		*/

		for(int nx : adj){
			dist[nx][1] = dist[cd][1] + 1;
			q.push(nx);
		}
	}
}

priority_queue<pii, vector<pii>, greater<pii>> pq;

void dijkstra(){

	for(int i = 0; i < N; i++){
		if(i == 0){
			if(dist[i][1] != INF){
				dist[i][2] = dist[i][1];
				pq.push({dist[i][1], i});
			}
		}else if(i == N - 1){
			if(dist[i][0] != INF){
				dist[i][2] = dist[i][0];
				pq.push({dist[i][0], i});
			}
		}else{
			if(dist[i][0] != INF && dist[i][1] != INF){
				dist[i][2] = dist[i][0] + dist[i][1] - 1;
				pq.push({dist[i][0] + dist[i][1] - 1, i});
			}
		}
	}

	for(; !pq.empty(); ){
		int cdist = pq.top().fi, cnode = pq.top().se; pq.pop();
		if(dist[cnode][2] < cdist) continue;

		vector<int> adj = S.que(cnode);
		for(int nx : adj){
			if(dist[nx][2] > cdist + 1){
				dist[nx][2] = cdist + 1;
				pq.push({dist[nx][2], nx});
			}
		}
	}
}

void init(){
	S.reset();
	for(int i = 0; i < N; i++){
		S.upd(l[i], r[i], i);
		vis[i] = 0;
	}
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin >> N;
	for(int i = 0; i < N; i++){
		cin >> l[i] >> r[i]; l[i]--, r[i]--;
		S.upd(l[i], r[i], i);
	}

	for(int i = 0; i < MX; i++) dist[i][0] = dist[i][1] = dist[i][2] = dist[i][3] = INF;

	init();
	bfs1();

	init();
	bfs2();

	init();
	dijkstra();

	cin >> Q;
	for(int i = 0; i < Q; i++){
		int v; cin >> v; v--;
		int ans = dist[v][2];
		if(ans >= INF) cout << -1 << '\n';
		else cout << ans << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12756 KB Output is correct
2 Correct 8 ms 12756 KB Output is correct
3 Correct 8 ms 12796 KB Output is correct
4 Correct 377 ms 39444 KB Output is correct
5 Correct 194 ms 29640 KB Output is correct
6 Correct 103 ms 28684 KB Output is correct
7 Correct 121 ms 31060 KB Output is correct
8 Correct 71 ms 29976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12756 KB Output is correct
2 Correct 8 ms 12756 KB Output is correct
3 Correct 7 ms 12756 KB Output is correct
4 Correct 7 ms 12756 KB Output is correct
5 Correct 8 ms 12792 KB Output is correct
6 Correct 8 ms 12792 KB Output is correct
7 Correct 7 ms 12800 KB Output is correct
8 Correct 8 ms 12756 KB Output is correct
9 Correct 7 ms 12792 KB Output is correct
10 Correct 8 ms 12800 KB Output is correct
11 Correct 8 ms 12756 KB Output is correct
12 Correct 8 ms 12884 KB Output is correct
13 Correct 8 ms 12884 KB Output is correct
14 Correct 8 ms 12884 KB Output is correct
15 Correct 8 ms 12884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12756 KB Output is correct
2 Correct 8 ms 12756 KB Output is correct
3 Correct 7 ms 12756 KB Output is correct
4 Correct 7 ms 12756 KB Output is correct
5 Correct 8 ms 12792 KB Output is correct
6 Correct 8 ms 12792 KB Output is correct
7 Correct 7 ms 12800 KB Output is correct
8 Correct 8 ms 12756 KB Output is correct
9 Correct 7 ms 12792 KB Output is correct
10 Correct 8 ms 12800 KB Output is correct
11 Correct 8 ms 12756 KB Output is correct
12 Correct 8 ms 12884 KB Output is correct
13 Correct 8 ms 12884 KB Output is correct
14 Correct 8 ms 12884 KB Output is correct
15 Correct 8 ms 12884 KB Output is correct
16 Correct 10 ms 13012 KB Output is correct
17 Correct 9 ms 13012 KB Output is correct
18 Correct 11 ms 13060 KB Output is correct
19 Correct 10 ms 13080 KB Output is correct
20 Correct 9 ms 13016 KB Output is correct
21 Correct 9 ms 13064 KB Output is correct
22 Correct 9 ms 13060 KB Output is correct
23 Correct 9 ms 13140 KB Output is correct
24 Correct 9 ms 13016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12756 KB Output is correct
2 Correct 8 ms 12756 KB Output is correct
3 Correct 7 ms 12756 KB Output is correct
4 Correct 7 ms 12756 KB Output is correct
5 Correct 8 ms 12792 KB Output is correct
6 Correct 8 ms 12792 KB Output is correct
7 Correct 7 ms 12800 KB Output is correct
8 Correct 8 ms 12756 KB Output is correct
9 Correct 7 ms 12792 KB Output is correct
10 Correct 8 ms 12800 KB Output is correct
11 Correct 8 ms 12756 KB Output is correct
12 Correct 8 ms 12884 KB Output is correct
13 Correct 8 ms 12884 KB Output is correct
14 Correct 8 ms 12884 KB Output is correct
15 Correct 8 ms 12884 KB Output is correct
16 Correct 10 ms 13012 KB Output is correct
17 Correct 9 ms 13012 KB Output is correct
18 Correct 11 ms 13060 KB Output is correct
19 Correct 10 ms 13080 KB Output is correct
20 Correct 9 ms 13016 KB Output is correct
21 Correct 9 ms 13064 KB Output is correct
22 Correct 9 ms 13060 KB Output is correct
23 Correct 9 ms 13140 KB Output is correct
24 Correct 9 ms 13016 KB Output is correct
25 Correct 7 ms 12768 KB Output is correct
26 Correct 7 ms 12756 KB Output is correct
27 Correct 10 ms 13092 KB Output is correct
28 Correct 10 ms 13040 KB Output is correct
29 Correct 9 ms 13028 KB Output is correct
30 Correct 9 ms 13020 KB Output is correct
31 Correct 10 ms 13140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12756 KB Output is correct
2 Correct 8 ms 12756 KB Output is correct
3 Correct 8 ms 12796 KB Output is correct
4 Correct 377 ms 39444 KB Output is correct
5 Correct 194 ms 29640 KB Output is correct
6 Correct 103 ms 28684 KB Output is correct
7 Correct 121 ms 31060 KB Output is correct
8 Correct 71 ms 29976 KB Output is correct
9 Correct 8 ms 12756 KB Output is correct
10 Correct 8 ms 12756 KB Output is correct
11 Correct 7 ms 12756 KB Output is correct
12 Correct 7 ms 12756 KB Output is correct
13 Correct 8 ms 12792 KB Output is correct
14 Correct 8 ms 12792 KB Output is correct
15 Correct 7 ms 12800 KB Output is correct
16 Correct 8 ms 12756 KB Output is correct
17 Correct 7 ms 12792 KB Output is correct
18 Correct 8 ms 12800 KB Output is correct
19 Correct 8 ms 12756 KB Output is correct
20 Correct 8 ms 12884 KB Output is correct
21 Correct 8 ms 12884 KB Output is correct
22 Correct 8 ms 12884 KB Output is correct
23 Correct 8 ms 12884 KB Output is correct
24 Correct 10 ms 13012 KB Output is correct
25 Correct 9 ms 13012 KB Output is correct
26 Correct 11 ms 13060 KB Output is correct
27 Correct 10 ms 13080 KB Output is correct
28 Correct 9 ms 13016 KB Output is correct
29 Correct 9 ms 13064 KB Output is correct
30 Correct 9 ms 13060 KB Output is correct
31 Correct 9 ms 13140 KB Output is correct
32 Correct 9 ms 13016 KB Output is correct
33 Correct 7 ms 12768 KB Output is correct
34 Correct 7 ms 12756 KB Output is correct
35 Correct 10 ms 13092 KB Output is correct
36 Correct 10 ms 13040 KB Output is correct
37 Correct 9 ms 13028 KB Output is correct
38 Correct 9 ms 13020 KB Output is correct
39 Correct 10 ms 13140 KB Output is correct
40 Correct 419 ms 41040 KB Output is correct
41 Correct 220 ms 31660 KB Output is correct
42 Correct 278 ms 40880 KB Output is correct
43 Correct 254 ms 41404 KB Output is correct
44 Correct 129 ms 30856 KB Output is correct
45 Correct 156 ms 32308 KB Output is correct
46 Correct 74 ms 20272 KB Output is correct
47 Correct 201 ms 36828 KB Output is correct
48 Correct 187 ms 32024 KB Output is correct