#include <bits/stdc++.h>
#define int long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);
int n;
cin>>n;
vector <int> l(n+1),r(n+1),mx(n+1);
for(int i=1;i<=n;i++){
cin>>l[i]>>r[i];
mx[i]=max(mx[i-1],r[i]);
}
int query;
cin>>query;
while(query--){
int s;
cin>>s;
if(s==1){
int x=r[s];
int ans=1;
while(x!=n){
if(mx[x]==x){
ans=-1;
break;
}
ans++;
x=mx[x];
}
cout<<ans<<"\n";
continue;
}
queue < pair <int,int> > q;
q.push({l[s],r[s]});
vector <vector <int> > dis(n+1,vector <int> (n+1,1e18));
dis[l[s]][r[s]]=1;
while(!q.empty()){
int left=q.front().ff;
int right=q.front().ss;
q.pop();
for(int i=left;i<=right;i++){
if(dis[min(left,l[i])][max(right,r[i])]>dis[left][right]+1){
dis[min(left,l[i])][max(right,r[i])]=dis[left][right]+1;
q.push({min(left,l[i]),max(right,r[i])});
}
}
}
if(dis[1][n]==1e18)cout<<-1<<"\n";
else cout<<dis[1][n]<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
74 ms |
7452 KB |
Output is correct |
5 |
Correct |
75 ms |
7360 KB |
Output is correct |
6 |
Correct |
81 ms |
7664 KB |
Output is correct |
7 |
Correct |
60 ms |
6720 KB |
Output is correct |
8 |
Correct |
53 ms |
5716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
2 ms |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
3 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
2 ms |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
3 ms |
1116 KB |
Output is correct |
16 |
Correct |
1996 ms |
47008 KB |
Output is correct |
17 |
Correct |
839 ms |
49004 KB |
Output is correct |
18 |
Correct |
31 ms |
49496 KB |
Output is correct |
19 |
Correct |
41 ms |
46944 KB |
Output is correct |
20 |
Execution timed out |
2060 ms |
49500 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
2 ms |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
3 ms |
1116 KB |
Output is correct |
16 |
Correct |
1996 ms |
47008 KB |
Output is correct |
17 |
Correct |
839 ms |
49004 KB |
Output is correct |
18 |
Correct |
31 ms |
49496 KB |
Output is correct |
19 |
Correct |
41 ms |
46944 KB |
Output is correct |
20 |
Execution timed out |
2060 ms |
49500 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
74 ms |
7452 KB |
Output is correct |
5 |
Correct |
75 ms |
7360 KB |
Output is correct |
6 |
Correct |
81 ms |
7664 KB |
Output is correct |
7 |
Correct |
60 ms |
6720 KB |
Output is correct |
8 |
Correct |
53 ms |
5716 KB |
Output is correct |
9 |
Correct |
1 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 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 |
2 ms |
1100 KB |
Output is correct |
20 |
Correct |
2 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
3 ms |
1116 KB |
Output is correct |
24 |
Correct |
1996 ms |
47008 KB |
Output is correct |
25 |
Correct |
839 ms |
49004 KB |
Output is correct |
26 |
Correct |
31 ms |
49496 KB |
Output is correct |
27 |
Correct |
41 ms |
46944 KB |
Output is correct |
28 |
Execution timed out |
2060 ms |
49500 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |