#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e15;
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<vector<pair<int,bool>>> adj(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.emplace_back();
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;
for(int bit=0;bit<18;bit++)if(tar&(1<<bit)){
adj[lift[r][bit]].emplace_back(i,true);
r-=(1<<bit);
}
}
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:50:33: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
50 | for(auto&[i,type]:adj[idx])if(!visited[i])
| ^
passport.cpp:67:33: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
67 | for(auto&[i,type]:adj[idx])if(!visited[i])
| ^
passport.cpp:85:33: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
85 | for(auto&[i,type]:adj[idx])if(!visited[i])
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
2081 ms |
462960 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 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 |
1024 KB |
Output is correct |
12 |
Correct |
2 ms |
856 KB |
Output is correct |
13 |
Correct |
2 ms |
856 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 |
348 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 |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 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 |
1024 KB |
Output is correct |
12 |
Correct |
2 ms |
856 KB |
Output is correct |
13 |
Correct |
2 ms |
856 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 |
5464 KB |
Output is correct |
17 |
Correct |
10 ms |
5364 KB |
Output is correct |
18 |
Correct |
14 ms |
5428 KB |
Output is correct |
19 |
Correct |
12 ms |
5396 KB |
Output is correct |
20 |
Correct |
10 ms |
5356 KB |
Output is correct |
21 |
Correct |
10 ms |
5484 KB |
Output is correct |
22 |
Correct |
10 ms |
5612 KB |
Output is correct |
23 |
Correct |
12 ms |
5708 KB |
Output is correct |
24 |
Correct |
10 ms |
5324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 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 |
1024 KB |
Output is correct |
12 |
Correct |
2 ms |
856 KB |
Output is correct |
13 |
Correct |
2 ms |
856 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 |
5464 KB |
Output is correct |
17 |
Correct |
10 ms |
5364 KB |
Output is correct |
18 |
Correct |
14 ms |
5428 KB |
Output is correct |
19 |
Correct |
12 ms |
5396 KB |
Output is correct |
20 |
Correct |
10 ms |
5356 KB |
Output is correct |
21 |
Correct |
10 ms |
5484 KB |
Output is correct |
22 |
Correct |
10 ms |
5612 KB |
Output is correct |
23 |
Correct |
12 ms |
5708 KB |
Output is correct |
24 |
Correct |
10 ms |
5324 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
14 ms |
5872 KB |
Output is correct |
28 |
Correct |
11 ms |
5356 KB |
Output is correct |
29 |
Correct |
10 ms |
5324 KB |
Output is correct |
30 |
Correct |
9 ms |
5356 KB |
Output is correct |
31 |
Correct |
10 ms |
5408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
2081 ms |
462960 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |