#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int MAXN = 1e5;
pii ints[MAXN];
pair<pair<int, bool>, int> endpoints[2*MAXN];
int next_int[MAXN][20];
int prev_int[MAXN][20];
int n, q;
int ints_sort[MAXN];
const int inf = 2e9;
class STree {
public:
int n;
int *mn_left;
int n_orig;
STree(int nn) {
n_orig = nn;
n = pow(2, ceil(log2(nn)));
mn_left = new int[2*n];
fill(mn_left, mn_left+2*n, 0);
for (int i = 0; i < nn; i++) mn_left[i+n] = ints_sort[i];
for (int i = n-1; i > 0; i--) {
int la = ints[mn_left[2*i]].first;
int lb = ints[mn_left[2*i+1]].first;
if (la <= lb) mn_left[i] = mn_left[2*i];
else mn_left[i] = mn_left[2*i+1];
}
}
int get_l(int i) {
int lval = ints[i].first;
int rval = ints[i].second;
int mn = -1;
int mx = n_orig-1;
while (mx > mn+1) {
int cur = (mn+mx)/2;
if (ints[ints_sort[cur]].second >= lval) mx = cur;
else mn = cur;
}
int l = mx+n;
mn = 0;
mx = n_orig;
while (mx > mn+1) {
int cur = (mn+mx)/2;
if (ints[ints_sort[cur]].second <= rval) mn = cur;
else mx = cur;
}
int r = mx+n;
int best = i;
while (r > l) {
if (l & 1) {
if (ints[best].first > ints[mn_left[l]].first) best = mn_left[l];
l++;
}
if (r & 1) {
--r;
if (ints[best].first > ints[mn_left[r]].first) best = mn_left[r];
}
l /= 2;
r /= 2;
}
return best;
}
};
struct comp {
bool operator()(const int &a, const int &b) {
return (ints[a].second == ints[b].second) ? (a < b) : (ints[a].second < ints[b].second);
}
} comp;
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> q;
for (int i = 0; i < n; i++) {
int x, y; cin >> x >> y;
ints[i] = pii(x, y);
endpoints[2*i] = pair<pair<int, bool>, int>(pair<int, bool>(x, 0), i);
endpoints[2*i+1] = pair<pair<int, bool>, int>(pair<int, bool>(y, 1), i);
ints_sort[i] = i;
}
sort(ints_sort, ints_sort+n, comp);
sort(endpoints, endpoints+2*n);
set<pii, greater<pii>> open;
for (int i = 0; i < 2*n; i++) {
pair<pair<int, bool>, int> ev = endpoints[i];
if (ev.first.second == 0) open.insert(pii(ints[ev.second].second, ev.second));
else {
pii t = *(open.begin());
next_int[ev.second][0] = t.second;
open.erase(pii(ev.first.first, ev.second));
}
}
STree s(n);
for (int i = 0; i < n; i++) {
prev_int[i][0] = s.get_l(i);
}
for (int i = 1; i < 20; i++) {
for (int j = 0; j < n; j++) {
next_int[j][i] = next_int[next_int[j][i-1]][i-1];
prev_int[j][i] = prev_int[prev_int[j][i-1]][i-1];
}
}
for (int i = 0; i < q; i++) {
int x, y; cin >> x >> y;
x--; y--;
if (x == y) {
cout << "0\n";
continue;
}
if (ints[x].second > ints[y].second || ints[prev_int[y][19]].first > ints[x].second) {
cout << "impossible\n";
continue;
}
if (ints[y].first <= ints[x].second) {
cout << "1\n";
continue;
}
int jumps = 0;
for (int j = 19; j >= 0; j--) {
if (ints[next_int[x][j]].second < ints[y].first) {
x = next_int[x][j];
jumps += (1 << j);
}
}
for (int j = 19; j >= 0; j--) {
if (ints[prev_int[y][j]].first > ints[x].second) {
y = prev_int[y][j];
jumps += (1 << j);
}
}
cout << (jumps+2) << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
159 ms |
24200 KB |
Output is correct |
3 |
Correct |
166 ms |
24136 KB |
Output is correct |
4 |
Correct |
207 ms |
24048 KB |
Output is correct |
5 |
Correct |
170 ms |
24144 KB |
Output is correct |
6 |
Correct |
170 ms |
24152 KB |
Output is correct |
7 |
Correct |
166 ms |
24292 KB |
Output is correct |
8 |
Correct |
137 ms |
24700 KB |
Output is correct |
9 |
Correct |
132 ms |
24572 KB |
Output is correct |
10 |
Correct |
178 ms |
24460 KB |
Output is correct |
11 |
Correct |
167 ms |
24496 KB |
Output is correct |
12 |
Correct |
82 ms |
23880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
556 KB |
Output is correct |
6 |
Correct |
1 ms |
500 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
556 KB |
Output is correct |
6 |
Correct |
1 ms |
500 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
508 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
504 KB |
Output is correct |
18 |
Correct |
1 ms |
508 KB |
Output is correct |
19 |
Correct |
43 ms |
2896 KB |
Output is correct |
20 |
Correct |
35 ms |
2864 KB |
Output is correct |
21 |
Correct |
34 ms |
3076 KB |
Output is correct |
22 |
Correct |
26 ms |
3156 KB |
Output is correct |
23 |
Correct |
25 ms |
3096 KB |
Output is correct |
24 |
Correct |
21 ms |
3128 KB |
Output is correct |
25 |
Correct |
30 ms |
2612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
556 KB |
Output is correct |
6 |
Correct |
1 ms |
500 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
508 KB |
Output is correct |
13 |
Correct |
1 ms |
508 KB |
Output is correct |
14 |
Correct |
1 ms |
560 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
133 ms |
22368 KB |
Output is correct |
20 |
Correct |
122 ms |
21628 KB |
Output is correct |
21 |
Correct |
144 ms |
23112 KB |
Output is correct |
22 |
Correct |
131 ms |
22284 KB |
Output is correct |
23 |
Correct |
130 ms |
26084 KB |
Output is correct |
24 |
Correct |
136 ms |
22344 KB |
Output is correct |
25 |
Correct |
90 ms |
25292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
165 ms |
24208 KB |
Output is correct |
2 |
Correct |
180 ms |
24192 KB |
Output is correct |
3 |
Correct |
198 ms |
23996 KB |
Output is correct |
4 |
Correct |
141 ms |
24516 KB |
Output is correct |
5 |
Correct |
196 ms |
24476 KB |
Output is correct |
6 |
Correct |
192 ms |
24204 KB |
Output is correct |
7 |
Correct |
210 ms |
24264 KB |
Output is correct |
8 |
Correct |
179 ms |
24480 KB |
Output is correct |
9 |
Correct |
142 ms |
26044 KB |
Output is correct |
10 |
Correct |
198 ms |
23884 KB |
Output is correct |
11 |
Correct |
189 ms |
23876 KB |
Output is correct |
12 |
Correct |
189 ms |
23896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
159 ms |
24200 KB |
Output is correct |
3 |
Correct |
166 ms |
24136 KB |
Output is correct |
4 |
Correct |
207 ms |
24048 KB |
Output is correct |
5 |
Correct |
170 ms |
24144 KB |
Output is correct |
6 |
Correct |
170 ms |
24152 KB |
Output is correct |
7 |
Correct |
166 ms |
24292 KB |
Output is correct |
8 |
Correct |
137 ms |
24700 KB |
Output is correct |
9 |
Correct |
132 ms |
24572 KB |
Output is correct |
10 |
Correct |
178 ms |
24460 KB |
Output is correct |
11 |
Correct |
167 ms |
24496 KB |
Output is correct |
12 |
Correct |
82 ms |
23880 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
504 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
556 KB |
Output is correct |
18 |
Correct |
1 ms |
500 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
504 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
508 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
504 KB |
Output is correct |
30 |
Correct |
1 ms |
508 KB |
Output is correct |
31 |
Correct |
43 ms |
2896 KB |
Output is correct |
32 |
Correct |
35 ms |
2864 KB |
Output is correct |
33 |
Correct |
34 ms |
3076 KB |
Output is correct |
34 |
Correct |
26 ms |
3156 KB |
Output is correct |
35 |
Correct |
25 ms |
3096 KB |
Output is correct |
36 |
Correct |
21 ms |
3128 KB |
Output is correct |
37 |
Correct |
30 ms |
2612 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
2 ms |
508 KB |
Output is correct |
41 |
Correct |
1 ms |
508 KB |
Output is correct |
42 |
Correct |
1 ms |
560 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
133 ms |
22368 KB |
Output is correct |
48 |
Correct |
122 ms |
21628 KB |
Output is correct |
49 |
Correct |
144 ms |
23112 KB |
Output is correct |
50 |
Correct |
131 ms |
22284 KB |
Output is correct |
51 |
Correct |
130 ms |
26084 KB |
Output is correct |
52 |
Correct |
136 ms |
22344 KB |
Output is correct |
53 |
Correct |
90 ms |
25292 KB |
Output is correct |
54 |
Correct |
165 ms |
24208 KB |
Output is correct |
55 |
Correct |
180 ms |
24192 KB |
Output is correct |
56 |
Correct |
198 ms |
23996 KB |
Output is correct |
57 |
Correct |
141 ms |
24516 KB |
Output is correct |
58 |
Correct |
196 ms |
24476 KB |
Output is correct |
59 |
Correct |
192 ms |
24204 KB |
Output is correct |
60 |
Correct |
210 ms |
24264 KB |
Output is correct |
61 |
Correct |
179 ms |
24480 KB |
Output is correct |
62 |
Correct |
142 ms |
26044 KB |
Output is correct |
63 |
Correct |
198 ms |
23884 KB |
Output is correct |
64 |
Correct |
189 ms |
23876 KB |
Output is correct |
65 |
Correct |
189 ms |
23896 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
171 ms |
24060 KB |
Output is correct |
68 |
Correct |
187 ms |
24140 KB |
Output is correct |
69 |
Correct |
205 ms |
24036 KB |
Output is correct |
70 |
Correct |
186 ms |
24264 KB |
Output is correct |
71 |
Correct |
174 ms |
24248 KB |
Output is correct |
72 |
Correct |
170 ms |
24184 KB |
Output is correct |
73 |
Correct |
132 ms |
24556 KB |
Output is correct |
74 |
Correct |
141 ms |
24568 KB |
Output is correct |
75 |
Correct |
176 ms |
24524 KB |
Output is correct |
76 |
Correct |
162 ms |
24564 KB |
Output is correct |
77 |
Correct |
81 ms |
23824 KB |
Output is correct |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
1 ms |
468 KB |
Output is correct |
81 |
Correct |
1 ms |
560 KB |
Output is correct |
82 |
Correct |
1 ms |
468 KB |
Output is correct |
83 |
Correct |
1 ms |
468 KB |
Output is correct |
84 |
Correct |
1 ms |
596 KB |
Output is correct |
85 |
Correct |
1 ms |
596 KB |
Output is correct |
86 |
Correct |
37 ms |
2816 KB |
Output is correct |
87 |
Correct |
33 ms |
2864 KB |
Output is correct |
88 |
Correct |
34 ms |
3060 KB |
Output is correct |
89 |
Correct |
24 ms |
3156 KB |
Output is correct |
90 |
Correct |
24 ms |
3076 KB |
Output is correct |
91 |
Correct |
22 ms |
3100 KB |
Output is correct |
92 |
Correct |
30 ms |
2592 KB |
Output is correct |
93 |
Correct |
131 ms |
22452 KB |
Output is correct |
94 |
Correct |
122 ms |
21628 KB |
Output is correct |
95 |
Correct |
137 ms |
23340 KB |
Output is correct |
96 |
Correct |
128 ms |
22476 KB |
Output is correct |
97 |
Correct |
124 ms |
26040 KB |
Output is correct |
98 |
Correct |
145 ms |
22372 KB |
Output is correct |
99 |
Correct |
94 ms |
25388 KB |
Output is correct |
100 |
Correct |
181 ms |
24260 KB |
Output is correct |
101 |
Correct |
175 ms |
24288 KB |
Output is correct |
102 |
Correct |
172 ms |
24532 KB |
Output is correct |
103 |
Correct |
174 ms |
23932 KB |
Output is correct |
104 |
Correct |
166 ms |
23628 KB |
Output is correct |
105 |
Correct |
193 ms |
24084 KB |
Output is correct |
106 |
Correct |
174 ms |
24780 KB |
Output is correct |
107 |
Correct |
180 ms |
23656 KB |
Output is correct |
108 |
Correct |
161 ms |
25164 KB |
Output is correct |
109 |
Correct |
171 ms |
25188 KB |
Output is correct |
110 |
Correct |
185 ms |
26572 KB |
Output is correct |
111 |
Correct |
181 ms |
26472 KB |
Output is correct |