#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
const int LOG = 18;
const int N = 1e5 + 5;
int s[N], e[N];
int nx[LOG][N];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<pair<int, int>> ev;
for (int i = 1; i <= n; ++i) {
cin >> s[i] >> e[i];
ev.emplace_back(s[i], ~i);
ev.emplace_back(e[i], i);
}
sort(ev.begin(), ev.end());
set<pair<int, int>> active;
for (int i = 0; i < 2 * n; ++i) {
auto [x, y] = ev[i];
if (y < 0) {
active.emplace(e[~y], ~y);
} else {
active.erase({x, y});
if (!active.empty()) {
auto tmp = *active.rbegin();
nx[0][y] = tmp.second;
}
}
}
for (int j = 1; j < LOG; ++j) {
for (int i = 1; i <= n; ++i) {
nx[j][i] = nx[j - 1][nx[j - 1][i]];
}
}
auto intersect = [&](int i, int j) -> bool {
return s[i] <= s[j] && s[j] <= e[i] && e[i] <= e[j];
};
while (q--) {
int st, en;
cin >> st >> en;
if (st == en) {
cout << 0 << '\n';
continue;
}
if (intersect(st, en)) {
cout << 1 << '\n';
continue;
}
int ans = 0;
for (int j = LOG - 1; j >= 0; --j) {
if (nx[j][st] && e[nx[j][st]] < s[en]) {
st = nx[j][st];
ans += 1 << j;
}
}
bool f = false;
for (int j = 1; j <= n; ++j) {
if (intersect(st, j) && intersect(j, en)) {
cout << ans + 2 << '\n';
f = true;
break;
}
}
if (!f) {
cout << "impossible" << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
572 ms |
10484 KB |
Output is correct |
3 |
Execution timed out |
1560 ms |
10280 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6744 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6744 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6744 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
580 ms |
10552 KB |
Output is correct |
2 |
Execution timed out |
1546 ms |
10336 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
572 ms |
10484 KB |
Output is correct |
3 |
Execution timed out |
1560 ms |
10280 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |