/*
author : abushbandit
contest : ---
*/
#include "bits/stdc++.h"
using namespace std;
void solve(int tc){
int n;
cin >> n;
vector<pair<int,int>> a(n + 1);
for(int i = 1;i <= n;i++){
int l,r;
cin >> l >> r;
a[i] = {l,r};
}
int q;
cin >> q;
for(int i = 0;i < q;i++){
int x;
cin >> x;
if(x == 1){
int i = 1,lst = a[1].second,cnt = 0;
bool check = 0;
while(true){
cnt++;
int mx = 0;
while(i <= n && i <= lst){
mx = max(mx,a[i].second);
i++;
}
if(i > n){
break;
} else if(lst < mx){
lst = mx;
} else{
check = 1;
break;
}
}
if(check){
cout << -1 << "\n";
} else{
cout << cnt << "\n";
}
break;
} else{
vector<vector<int>> dis(n + 1, vector<int> (n + 1,1e18));
vector<vector<int>> vis(n + 1, vector<int> (n + 1,0));
set<vector<int>> st;
dis[a[x].first][a[x].second] = 1;
st.insert({1,a[x].first,a[x].second});
while(!st.empty()){
int l = (*st.begin())[1], r = (*st.begin())[2];
st.erase(st.begin());
if(vis[l][r])continue;
vis[l][r] = 1;
for(int i = l;i <= r;i++){
for(int j = r;j <= max(r,a[i].second);j++){
for(int k = l;k >= min(l,a[i].first);k--){
if(dis[l][r] + 1 < dis[k][j]){
st.insert({dis[l][r] + 1,k,j});
dis[k][j] = dis[l][r] + 1;
}
}
}
}
}
if(dis[1][n] == 1e18){
cout << "-1\n";
} else{
cout << dis[1][n] << "\n";
}
}
}
}
signed main(){
// start_file("");
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int test_cases = 1;
// cin >> test_cases ;
for(int tc = 1;tc <= test_cases;++ tc){
solve(tc);
}
}
/*
*/
Compilation message
passport.cpp: In function 'void solve(int)':
passport.cpp:59:54: warning: overflow in conversion from 'double' to 'std::vector<int>::value_type' {aka 'int'} changes value from '1.0e+18' to '2147483647' [-Woverflow]
59 | vector<vector<int>> dis(n + 1, vector<int> (n + 1,1e18));
| ^~~~
# |
Verdict |
Execution time |
Memory |
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 |
23 ms |
1884 KB |
Output is correct |
5 |
Correct |
23 ms |
1884 KB |
Output is correct |
6 |
Correct |
24 ms |
1884 KB |
Output is correct |
7 |
Correct |
19 ms |
1884 KB |
Output is correct |
8 |
Correct |
17 ms |
1628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
600 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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
600 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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
600 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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
23 ms |
1884 KB |
Output is correct |
5 |
Correct |
23 ms |
1884 KB |
Output is correct |
6 |
Correct |
24 ms |
1884 KB |
Output is correct |
7 |
Correct |
19 ms |
1884 KB |
Output is correct |
8 |
Correct |
17 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
600 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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |