답안 #912142

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
912142 2024-01-19T07:49:27 Z yellowtoad Passport (JOI23_passport) C++17
100 / 100
1363 ms 135704 KB
#include <iostream>
#include <vector>
#include <queue>
#define f first
#define s second
using namespace std;

int n, node[800010], cnt, l, r, test, dist[2][400010], vis[400010], dp[400010], minn[400010], dpp[400010], minnn[400010];
vector<pair<pair<int,int>,pair<int,int>>> edge[400010];
vector<pair<int,int>> rev[400010];
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;

void build(int id, int x, int y) {
	if (x == y) {
		node[id] = x;
		return;
	}
	int mid = (x+y)/2;
	build(id*2,x,mid);
	build(id*2+1,mid+1,y);
	node[id] = ++cnt;
	dpp[cnt] = x;
	dp[cnt] = y;
	edge[node[id*2]].push_back({{cnt,0},{1e9,0}});
	edge[node[id*2+1]].push_back({{cnt,0},{1e9,0}});
}

void update(int id, int x, int y, int from) {
	if ((l <= x) && (y <= r)) {
		edge[node[id]].push_back({{from,1},{l,r}});
		return;
	}
	if ((r < x) || (y < l)) return;
	int mid = (x+y)/2;
	update(id*2,x,mid,from);
	update(id*2+1,mid+1,y,from);
}

void dijk(int from, int id) {
	for (int i = 1; i <= cnt; i++) dist[id][i] = 1e9, vis[i] = 0;
	pq.push({0,from});
	dist[id][from] = 0;
	while (pq.size()) {
		int u = pq.top().s;
		pq.pop();
		if (vis[u]) continue;
		vis[u] = 1;
		for (int i = 0; i < edge[u].size(); i++) {
			if (dist[id][u]+edge[u][i].f.s < dist[id][edge[u][i].f.f]) {
				dist[id][edge[u][i].f.f] = dist[id][u]+edge[u][i].f.s;
				pq.push({dist[id][edge[u][i].f.f],edge[u][i].f.f});
			}
		}
	}
}

void dfs(int u) {
	vis[u] = 1;
	for (int i = 0; i < rev[u].size(); i++) {
		if (!vis[rev[u][i].f]) dfs(rev[u][i].f);
		dp[u] = max(dp[u],max(rev[u][i].s,dp[rev[u][i].f]));
	}
}

void dfss(int u) {
	vis[u] = 1;
	for (int i = 0; i < rev[u].size(); i++) {
		if (!vis[rev[u][i].f]) dfss(rev[u][i].f);
		dpp[u] = min(dpp[u],min(rev[u][i].s,dpp[rev[u][i].f]));
	}
}

int main() {
	cin >> n;
	cnt = n;
	build(1,1,n);
	for (int i = 1; i <= n; i++) {
		cin >> l >> r;
		update(1,1,n,i);
	}
	dijk(1,0);
	dijk(n,1);
	for (int i = 1; i <= cnt; i++) for (int j = 0; j < edge[i].size(); j++) if (dist[0][i]+edge[i][j].f.s == dist[0][edge[i][j].f.f]) rev[edge[i][j].f.f].push_back({i,edge[i][j].s.s});
	for (int i = 1; i <= cnt; i++) vis[i] = 0;
	for (int i = 1; i <= n; i++) dp[i] = i;
	minn[0] = 1e9;
	for (int i = 1; i <= n; i++) minn[i] = min(minn[i-1],dist[1][i]);
	for (int i = 1; i <= cnt; i++) if (!vis[i]) dfs(i);
	for (int i = 1; i <= cnt; i++) rev[i].clear();
	for (int i = 1; i <= cnt; i++) for (int j = 0; j < edge[i].size(); j++) if (dist[1][i]+edge[i][j].f.s == dist[1][edge[i][j].f.f]) rev[edge[i][j].f.f].push_back({i,edge[i][j].s.f});
	for (int i = 1; i <= cnt; i++) vis[i] = 0;
	for (int i = 1; i <= n; i++) dpp[i] = i;
	minnn[n+1] = 1e9;
	for (int i = n; i >= 1; i--) minnn[i] = min(minnn[i+1],dist[0][i]);
	for (int i = 1; i <= cnt; i++) if (!vis[i]) dfss(i);
	/*for (int i = 1; i <= n; i++) cout << dpp[i] << " ";
	cout << endl;*/
	cin >> test;
	while (test--) {
		int u;
		cin >> u;
		if (min(max(dist[0][u],minn[dp[u]]),max(dist[1][u],minnn[dpp[u]])) == 1e9) cout << -1 << endl;
		else cout << min(dist[0][u]+minn[dp[u]],dist[1][u]+minnn[dpp[u]]) << endl;
	}
}
/*
7
1 1
2 4
1 3
4 6
1 5
1 7
7 7
7
1 2 3 4 5 6 7
*/

Compilation message

passport.cpp: In function 'void dijk(int, int)':
passport.cpp:48:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |   for (int i = 0; i < edge[u].size(); i++) {
      |                   ~~^~~~~~~~~~~~~~~~
passport.cpp: In function 'void dfs(int)':
passport.cpp:59:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |  for (int i = 0; i < rev[u].size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~
passport.cpp: In function 'void dfss(int)':
passport.cpp:67:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |  for (int i = 0; i < rev[u].size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~
passport.cpp: In function 'int main()':
passport.cpp:83:51: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |  for (int i = 1; i <= cnt; i++) for (int j = 0; j < edge[i].size(); j++) if (dist[0][i]+edge[i][j].f.s == dist[0][edge[i][j].f.f]) rev[edge[i][j].f.f].push_back({i,edge[i][j].s.s});
      |                                                 ~~^~~~~~~~~~~~~~~~
passport.cpp:90:51: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |  for (int i = 1; i <= cnt; i++) for (int j = 0; j < edge[i].size(); j++) if (dist[1][i]+edge[i][j].f.s == dist[1][edge[i][j].f.f]) rev[edge[i][j].f.f].push_back({i,edge[i][j].s.f});
      |                                                 ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 31324 KB Output is correct
2 Correct 9 ms 31324 KB Output is correct
3 Correct 8 ms 31324 KB Output is correct
4 Correct 1048 ms 127148 KB Output is correct
5 Correct 460 ms 81804 KB Output is correct
6 Correct 299 ms 87824 KB Output is correct
7 Correct 226 ms 63936 KB Output is correct
8 Correct 222 ms 76120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 31576 KB Output is correct
2 Correct 9 ms 31324 KB Output is correct
3 Correct 9 ms 31164 KB Output is correct
4 Correct 8 ms 31324 KB Output is correct
5 Correct 8 ms 31320 KB Output is correct
6 Correct 8 ms 31324 KB Output is correct
7 Correct 8 ms 31324 KB Output is correct
8 Correct 10 ms 31320 KB Output is correct
9 Correct 10 ms 31320 KB Output is correct
10 Correct 9 ms 31320 KB Output is correct
11 Correct 8 ms 31324 KB Output is correct
12 Correct 8 ms 31320 KB Output is correct
13 Correct 9 ms 31324 KB Output is correct
14 Correct 9 ms 31472 KB Output is correct
15 Correct 8 ms 31360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 31576 KB Output is correct
2 Correct 9 ms 31324 KB Output is correct
3 Correct 9 ms 31164 KB Output is correct
4 Correct 8 ms 31324 KB Output is correct
5 Correct 8 ms 31320 KB Output is correct
6 Correct 8 ms 31324 KB Output is correct
7 Correct 8 ms 31324 KB Output is correct
8 Correct 10 ms 31320 KB Output is correct
9 Correct 10 ms 31320 KB Output is correct
10 Correct 9 ms 31320 KB Output is correct
11 Correct 8 ms 31324 KB Output is correct
12 Correct 8 ms 31320 KB Output is correct
13 Correct 9 ms 31324 KB Output is correct
14 Correct 9 ms 31472 KB Output is correct
15 Correct 8 ms 31360 KB Output is correct
16 Correct 12 ms 32088 KB Output is correct
17 Correct 15 ms 31836 KB Output is correct
18 Correct 16 ms 32348 KB Output is correct
19 Correct 12 ms 32092 KB Output is correct
20 Correct 11 ms 31836 KB Output is correct
21 Correct 10 ms 31836 KB Output is correct
22 Correct 10 ms 31652 KB Output is correct
23 Correct 11 ms 31836 KB Output is correct
24 Correct 11 ms 31836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 31576 KB Output is correct
2 Correct 9 ms 31324 KB Output is correct
3 Correct 9 ms 31164 KB Output is correct
4 Correct 8 ms 31324 KB Output is correct
5 Correct 8 ms 31320 KB Output is correct
6 Correct 8 ms 31324 KB Output is correct
7 Correct 8 ms 31324 KB Output is correct
8 Correct 10 ms 31320 KB Output is correct
9 Correct 10 ms 31320 KB Output is correct
10 Correct 9 ms 31320 KB Output is correct
11 Correct 8 ms 31324 KB Output is correct
12 Correct 8 ms 31320 KB Output is correct
13 Correct 9 ms 31324 KB Output is correct
14 Correct 9 ms 31472 KB Output is correct
15 Correct 8 ms 31360 KB Output is correct
16 Correct 12 ms 32088 KB Output is correct
17 Correct 15 ms 31836 KB Output is correct
18 Correct 16 ms 32348 KB Output is correct
19 Correct 12 ms 32092 KB Output is correct
20 Correct 11 ms 31836 KB Output is correct
21 Correct 10 ms 31836 KB Output is correct
22 Correct 10 ms 31652 KB Output is correct
23 Correct 11 ms 31836 KB Output is correct
24 Correct 11 ms 31836 KB Output is correct
25 Correct 9 ms 31324 KB Output is correct
26 Correct 8 ms 31324 KB Output is correct
27 Correct 16 ms 32092 KB Output is correct
28 Correct 15 ms 32004 KB Output is correct
29 Correct 15 ms 32100 KB Output is correct
30 Correct 15 ms 31764 KB Output is correct
31 Correct 15 ms 31836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 31324 KB Output is correct
2 Correct 9 ms 31324 KB Output is correct
3 Correct 8 ms 31324 KB Output is correct
4 Correct 1048 ms 127148 KB Output is correct
5 Correct 460 ms 81804 KB Output is correct
6 Correct 299 ms 87824 KB Output is correct
7 Correct 226 ms 63936 KB Output is correct
8 Correct 222 ms 76120 KB Output is correct
9 Correct 8 ms 31576 KB Output is correct
10 Correct 9 ms 31324 KB Output is correct
11 Correct 9 ms 31164 KB Output is correct
12 Correct 8 ms 31324 KB Output is correct
13 Correct 8 ms 31320 KB Output is correct
14 Correct 8 ms 31324 KB Output is correct
15 Correct 8 ms 31324 KB Output is correct
16 Correct 10 ms 31320 KB Output is correct
17 Correct 10 ms 31320 KB Output is correct
18 Correct 9 ms 31320 KB Output is correct
19 Correct 8 ms 31324 KB Output is correct
20 Correct 8 ms 31320 KB Output is correct
21 Correct 9 ms 31324 KB Output is correct
22 Correct 9 ms 31472 KB Output is correct
23 Correct 8 ms 31360 KB Output is correct
24 Correct 12 ms 32088 KB Output is correct
25 Correct 15 ms 31836 KB Output is correct
26 Correct 16 ms 32348 KB Output is correct
27 Correct 12 ms 32092 KB Output is correct
28 Correct 11 ms 31836 KB Output is correct
29 Correct 10 ms 31836 KB Output is correct
30 Correct 10 ms 31652 KB Output is correct
31 Correct 11 ms 31836 KB Output is correct
32 Correct 11 ms 31836 KB Output is correct
33 Correct 9 ms 31324 KB Output is correct
34 Correct 8 ms 31324 KB Output is correct
35 Correct 16 ms 32092 KB Output is correct
36 Correct 15 ms 32004 KB Output is correct
37 Correct 15 ms 32100 KB Output is correct
38 Correct 15 ms 31764 KB Output is correct
39 Correct 15 ms 31836 KB Output is correct
40 Correct 1363 ms 135704 KB Output is correct
41 Correct 760 ms 84816 KB Output is correct
42 Correct 955 ms 126048 KB Output is correct
43 Correct 884 ms 126064 KB Output is correct
44 Correct 600 ms 90464 KB Output is correct
45 Correct 670 ms 86460 KB Output is correct
46 Correct 323 ms 50320 KB Output is correct
47 Correct 922 ms 91752 KB Output is correct
48 Correct 795 ms 95028 KB Output is correct