#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
set<int> s;
map<int, int> idx;
map<pair<int, int>, int> toEvent;
vector<pair<int, int>> p;
vector<int> mini;
int point[200001][20];
int getPoint(int y2, int mid) {
for(int i = 0; i < 20; i++) {
if(mid & (1 << i)) {
y2 = point[y2][i];
}
}
return y2;
}
struct SegTree {
int lx, rx;
int val = 1e9, id = -1;
SegTree *ls, *rs;
SegTree(int l, int r) {
lx = l;
rx = r;
if(rx - lx > 1) {
int mid = (lx + rx) / 2;
ls = new SegTree(lx, mid);
rs = new SegTree(mid, rx);
val = min(ls->val, rs->val);
if(val == ls->val) id = ls->id;
if(val == rs->val) id = rs->id;
}
else {
val = mini[lx];
id = lx;
}
}
pair<int, int> getMin(int l, int r) {
if(l <= lx && rx <= r) return {val, id};
if(r <= lx || rx <= l) return {1e9, -1};
return min(ls->getMin(l, r), rs->getMin(l, r));
}
};
vector<int> v;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
int g = 1;
while(g <= 2*n) g *= 2;
p.resize(n+1);
mini.resize(g, 1e9);
for(int i = 0; i < n; i++) {
cin >> p[i].first >> p[i].second;
s.insert(p[i].first);
s.insert(p[i].second);
}
for(int y : s) {
idx[y] = idx.size();
}
for(int i = 0; i < n; i++) {
p[i].first = idx[p[i].first];
p[i].second = idx[p[i].second];
int x, y;
tie(x, y) = p[i];
mini[y] = min(mini[y], x);
toEvent[p[i]] = i;
}
pair<int, int> badP = {1e9, -1};
p[n] = badP;
toEvent[badP] = n;
SegTree st(0, g);
for(int i = 0; i < n; i++) {
point[i][0] = toEvent[st.getMin(p[i].first, p[i].second + 1)];
if(point[i][0] == i) point[i][0] = n;
}
for(int i = 0; i < 20; i++) {
point[n][i] = n;
}
for(int i = 1; i < 20; i++) {
for(int j = 0; j < n; j++) {
point[j][i] = point[point[j][i-1]][i-1];
}
}
while(q--) {
int x, y;
cin >> x >> y;
x--; y--;
if(x == y) {
cout << "0\n";
continue;
}
int l = 0, r = n;
while(l != r - 1) {
int mid = (l + r) / 2;
int y2 = getPoint(y, mid);
if(y2 == n) r = mid;
else if(p[y2].first <= p[x].second) r = mid;
else l = mid;
}
int y2 = getPoint(y, r);
if(y2 == n || r == n) {
cout << "impossible\n";
}
else if(!(p[y2].first <= p[x].second && p[x].second <= p[y2].second)) {
cout << "impossible\n";
}
else if(p[y] == p[x]) {
cout << r << '\n';
}
else {
cout << r+1 << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
406 ms |
50780 KB |
Output is correct |
3 |
Correct |
482 ms |
51148 KB |
Output is correct |
4 |
Incorrect |
489 ms |
50768 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Incorrect |
2 ms |
724 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Incorrect |
2 ms |
724 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Incorrect |
2 ms |
724 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
436 ms |
50848 KB |
Output is correct |
2 |
Correct |
470 ms |
50976 KB |
Output is correct |
3 |
Incorrect |
498 ms |
50764 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
406 ms |
50780 KB |
Output is correct |
3 |
Correct |
482 ms |
51148 KB |
Output is correct |
4 |
Incorrect |
489 ms |
50768 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |