#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v) (int)v.size()
#define x first
#define y second
#define nl "\n"
using namespace std;
const int N = (int)1e5 + 7;
const int M = (int)2e6 + 5e5 + 7;
const ll MOD = (ll)1e9;
const int inf = (int)1e9 + 7;
const int B = (int)390;
const ll INF = (ll)1e18 + 7;
pair<int, int> dir[] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
bool bit(int mask, int i) {
return (mask >> i & 1);
}
int sum(int x, int y) {
x += y;
if(x >= MOD) x -= MOD;
return x;
}
int mult(int x, int y) {
return 1ll * x * y % MOD;
}
int n, q;
int l[N], r[N], d[N];
vector<int> g[N];
void solve() {
cin >> n >> q;
for(int i = 1; i <= n; ++i) {
cin >> l[i] >> r[i];
}
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j) {
if(l[j] <= r[i] && r[i] <= r[j]) {
g[i].pb(j);
}
}
}
while(q--) {
int s, t;
cin >> s >> t;
queue<int> q;
fill(d + 1, d + 1 + n, inf);
d[s] = 0;
q.push(s);
while(!q.empty()) {
int v = q.front();
q.pop();
for(auto to : g[v]) {
if(d[v] + 1 < d[to]) {
d[to] = d[v] + 1;
q.push(to);
}
}
}
if(d[t] == inf) cout << "impossible\n";
else cout << d[t] << nl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int test = 1;
//cin >> test;
for(int i = 1; i <= test; ++i) {
//cout << "Case " << i << ":\n";
solve();
}
return 0;
}
/*
possible causes of error :
* array bounds
* int overflow
* special case (n == 1?)
* haven't clean something
* wrong N | M | inf | INF | MOD
* try to write brute force to find test
* don't waste time
* don't write bullshit
* solution is easy
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2644 KB |
Output is correct |
2 |
Execution timed out |
1560 ms |
5660 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2676 KB |
Output is correct |
3 |
Correct |
8 ms |
2644 KB |
Output is correct |
4 |
Correct |
6 ms |
2724 KB |
Output is correct |
5 |
Correct |
7 ms |
2676 KB |
Output is correct |
6 |
Correct |
11 ms |
3412 KB |
Output is correct |
7 |
Correct |
18 ms |
4308 KB |
Output is correct |
8 |
Correct |
19 ms |
5376 KB |
Output is correct |
9 |
Correct |
76 ms |
6612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2676 KB |
Output is correct |
3 |
Correct |
8 ms |
2644 KB |
Output is correct |
4 |
Correct |
6 ms |
2724 KB |
Output is correct |
5 |
Correct |
7 ms |
2676 KB |
Output is correct |
6 |
Correct |
11 ms |
3412 KB |
Output is correct |
7 |
Correct |
18 ms |
4308 KB |
Output is correct |
8 |
Correct |
19 ms |
5376 KB |
Output is correct |
9 |
Correct |
76 ms |
6612 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2676 KB |
Output is correct |
12 |
Correct |
9 ms |
2644 KB |
Output is correct |
13 |
Correct |
7 ms |
2684 KB |
Output is correct |
14 |
Correct |
7 ms |
2684 KB |
Output is correct |
15 |
Correct |
12 ms |
3424 KB |
Output is correct |
16 |
Correct |
18 ms |
4244 KB |
Output is correct |
17 |
Correct |
20 ms |
5332 KB |
Output is correct |
18 |
Correct |
81 ms |
6724 KB |
Output is correct |
19 |
Execution timed out |
1591 ms |
32876 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2676 KB |
Output is correct |
3 |
Correct |
8 ms |
2644 KB |
Output is correct |
4 |
Correct |
6 ms |
2724 KB |
Output is correct |
5 |
Correct |
7 ms |
2676 KB |
Output is correct |
6 |
Correct |
11 ms |
3412 KB |
Output is correct |
7 |
Correct |
18 ms |
4308 KB |
Output is correct |
8 |
Correct |
19 ms |
5376 KB |
Output is correct |
9 |
Correct |
76 ms |
6612 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
8 ms |
2644 KB |
Output is correct |
13 |
Correct |
7 ms |
2684 KB |
Output is correct |
14 |
Correct |
7 ms |
2684 KB |
Output is correct |
15 |
Correct |
10 ms |
3496 KB |
Output is correct |
16 |
Correct |
19 ms |
4320 KB |
Output is correct |
17 |
Correct |
20 ms |
5400 KB |
Output is correct |
18 |
Correct |
89 ms |
6656 KB |
Output is correct |
19 |
Execution timed out |
1570 ms |
5484 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1541 ms |
5616 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2644 KB |
Output is correct |
2 |
Execution timed out |
1560 ms |
5660 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |