#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
const int N = 2e5+5;
vector<int> side[N];
vector<int> rside[N];
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n;cin>>n;
for(int i=1;i<=n;i++){
int l,r;cin>>l>>r;
for(int j=l;j<=r;j++){
side[j].push_back(i);
rside[i].push_back(j);
}
}
int dis1[n+1]={0};
int disn[n+1]={0};
dis1[1]=1;
disn[n]=1;
queue<int> q;
q.push(1);
while(q.size()){
int a = q.front();
q.pop();
for(auto i : side[a]){
if(dis1[i]) continue;
dis1[i] = dis1[a]+1;
q.push(i);
}
}
queue<int> q2;
q2.push(n);
while(q2.size()){
int a = q2.front();
q2.pop();
for(auto i : side[a]){
if(disn[i]) continue;
disn[i] = disn[a]+1;
q2.push(i);
}
}
int Q;cin>>Q;
int rans[n+1] = {0};
for(int i=1;i<=n;i++) {
if(!dis1[i]) dis1[i] = 1e8;
if(!disn[i]) disn[i] = 1e8;
dis1[i]--;
disn[i]--;
}
while(Q--){
int x;cin>>x;
queue<int> qx;
qx.push(x);
vector<int> d(n+1,0);
d[x]=1;
while(qx.size()){
int a = qx.front();
qx.pop();
for(auto i : rside[a]){
if(d[i]) continue;
d[i]= d[a]+1;
qx.push(i);
}
}
ll ans = 1e9;
for(int i=1;i<=n;i++){
if(d[i]==0) continue;
d[i]--;
//cout<<rans[i]<<" "<<d[i]<<"\n";
ans = min(ans,1LL*max(dis1[i]-1,0)+max(disn[i]-1,0)+d[i]+1);
}
if(ans<=n) cout<<ans<<"\n";
else cout<<-1<<"\n";
}
return 0;
}
Compilation message
passport.cpp: In function 'int main()':
passport.cpp:48:6: warning: unused variable 'rans' [-Wunused-variable]
48 | int rans[n+1] = {0};
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Correct |
2 ms |
9816 KB |
Output is correct |
3 |
Correct |
2 ms |
9828 KB |
Output is correct |
4 |
Execution timed out |
2089 ms |
969788 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Correct |
3 ms |
9824 KB |
Output is correct |
3 |
Correct |
2 ms |
9820 KB |
Output is correct |
4 |
Correct |
2 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9820 KB |
Output is correct |
6 |
Correct |
2 ms |
9816 KB |
Output is correct |
7 |
Correct |
2 ms |
9816 KB |
Output is correct |
8 |
Correct |
2 ms |
9820 KB |
Output is correct |
9 |
Correct |
2 ms |
9816 KB |
Output is correct |
10 |
Correct |
2 ms |
9820 KB |
Output is correct |
11 |
Correct |
3 ms |
10072 KB |
Output is correct |
12 |
Correct |
3 ms |
9820 KB |
Output is correct |
13 |
Correct |
3 ms |
10332 KB |
Output is correct |
14 |
Correct |
3 ms |
10328 KB |
Output is correct |
15 |
Correct |
3 ms |
9816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Correct |
3 ms |
9824 KB |
Output is correct |
3 |
Correct |
2 ms |
9820 KB |
Output is correct |
4 |
Correct |
2 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9820 KB |
Output is correct |
6 |
Correct |
2 ms |
9816 KB |
Output is correct |
7 |
Correct |
2 ms |
9816 KB |
Output is correct |
8 |
Correct |
2 ms |
9820 KB |
Output is correct |
9 |
Correct |
2 ms |
9816 KB |
Output is correct |
10 |
Correct |
2 ms |
9820 KB |
Output is correct |
11 |
Correct |
3 ms |
10072 KB |
Output is correct |
12 |
Correct |
3 ms |
9820 KB |
Output is correct |
13 |
Correct |
3 ms |
10332 KB |
Output is correct |
14 |
Correct |
3 ms |
10328 KB |
Output is correct |
15 |
Correct |
3 ms |
9816 KB |
Output is correct |
16 |
Correct |
22 ms |
25636 KB |
Output is correct |
17 |
Correct |
5 ms |
10332 KB |
Output is correct |
18 |
Correct |
43 ms |
43644 KB |
Output is correct |
19 |
Correct |
41 ms |
41852 KB |
Output is correct |
20 |
Correct |
3 ms |
10072 KB |
Output is correct |
21 |
Correct |
8 ms |
14428 KB |
Output is correct |
22 |
Correct |
42 ms |
50256 KB |
Output is correct |
23 |
Correct |
37 ms |
37200 KB |
Output is correct |
24 |
Correct |
35 ms |
36700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Correct |
3 ms |
9824 KB |
Output is correct |
3 |
Correct |
2 ms |
9820 KB |
Output is correct |
4 |
Correct |
2 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9820 KB |
Output is correct |
6 |
Correct |
2 ms |
9816 KB |
Output is correct |
7 |
Correct |
2 ms |
9816 KB |
Output is correct |
8 |
Correct |
2 ms |
9820 KB |
Output is correct |
9 |
Correct |
2 ms |
9816 KB |
Output is correct |
10 |
Correct |
2 ms |
9820 KB |
Output is correct |
11 |
Correct |
3 ms |
10072 KB |
Output is correct |
12 |
Correct |
3 ms |
9820 KB |
Output is correct |
13 |
Correct |
3 ms |
10332 KB |
Output is correct |
14 |
Correct |
3 ms |
10328 KB |
Output is correct |
15 |
Correct |
3 ms |
9816 KB |
Output is correct |
16 |
Correct |
22 ms |
25636 KB |
Output is correct |
17 |
Correct |
5 ms |
10332 KB |
Output is correct |
18 |
Correct |
43 ms |
43644 KB |
Output is correct |
19 |
Correct |
41 ms |
41852 KB |
Output is correct |
20 |
Correct |
3 ms |
10072 KB |
Output is correct |
21 |
Correct |
8 ms |
14428 KB |
Output is correct |
22 |
Correct |
42 ms |
50256 KB |
Output is correct |
23 |
Correct |
37 ms |
37200 KB |
Output is correct |
24 |
Correct |
35 ms |
36700 KB |
Output is correct |
25 |
Correct |
3 ms |
9816 KB |
Output is correct |
26 |
Correct |
2 ms |
9816 KB |
Output is correct |
27 |
Execution timed out |
2055 ms |
26968 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Correct |
2 ms |
9816 KB |
Output is correct |
3 |
Correct |
2 ms |
9828 KB |
Output is correct |
4 |
Execution timed out |
2089 ms |
969788 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |