#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
const int M = 1e4+2;
struct rng {int l, r, i;};
signed main() {
cin.tie(0)->sync_with_stdio(0);
int n, q;
cin >> n >> q;
vector<rng> v(n);
for (auto &[l, r, i]:v) cin >> l >> r;
for (int i = 0; i < n; i++) v[i].i = i;
bitset<100005> vis;
while (q--) {
int a, b;
cin >> a >> b; a--; b--;
if (a == b) {
cout << 0 << endl;
continue;
}
vis[b] = 1;
int ans = 0;
rng cr = v[b];
while (true) {
// cout << q << "I: "<< cr.i+1 << endl;
rng tmp;
bool ch = 0;
int mn = INT_MAX;
for (auto [l, r, i]:v) {
if (!vis[i]) {
if (cr.l <= r && r <= cr.r) {
if (i == a) {
ans++; ch = 0;
break;
}
if (l < mn) {
ch = true;
tmp = {l, r, i};
mn = l;
}
}
}
}
if (!ch) break;
if (tmp.l < cr.l) cr = tmp;
else break;
ans++;
vis[cr.i] = 1;
} vis = 0;
if (cr.l <= v[a].r && v[a].r <= cr.r) {
cout << ans << endl;
} else {
cout << "impossible" << endl;
}
}
return 0;
}
/*
8 5
1 2
3 4
1 5
6 7
5 10
10 20
15 20
999999999 1000000000
1 6
1 7
2 4
3 3
5 8
5 2
1 3
2 4
4 7
7 9
3 7
1 4
3 2
*/
Compilation message
events.cpp: In function 'int main()':
events.cpp:59:28: warning: 'tmp.rng::r' may be used uninitialized in this function [-Wmaybe-uninitialized]
59 | if (cr.l <= v[a].r && v[a].r <= cr.r) {
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
events.cpp:32:17: warning: 'tmp.rng::i' may be used uninitialized in this function [-Wmaybe-uninitialized]
32 | rng tmp;
| ^~~
events.cpp:53:13: warning: 'tmp.rng::l' may be used uninitialized in this function [-Wmaybe-uninitialized]
53 | if (tmp.l < cr.l) cr = tmp;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1579 ms |
1492 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
161 ms |
340 KB |
Output is correct |
4 |
Correct |
35 ms |
340 KB |
Output is correct |
5 |
Correct |
60 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
161 ms |
340 KB |
Output is correct |
4 |
Correct |
35 ms |
340 KB |
Output is correct |
5 |
Correct |
60 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
168 ms |
340 KB |
Output is correct |
13 |
Correct |
34 ms |
340 KB |
Output is correct |
14 |
Correct |
53 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Execution timed out |
1577 ms |
340 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
161 ms |
340 KB |
Output is correct |
4 |
Correct |
35 ms |
340 KB |
Output is correct |
5 |
Correct |
60 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
169 ms |
340 KB |
Output is correct |
13 |
Correct |
37 ms |
344 KB |
Output is correct |
14 |
Correct |
51 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Execution timed out |
1585 ms |
1492 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1552 ms |
1492 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1579 ms |
1492 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |