#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 1e5;
struct event {
int l, r, i;
};
int main() {
int n, m;
cin >> n >> m;
vector<event> ev(n), og;
map<int,int> comp;
int _i = 0;
for (auto& [s, e, i] : ev) {
cin >> s >> e;
comp[s] = comp[e] = 0;
i = _i++;
}
int k = 0;
for (auto& mp : comp)
mp.second = k++;
for (auto& [s, e, i] : ev)
s = comp[s], e = comp[e];
og = ev;
sort(ev.begin(), ev.end(), [&](auto& a, auto& b) {
return a.r < b.r;
});
while (m--) {
int s, e;
cin >> s >> e;
s--;e--;
if (s == e) {
cout << "0\n";
continue;
}
int si = 0, ei;
for (int j = 0; j < n; j++) {
if (ev[j].i == s) si = j;
if (ev[j].i == e) ei = j;
}
vector memo(n+1, -1);
auto dp = [&](auto& dp, int i)->int{
int t = ev[i].r;
if (i == ei) return 0;
if (t > ev[ei].r) return inf;
if (memo[i]!=-1) return memo[i];
int res = inf;
for (int j = i+1; j < n; j++) {
if (ev[j].l <= t && t <= ev[j].r)
res = min(res, 1 + dp(dp, j));
}
return memo[i] = res;
};
int res = dp(dp, si);
if (res < inf) cout << res << '\n';
else cout << "impossible\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1540 ms |
12336 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
58 ms |
340 KB |
Output is correct |
4 |
Correct |
11 ms |
340 KB |
Output is correct |
5 |
Correct |
13 ms |
340 KB |
Output is correct |
6 |
Incorrect |
13 ms |
300 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
58 ms |
340 KB |
Output is correct |
4 |
Correct |
11 ms |
340 KB |
Output is correct |
5 |
Correct |
13 ms |
340 KB |
Output is correct |
6 |
Incorrect |
13 ms |
300 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
58 ms |
340 KB |
Output is correct |
4 |
Correct |
11 ms |
340 KB |
Output is correct |
5 |
Correct |
13 ms |
340 KB |
Output is correct |
6 |
Incorrect |
13 ms |
300 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1507 ms |
12304 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1540 ms |
12336 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |