답안 #1026788

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1026788 2024-07-18T11:06:41 Z Unforgettablepl Passport (JOI23_passport) C++17
100 / 100
1888 ms 396424 KB
#pragma GCC optimize("Ofast","unroll-loops")
#include <bits/stdc++.h>
using namespace std;

#define int long long

const int INF = 1e15;

inline int msb(int x){
	for(int bit=30;bit>=0;bit--)if(x&(1<<bit))return bit;
}

int32_t main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	int n;
	cin >> n;
	vector<vector<pair<int,bool>>> adj(18*n+1);
	int S = n;
	vector<vector<int>> lift(n+1,vector<int>(18));
	// Do the binary lift magic
	for(int i=1;i<=n;i++)lift[i][0]=i;
	for(int bit=1;bit<18;bit++){
		for(int i=1;i<=n;i++){
			lift[i][bit]=++S;
			adj[lift[i][bit-1]].emplace_back(S,false);
			adj[lift[max(0ll,i-(1<<(bit-1)))][bit-1]].emplace_back(S,false);
		}
	}
	vector<pair<int,int>> ranges(n+1);
	for(int i=1;i<=n;i++){
		int l,r;cin>>l>>r;
		ranges[i]={l,r};
		int tar = r-l+1;
		int bit = msb(tar);
		adj[lift[r][bit]].emplace_back(i,true);
		adj[lift[l+(1<<bit)-1][bit]].emplace_back(i,true);
	}
	vector<int> distL(S+1,INF);
	vector<int> distR(S+1,INF);
	{
		// distL calculation
		deque<pair<int,int>> q;
		for(int i=1;i<=n;i++){
			if(ranges[i].first==1)q.emplace_back(i,0);
		}
		vector<bool> visited(S+1);
		while(!q.empty()){
			auto [idx,dist] = q.front();q.pop_front();
			if(visited[idx])continue;
			visited[idx]=true;
			distL[idx]=dist;
			for(auto&[i,type]:adj[idx])if(!visited[i])
				if(type)q.emplace_back(i,dist+1);
				else q.emplace_front(i,dist);
		}
	}
	{
		// distR calculation
		deque<pair<int,int>> q;
		for(int i=1;i<=n;i++){
			if(ranges[i].second==n)q.emplace_back(i,0);
		}
		vector<bool> visited(S+1);
		while(!q.empty()){
			auto [idx,dist] = q.front();q.pop_front();
			if(visited[idx])continue;
			visited[idx]=true;
			distR[idx]=dist;
			for(auto&[i,type]:adj[idx])if(!visited[i])
				if(type)q.emplace_back(i,dist+1);
				else q.emplace_front(i,dist);
		}
	}
	vector<int> totdist(S+1,INF);
	{
		// totdist calculation
		vector<bool> visited(S+1);
		priority_queue<pair<int,int>> q;
		for(int i=1;i<=n;i++){
			q.emplace(-(distL[i]+distR[i]+1),i);
		}
		while(!q.empty()){
			auto [dist,idx] = q.top();q.pop();dist=-dist;
			if(visited[idx])continue;
			visited[idx]=true;
			totdist[idx]=dist;
			for(auto&[i,type]:adj[idx])if(!visited[i])
				if(type)q.emplace(-dist-1,i);
				else q.emplace(-dist,i);
		}
	}
	int q;
	cin >> q;
	for(int i=1;i<=q;i++){
		int x;cin>>x;
		if(totdist[x]>=INF)cout<<"-1\n";
		else cout << totdist[x] << '\n';
	}
}

Compilation message

passport.cpp: In function 'int32_t main()':
passport.cpp:53:33: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
   53 |    for(auto&[i,type]:adj[idx])if(!visited[i])
      |                                 ^
passport.cpp:70:33: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
   70 |    for(auto&[i,type]:adj[idx])if(!visited[i])
      |                                 ^
passport.cpp:88:33: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
   88 |    for(auto&[i,type]:adj[idx])if(!visited[i])
      |                                 ^
passport.cpp: In function 'long long int msb(long long int)':
passport.cpp:11:1: warning: control reaches end of non-void function [-Wreturn-type]
   11 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1888 ms 394260 KB Output is correct
5 Correct 1605 ms 386116 KB Output is correct
6 Correct 1048 ms 381740 KB Output is correct
7 Correct 1084 ms 379708 KB Output is correct
8 Correct 790 ms 307592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 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 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 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 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 12 ms 5080 KB Output is correct
17 Correct 10 ms 5176 KB Output is correct
18 Correct 13 ms 5076 KB Output is correct
19 Correct 12 ms 5080 KB Output is correct
20 Correct 13 ms 5076 KB Output is correct
21 Correct 10 ms 5292 KB Output is correct
22 Correct 12 ms 5328 KB Output is correct
23 Correct 11 ms 5456 KB Output is correct
24 Correct 9 ms 5148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 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 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 12 ms 5080 KB Output is correct
17 Correct 10 ms 5176 KB Output is correct
18 Correct 13 ms 5076 KB Output is correct
19 Correct 12 ms 5080 KB Output is correct
20 Correct 13 ms 5076 KB Output is correct
21 Correct 10 ms 5292 KB Output is correct
22 Correct 12 ms 5328 KB Output is correct
23 Correct 11 ms 5456 KB Output is correct
24 Correct 9 ms 5148 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 11 ms 5336 KB Output is correct
28 Correct 10 ms 5404 KB Output is correct
29 Correct 10 ms 5108 KB Output is correct
30 Correct 9 ms 5076 KB Output is correct
31 Correct 11 ms 5080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1888 ms 394260 KB Output is correct
5 Correct 1605 ms 386116 KB Output is correct
6 Correct 1048 ms 381740 KB Output is correct
7 Correct 1084 ms 379708 KB Output is correct
8 Correct 790 ms 307592 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 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 860 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
22 Correct 1 ms 860 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 12 ms 5080 KB Output is correct
25 Correct 10 ms 5176 KB Output is correct
26 Correct 13 ms 5076 KB Output is correct
27 Correct 12 ms 5080 KB Output is correct
28 Correct 13 ms 5076 KB Output is correct
29 Correct 10 ms 5292 KB Output is correct
30 Correct 12 ms 5328 KB Output is correct
31 Correct 11 ms 5456 KB Output is correct
32 Correct 9 ms 5148 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 11 ms 5336 KB Output is correct
36 Correct 10 ms 5404 KB Output is correct
37 Correct 10 ms 5108 KB Output is correct
38 Correct 9 ms 5076 KB Output is correct
39 Correct 11 ms 5080 KB Output is correct
40 Correct 1839 ms 396424 KB Output is correct
41 Correct 1534 ms 389676 KB Output is correct
42 Correct 1543 ms 385612 KB Output is correct
43 Correct 1613 ms 389936 KB Output is correct
44 Correct 1161 ms 382180 KB Output is correct
45 Correct 1380 ms 384464 KB Output is correct
46 Correct 538 ms 147556 KB Output is correct
47 Correct 1456 ms 394852 KB Output is correct
48 Correct 1395 ms 390592 KB Output is correct