#include <bits/stdc++.h>
// #define MULTI_TEST_CASE
// #define TEST_TEXT
using namespace std;
#define ll long long
#define MAX(a, b) (a) = max((a), (b))
#define MIN(a, b) (a) = min((a), (b))
#define all(a) (a).begin(), (a).end()
#define sortedpair(a, b) {min((a), (b)), max((a), (b))}
const ll MOD = 1e9+7;
void solve(){
int n, q; cin>>n>>q;
vector<pair<int, int>> v(n);
for(auto&i:v)cin>>i.first>>i.second;
vector<vector<int>> g(n, vector<int>());
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(i==j)continue;
if(v[j].first <= v[i].second && v[i].second <= v[j].second){
g[i].push_back(j);
}
}
}
vector<vector<int>> d(n, vector<int>(n, 1e9));
for(int i = 0; i < n; i++){
d[i][i] = 0;
queue<int> qu;
qu.push(i);
while(!qu.empty()){
int c = qu.front();
qu.pop();
for(int j : g[c]){
if(d[i][c] + 1 < d[i][j]){
d[i][j] = d[i][c] + 1;
qu.push(j);
}
}
}
}
while(q--){
int a, b; cin>>a>>b; a--; b--;
if(d[a][b] == 1e9)cout<<"impossible"<<endl;
else cout<<d[a][b]<<endl;
}
}
// struct event{
// int start, end, ind;
// event(){}
// bool operator<(const event&o){
// return end < o.end;
// }
// };
// int n, q;
// vector<event> v;
// vector<int> p;
// void solve(){
// cin>>n>>q;
// v.resize(n);
// for(int i = 0; i < n; i++){
// cin>>v[i].start>>v[i].end;
// v[i].ind = i;
// }
// sort(all(v));
// vector<int> hol(n);
// for(int i = 0; i < n; i++){
// hol[v[i].ind] = i;
// }
// vector<int> megszakit;
// for(int i = 0; i < n-1; i++){
// if(v[i].end < v[i+1].start){
// megszakit.push_back(i);
// }
// }
// megszakit.push_back(1e9);
// while(q--){
// int a, b; cin>>a>>b; a--; b--;
// if(v[hol[b]].end < v[hol[a]].end){
// cout<<"impossible"<<endl;
// continue;
// }
// if(v[hol[b]].end == v[hol[a]].end){
// cout<<1<<endl;
// continue;
// }
// if(*lower_bound(megszakit.begin(), megszakit.end(), hol[a]) < hol[b]){
// cout<<"impossible"<<endl;
// continue;
// }
// cout<<hol[b]-hol[a]<<endl;
// }
// }
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int _t = 1;
#ifdef MULTI_TEST_CASE
cin >> _t;
#endif
for(int _i = 0; _i < _t; _i++){
#ifdef TEST_TEXT
cout<<"Case #"<<_i+1<<": ";
#endif
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1576 ms |
3656 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
16 ms |
4308 KB |
Output is correct |
4 |
Correct |
12 ms |
4212 KB |
Output is correct |
5 |
Correct |
16 ms |
4308 KB |
Output is correct |
6 |
Correct |
61 ms |
5076 KB |
Output is correct |
7 |
Correct |
187 ms |
5908 KB |
Output is correct |
8 |
Correct |
267 ms |
6964 KB |
Output is correct |
9 |
Correct |
1337 ms |
8320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
16 ms |
4308 KB |
Output is correct |
4 |
Correct |
12 ms |
4212 KB |
Output is correct |
5 |
Correct |
16 ms |
4308 KB |
Output is correct |
6 |
Correct |
61 ms |
5076 KB |
Output is correct |
7 |
Correct |
187 ms |
5908 KB |
Output is correct |
8 |
Correct |
267 ms |
6964 KB |
Output is correct |
9 |
Correct |
1337 ms |
8320 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
15 ms |
4340 KB |
Output is correct |
13 |
Correct |
10 ms |
4308 KB |
Output is correct |
14 |
Correct |
16 ms |
4344 KB |
Output is correct |
15 |
Correct |
65 ms |
5096 KB |
Output is correct |
16 |
Correct |
188 ms |
5904 KB |
Output is correct |
17 |
Correct |
252 ms |
6972 KB |
Output is correct |
18 |
Correct |
1353 ms |
8312 KB |
Output is correct |
19 |
Execution timed out |
1587 ms |
128368 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
16 ms |
4308 KB |
Output is correct |
4 |
Correct |
12 ms |
4212 KB |
Output is correct |
5 |
Correct |
16 ms |
4308 KB |
Output is correct |
6 |
Correct |
61 ms |
5076 KB |
Output is correct |
7 |
Correct |
187 ms |
5908 KB |
Output is correct |
8 |
Correct |
267 ms |
6964 KB |
Output is correct |
9 |
Correct |
1337 ms |
8320 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
18 ms |
4308 KB |
Output is correct |
13 |
Correct |
14 ms |
4220 KB |
Output is correct |
14 |
Correct |
19 ms |
4308 KB |
Output is correct |
15 |
Correct |
75 ms |
5020 KB |
Output is correct |
16 |
Correct |
206 ms |
5908 KB |
Output is correct |
17 |
Correct |
290 ms |
6968 KB |
Output is correct |
18 |
Correct |
1498 ms |
8320 KB |
Output is correct |
19 |
Execution timed out |
1550 ms |
3612 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1567 ms |
3528 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1576 ms |
3656 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |