#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;
rans[i] = 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;
ans= min(ans,1LL*rans[i]+d[i]-3);
}
if(ans<=n) cout<<ans<<"\n";
else cout<<-1<<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9816 KB |
Output is correct |
2 |
Correct |
3 ms |
9820 KB |
Output is correct |
3 |
Correct |
3 ms |
9820 KB |
Output is correct |
4 |
Execution timed out |
2091 ms |
981208 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9816 KB |
Output is correct |
2 |
Correct |
2 ms |
9816 KB |
Output is correct |
3 |
Correct |
2 ms |
9816 KB |
Output is correct |
4 |
Correct |
3 ms |
9816 KB |
Output is correct |
5 |
Correct |
2 ms |
9816 KB |
Output is correct |
6 |
Correct |
2 ms |
9816 KB |
Output is correct |
7 |
Correct |
2 ms |
9816 KB |
Output is correct |
8 |
Incorrect |
3 ms |
9820 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9816 KB |
Output is correct |
2 |
Correct |
2 ms |
9816 KB |
Output is correct |
3 |
Correct |
2 ms |
9816 KB |
Output is correct |
4 |
Correct |
3 ms |
9816 KB |
Output is correct |
5 |
Correct |
2 ms |
9816 KB |
Output is correct |
6 |
Correct |
2 ms |
9816 KB |
Output is correct |
7 |
Correct |
2 ms |
9816 KB |
Output is correct |
8 |
Incorrect |
3 ms |
9820 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9816 KB |
Output is correct |
2 |
Correct |
2 ms |
9816 KB |
Output is correct |
3 |
Correct |
2 ms |
9816 KB |
Output is correct |
4 |
Correct |
3 ms |
9816 KB |
Output is correct |
5 |
Correct |
2 ms |
9816 KB |
Output is correct |
6 |
Correct |
2 ms |
9816 KB |
Output is correct |
7 |
Correct |
2 ms |
9816 KB |
Output is correct |
8 |
Incorrect |
3 ms |
9820 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9816 KB |
Output is correct |
2 |
Correct |
3 ms |
9820 KB |
Output is correct |
3 |
Correct |
3 ms |
9820 KB |
Output is correct |
4 |
Execution timed out |
2091 ms |
981208 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |