#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;
vector<int> mo;
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);
mo.resize(n);
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;
}
for(int i = 0; i < n; i++) {
mo[i] = toEvent[p[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;
}
else if(p[x] == p[y]) {
cout << "1\n";
continue;
}
x = mo[x]; y = mo[y];
int l = -1, r = n;
while(l != r - 1) {
int mid = (l + r + 1) / 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 {
cout << r+1 << '\n';
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
471 ms |
51296 KB |
Output is correct |
3 |
Correct |
536 ms |
51316 KB |
Output is correct |
4 |
Correct |
479 ms |
51136 KB |
Output is correct |
5 |
Correct |
503 ms |
52188 KB |
Output is correct |
6 |
Correct |
488 ms |
53084 KB |
Output is correct |
7 |
Correct |
474 ms |
53504 KB |
Output is correct |
8 |
Correct |
499 ms |
61048 KB |
Output is correct |
9 |
Correct |
479 ms |
61068 KB |
Output is correct |
10 |
Correct |
737 ms |
51568 KB |
Output is correct |
11 |
Correct |
574 ms |
55368 KB |
Output is correct |
12 |
Correct |
219 ms |
48576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Correct |
2 ms |
724 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Correct |
2 ms |
724 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
660 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
2 ms |
724 KB |
Output is correct |
15 |
Correct |
3 ms |
724 KB |
Output is correct |
16 |
Correct |
3 ms |
852 KB |
Output is correct |
17 |
Correct |
2 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
80 ms |
3764 KB |
Output is correct |
20 |
Correct |
70 ms |
3628 KB |
Output is correct |
21 |
Correct |
99 ms |
3936 KB |
Output is correct |
22 |
Correct |
68 ms |
3880 KB |
Output is correct |
23 |
Correct |
81 ms |
4172 KB |
Output is correct |
24 |
Correct |
89 ms |
4352 KB |
Output is correct |
25 |
Correct |
70 ms |
3972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Correct |
2 ms |
724 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
724 KB |
Output is correct |
13 |
Correct |
3 ms |
724 KB |
Output is correct |
14 |
Correct |
2 ms |
724 KB |
Output is correct |
15 |
Correct |
2 ms |
724 KB |
Output is correct |
16 |
Correct |
2 ms |
852 KB |
Output is correct |
17 |
Correct |
2 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
341 ms |
50972 KB |
Output is correct |
20 |
Correct |
380 ms |
50888 KB |
Output is correct |
21 |
Correct |
320 ms |
48816 KB |
Output is correct |
22 |
Correct |
412 ms |
54204 KB |
Output is correct |
23 |
Correct |
494 ms |
60336 KB |
Output is correct |
24 |
Correct |
496 ms |
60012 KB |
Output is correct |
25 |
Correct |
77 ms |
34984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
449 ms |
51216 KB |
Output is correct |
2 |
Correct |
486 ms |
51192 KB |
Output is correct |
3 |
Correct |
590 ms |
51276 KB |
Output is correct |
4 |
Correct |
498 ms |
61152 KB |
Output is correct |
5 |
Correct |
661 ms |
51504 KB |
Output is correct |
6 |
Correct |
785 ms |
60748 KB |
Output is correct |
7 |
Correct |
744 ms |
60756 KB |
Output is correct |
8 |
Correct |
663 ms |
60872 KB |
Output is correct |
9 |
Correct |
439 ms |
59940 KB |
Output is correct |
10 |
Correct |
612 ms |
60408 KB |
Output is correct |
11 |
Correct |
611 ms |
60196 KB |
Output is correct |
12 |
Correct |
591 ms |
60480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
471 ms |
51296 KB |
Output is correct |
3 |
Correct |
536 ms |
51316 KB |
Output is correct |
4 |
Correct |
479 ms |
51136 KB |
Output is correct |
5 |
Correct |
503 ms |
52188 KB |
Output is correct |
6 |
Correct |
488 ms |
53084 KB |
Output is correct |
7 |
Correct |
474 ms |
53504 KB |
Output is correct |
8 |
Correct |
499 ms |
61048 KB |
Output is correct |
9 |
Correct |
479 ms |
61068 KB |
Output is correct |
10 |
Correct |
737 ms |
51568 KB |
Output is correct |
11 |
Correct |
574 ms |
55368 KB |
Output is correct |
12 |
Correct |
219 ms |
48576 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
724 KB |
Output is correct |
16 |
Correct |
2 ms |
724 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
724 KB |
Output is correct |
19 |
Correct |
2 ms |
852 KB |
Output is correct |
20 |
Correct |
2 ms |
852 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
2 ms |
660 KB |
Output is correct |
25 |
Correct |
2 ms |
724 KB |
Output is correct |
26 |
Correct |
2 ms |
724 KB |
Output is correct |
27 |
Correct |
3 ms |
724 KB |
Output is correct |
28 |
Correct |
3 ms |
852 KB |
Output is correct |
29 |
Correct |
2 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
80 ms |
3764 KB |
Output is correct |
32 |
Correct |
70 ms |
3628 KB |
Output is correct |
33 |
Correct |
99 ms |
3936 KB |
Output is correct |
34 |
Correct |
68 ms |
3880 KB |
Output is correct |
35 |
Correct |
81 ms |
4172 KB |
Output is correct |
36 |
Correct |
89 ms |
4352 KB |
Output is correct |
37 |
Correct |
70 ms |
3972 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
2 ms |
724 KB |
Output is correct |
41 |
Correct |
3 ms |
724 KB |
Output is correct |
42 |
Correct |
2 ms |
724 KB |
Output is correct |
43 |
Correct |
2 ms |
724 KB |
Output is correct |
44 |
Correct |
2 ms |
852 KB |
Output is correct |
45 |
Correct |
2 ms |
852 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
341 ms |
50972 KB |
Output is correct |
48 |
Correct |
380 ms |
50888 KB |
Output is correct |
49 |
Correct |
320 ms |
48816 KB |
Output is correct |
50 |
Correct |
412 ms |
54204 KB |
Output is correct |
51 |
Correct |
494 ms |
60336 KB |
Output is correct |
52 |
Correct |
496 ms |
60012 KB |
Output is correct |
53 |
Correct |
77 ms |
34984 KB |
Output is correct |
54 |
Correct |
449 ms |
51216 KB |
Output is correct |
55 |
Correct |
486 ms |
51192 KB |
Output is correct |
56 |
Correct |
590 ms |
51276 KB |
Output is correct |
57 |
Correct |
498 ms |
61152 KB |
Output is correct |
58 |
Correct |
661 ms |
51504 KB |
Output is correct |
59 |
Correct |
785 ms |
60748 KB |
Output is correct |
60 |
Correct |
744 ms |
60756 KB |
Output is correct |
61 |
Correct |
663 ms |
60872 KB |
Output is correct |
62 |
Correct |
439 ms |
59940 KB |
Output is correct |
63 |
Correct |
612 ms |
60408 KB |
Output is correct |
64 |
Correct |
611 ms |
60196 KB |
Output is correct |
65 |
Correct |
591 ms |
60480 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
411 ms |
51156 KB |
Output is correct |
68 |
Correct |
448 ms |
51128 KB |
Output is correct |
69 |
Correct |
530 ms |
51212 KB |
Output is correct |
70 |
Correct |
547 ms |
52128 KB |
Output is correct |
71 |
Correct |
571 ms |
53116 KB |
Output is correct |
72 |
Correct |
532 ms |
53340 KB |
Output is correct |
73 |
Correct |
471 ms |
61124 KB |
Output is correct |
74 |
Correct |
481 ms |
61120 KB |
Output is correct |
75 |
Correct |
721 ms |
51620 KB |
Output is correct |
76 |
Correct |
557 ms |
55408 KB |
Output is correct |
77 |
Correct |
230 ms |
48560 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
724 KB |
Output is correct |
80 |
Correct |
2 ms |
724 KB |
Output is correct |
81 |
Correct |
2 ms |
724 KB |
Output is correct |
82 |
Correct |
2 ms |
636 KB |
Output is correct |
83 |
Correct |
3 ms |
852 KB |
Output is correct |
84 |
Correct |
3 ms |
872 KB |
Output is correct |
85 |
Correct |
2 ms |
596 KB |
Output is correct |
86 |
Correct |
81 ms |
3748 KB |
Output is correct |
87 |
Correct |
67 ms |
3620 KB |
Output is correct |
88 |
Correct |
97 ms |
3884 KB |
Output is correct |
89 |
Correct |
66 ms |
3804 KB |
Output is correct |
90 |
Correct |
74 ms |
4172 KB |
Output is correct |
91 |
Correct |
75 ms |
4228 KB |
Output is correct |
92 |
Correct |
72 ms |
3916 KB |
Output is correct |
93 |
Correct |
358 ms |
50728 KB |
Output is correct |
94 |
Correct |
333 ms |
50544 KB |
Output is correct |
95 |
Correct |
341 ms |
48744 KB |
Output is correct |
96 |
Correct |
351 ms |
54112 KB |
Output is correct |
97 |
Correct |
463 ms |
59984 KB |
Output is correct |
98 |
Correct |
503 ms |
60024 KB |
Output is correct |
99 |
Correct |
57 ms |
34976 KB |
Output is correct |
100 |
Correct |
761 ms |
60836 KB |
Output is correct |
101 |
Correct |
654 ms |
60652 KB |
Output is correct |
102 |
Correct |
596 ms |
61008 KB |
Output is correct |
103 |
Correct |
571 ms |
60432 KB |
Output is correct |
104 |
Correct |
573 ms |
60156 KB |
Output is correct |
105 |
Correct |
625 ms |
60416 KB |
Output is correct |
106 |
Correct |
563 ms |
48912 KB |
Output is correct |
107 |
Correct |
693 ms |
51460 KB |
Output is correct |
108 |
Correct |
434 ms |
49584 KB |
Output is correct |
109 |
Correct |
376 ms |
49552 KB |
Output is correct |
110 |
Correct |
599 ms |
60640 KB |
Output is correct |
111 |
Correct |
631 ms |
60604 KB |
Output is correct |