#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 ? a.l < b.l : a.r < b.r ;
});
//for (auto[a,b,c] : ev) cout << a << ' ' << b << ' ' << c <<'\n';
vector jmp(20, vector(n, -1));
for (int i = 0; i < n; i++) {
int mxr = -1, mxi = -1;
for (int j =0; j < n; j++) {
if (ev[j].l <= ev[i].r && ev[i].r <= ev[j].r && ev[j].r > mxr) {
mxr = ev[j].r;
mxi = j;
}
}
if (mxr != ev[i].r)
jmp[0][i] = mxi;
}
for (int j = 1; j < 20; j++)
for (int i = 0; i < n;i++)
if (jmp[j-1][i] != -1)
jmp[j][i] = jmp[j-1][jmp[j-1][i]];
while (m--) {
int s, e;
cin >> s >> e;
s--;e--;
if (s == e) {
cout << "0\n";
continue;
}
if (og[e].l <= og[s].r && og[s].r <= og[e].r) {
cout << "1\n";
continue;
}
int si = 0, ei = 0;
for (int j = 0; j < n; j++) {
if (ev[j].i == s) si = j;
if (ev[j].i == e) ei = j;
}
int res = 0;
for (int j = 19; j >= 0; j--) {
if (jmp[j][si] != -1 && ev[jmp[j][si]].r < ev[ei].l) {
si = jmp[j][si];
res += 1<<j;
}
}
int j = jmp[0][si];
if (j != -1 && jmp[0][j] != -1 && ev[ei].l <= ev[jmp[0][j]].r) {
cout << res+2 << '\n';
} else {
cout << "impossible\n";
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1548 ms |
15500 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |