#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct event {
int s, e, ind;
};
int n, q;
vector <event> ev;
vector <int> new_ind(1e5 + 1);
vector <int> Log(1e5 + 1, 0);
vector <vector <int>> st(2e5, vector<int>(17));
vector <vector <int>> lift(1e5, vector<int>(17));
bool srt(event a, event b) {
if (a.e < b.e) return true;
if (a.e > b.e) return false;
return (a.s < b.s ? true : false);
}
int mini(int a, int b) {
return (ev[a].s < ev[b].s ? a : b);
}
int range_mini(int l, int r) {
int j = Log[r - l + 1];
return mini(st[l][j], st[r - (1 << j) + 1][j]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int a, b, c, ans;
cin >> n >> q;
ev.resize(n);
for (int i = 0; i < n; i++) {
cin >> ev[i].s >> ev[i].e;
ev[i].ind = i + 1;
}
sort(ev.begin(), ev.end(), srt);
for (int i = 0; i < n; i++) new_ind[ev[i].ind] = i;
for (int i = 2; i <= 1e5; i++) Log[i] = Log[i / 2] + 1;
for (int i = 0; i < n; i++) st[i][0] = i;
for (int i = n; i < 2e5; i++) fill(st[i].begin(), st[i].end(), n - 1);
for (int j = 1; j < 17; j++) {
for (int i = 0; i < n; i++) {
st[i][j] = mini(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
for (int i = 0; i < n; i++) {
a = -1; b = i;
while (b - a > 1) {
c = (a + b) / 2;
(ev[c].e < ev[i].s ? a = c : b = c);
}
lift[i][0] = b;
for (int j = 1; j < 17; j++) {
a = range_mini(lift[i][j - 1], i);
lift[i][j] = lift[a][j - 1];
}
}
for (int i = 0; i < q; i++) {
cin >> a >> b;
a = new_ind[a];
b = new_ind[b];
if (a == b) { cout << "0\n"; continue; }
if (ev[a].e == ev[b].e) { cout << "1\n"; continue; }
if (a > b) { cout << "impossible\n"; continue; }
c = b; ans = 0;
for (int j = 16; j >= 0; j--) {
if (lift[c][j] <= a) continue;
c = lift[c][j];
c = range_mini(c, b);
ans += (1 << j);
}
ans++;
if (ans < 1e5) cout << ans << endl;
else cout << "impossible\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
31572 KB |
Output is correct |
2 |
Correct |
270 ms |
36504 KB |
Output is correct |
3 |
Correct |
244 ms |
36460 KB |
Output is correct |
4 |
Correct |
286 ms |
36492 KB |
Output is correct |
5 |
Correct |
287 ms |
36564 KB |
Output is correct |
6 |
Correct |
269 ms |
36504 KB |
Output is correct |
7 |
Correct |
262 ms |
36516 KB |
Output is correct |
8 |
Correct |
127 ms |
36888 KB |
Output is correct |
9 |
Correct |
128 ms |
36888 KB |
Output is correct |
10 |
Correct |
205 ms |
36844 KB |
Output is correct |
11 |
Correct |
189 ms |
34892 KB |
Output is correct |
12 |
Correct |
115 ms |
36248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
31628 KB |
Output is correct |
2 |
Correct |
21 ms |
31632 KB |
Output is correct |
3 |
Correct |
24 ms |
31572 KB |
Output is correct |
4 |
Correct |
24 ms |
31572 KB |
Output is correct |
5 |
Correct |
24 ms |
31596 KB |
Output is correct |
6 |
Correct |
23 ms |
31584 KB |
Output is correct |
7 |
Correct |
33 ms |
31652 KB |
Output is correct |
8 |
Correct |
33 ms |
31692 KB |
Output is correct |
9 |
Correct |
24 ms |
31660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
31628 KB |
Output is correct |
2 |
Correct |
21 ms |
31632 KB |
Output is correct |
3 |
Correct |
24 ms |
31572 KB |
Output is correct |
4 |
Correct |
24 ms |
31572 KB |
Output is correct |
5 |
Correct |
24 ms |
31596 KB |
Output is correct |
6 |
Correct |
23 ms |
31584 KB |
Output is correct |
7 |
Correct |
33 ms |
31652 KB |
Output is correct |
8 |
Correct |
33 ms |
31692 KB |
Output is correct |
9 |
Correct |
24 ms |
31660 KB |
Output is correct |
10 |
Correct |
20 ms |
31592 KB |
Output is correct |
11 |
Correct |
20 ms |
31536 KB |
Output is correct |
12 |
Correct |
24 ms |
31648 KB |
Output is correct |
13 |
Correct |
23 ms |
31612 KB |
Output is correct |
14 |
Correct |
24 ms |
31552 KB |
Output is correct |
15 |
Correct |
24 ms |
31680 KB |
Output is correct |
16 |
Correct |
25 ms |
31644 KB |
Output is correct |
17 |
Correct |
31 ms |
31576 KB |
Output is correct |
18 |
Correct |
23 ms |
31656 KB |
Output is correct |
19 |
Correct |
151 ms |
33108 KB |
Output is correct |
20 |
Correct |
152 ms |
33200 KB |
Output is correct |
21 |
Correct |
107 ms |
33404 KB |
Output is correct |
22 |
Correct |
86 ms |
33452 KB |
Output is correct |
23 |
Correct |
129 ms |
33464 KB |
Output is correct |
24 |
Correct |
102 ms |
33320 KB |
Output is correct |
25 |
Correct |
165 ms |
33008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
31628 KB |
Output is correct |
2 |
Correct |
21 ms |
31632 KB |
Output is correct |
3 |
Correct |
24 ms |
31572 KB |
Output is correct |
4 |
Correct |
24 ms |
31572 KB |
Output is correct |
5 |
Correct |
24 ms |
31596 KB |
Output is correct |
6 |
Correct |
23 ms |
31584 KB |
Output is correct |
7 |
Correct |
33 ms |
31652 KB |
Output is correct |
8 |
Correct |
33 ms |
31692 KB |
Output is correct |
9 |
Correct |
24 ms |
31660 KB |
Output is correct |
10 |
Correct |
24 ms |
31572 KB |
Output is correct |
11 |
Correct |
23 ms |
31576 KB |
Output is correct |
12 |
Correct |
24 ms |
31660 KB |
Output is correct |
13 |
Correct |
23 ms |
31584 KB |
Output is correct |
14 |
Correct |
25 ms |
31644 KB |
Output is correct |
15 |
Correct |
31 ms |
31660 KB |
Output is correct |
16 |
Correct |
25 ms |
31656 KB |
Output is correct |
17 |
Correct |
25 ms |
31572 KB |
Output is correct |
18 |
Correct |
32 ms |
31644 KB |
Output is correct |
19 |
Correct |
98 ms |
34752 KB |
Output is correct |
20 |
Correct |
95 ms |
33960 KB |
Output is correct |
21 |
Correct |
100 ms |
34652 KB |
Output is correct |
22 |
Correct |
135 ms |
34764 KB |
Output is correct |
23 |
Correct |
88 ms |
34740 KB |
Output is correct |
24 |
Correct |
122 ms |
34740 KB |
Output is correct |
25 |
Correct |
74 ms |
34072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
235 ms |
36524 KB |
Output is correct |
2 |
Correct |
287 ms |
36428 KB |
Output is correct |
3 |
Correct |
258 ms |
36500 KB |
Output is correct |
4 |
Correct |
128 ms |
36888 KB |
Output is correct |
5 |
Correct |
227 ms |
36876 KB |
Output is correct |
6 |
Correct |
331 ms |
36588 KB |
Output is correct |
7 |
Correct |
252 ms |
36636 KB |
Output is correct |
8 |
Correct |
202 ms |
36744 KB |
Output is correct |
9 |
Correct |
89 ms |
33448 KB |
Output is correct |
10 |
Correct |
245 ms |
36280 KB |
Output is correct |
11 |
Correct |
217 ms |
36044 KB |
Output is correct |
12 |
Correct |
255 ms |
36468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
31572 KB |
Output is correct |
2 |
Correct |
270 ms |
36504 KB |
Output is correct |
3 |
Correct |
244 ms |
36460 KB |
Output is correct |
4 |
Correct |
286 ms |
36492 KB |
Output is correct |
5 |
Correct |
287 ms |
36564 KB |
Output is correct |
6 |
Correct |
269 ms |
36504 KB |
Output is correct |
7 |
Correct |
262 ms |
36516 KB |
Output is correct |
8 |
Correct |
127 ms |
36888 KB |
Output is correct |
9 |
Correct |
128 ms |
36888 KB |
Output is correct |
10 |
Correct |
205 ms |
36844 KB |
Output is correct |
11 |
Correct |
189 ms |
34892 KB |
Output is correct |
12 |
Correct |
115 ms |
36248 KB |
Output is correct |
13 |
Correct |
22 ms |
31628 KB |
Output is correct |
14 |
Correct |
21 ms |
31632 KB |
Output is correct |
15 |
Correct |
24 ms |
31572 KB |
Output is correct |
16 |
Correct |
24 ms |
31572 KB |
Output is correct |
17 |
Correct |
24 ms |
31596 KB |
Output is correct |
18 |
Correct |
23 ms |
31584 KB |
Output is correct |
19 |
Correct |
33 ms |
31652 KB |
Output is correct |
20 |
Correct |
33 ms |
31692 KB |
Output is correct |
21 |
Correct |
24 ms |
31660 KB |
Output is correct |
22 |
Correct |
20 ms |
31592 KB |
Output is correct |
23 |
Correct |
20 ms |
31536 KB |
Output is correct |
24 |
Correct |
24 ms |
31648 KB |
Output is correct |
25 |
Correct |
23 ms |
31612 KB |
Output is correct |
26 |
Correct |
24 ms |
31552 KB |
Output is correct |
27 |
Correct |
24 ms |
31680 KB |
Output is correct |
28 |
Correct |
25 ms |
31644 KB |
Output is correct |
29 |
Correct |
31 ms |
31576 KB |
Output is correct |
30 |
Correct |
23 ms |
31656 KB |
Output is correct |
31 |
Correct |
151 ms |
33108 KB |
Output is correct |
32 |
Correct |
152 ms |
33200 KB |
Output is correct |
33 |
Correct |
107 ms |
33404 KB |
Output is correct |
34 |
Correct |
86 ms |
33452 KB |
Output is correct |
35 |
Correct |
129 ms |
33464 KB |
Output is correct |
36 |
Correct |
102 ms |
33320 KB |
Output is correct |
37 |
Correct |
165 ms |
33008 KB |
Output is correct |
38 |
Correct |
24 ms |
31572 KB |
Output is correct |
39 |
Correct |
23 ms |
31576 KB |
Output is correct |
40 |
Correct |
24 ms |
31660 KB |
Output is correct |
41 |
Correct |
23 ms |
31584 KB |
Output is correct |
42 |
Correct |
25 ms |
31644 KB |
Output is correct |
43 |
Correct |
31 ms |
31660 KB |
Output is correct |
44 |
Correct |
25 ms |
31656 KB |
Output is correct |
45 |
Correct |
25 ms |
31572 KB |
Output is correct |
46 |
Correct |
32 ms |
31644 KB |
Output is correct |
47 |
Correct |
98 ms |
34752 KB |
Output is correct |
48 |
Correct |
95 ms |
33960 KB |
Output is correct |
49 |
Correct |
100 ms |
34652 KB |
Output is correct |
50 |
Correct |
135 ms |
34764 KB |
Output is correct |
51 |
Correct |
88 ms |
34740 KB |
Output is correct |
52 |
Correct |
122 ms |
34740 KB |
Output is correct |
53 |
Correct |
74 ms |
34072 KB |
Output is correct |
54 |
Correct |
235 ms |
36524 KB |
Output is correct |
55 |
Correct |
287 ms |
36428 KB |
Output is correct |
56 |
Correct |
258 ms |
36500 KB |
Output is correct |
57 |
Correct |
128 ms |
36888 KB |
Output is correct |
58 |
Correct |
227 ms |
36876 KB |
Output is correct |
59 |
Correct |
331 ms |
36588 KB |
Output is correct |
60 |
Correct |
252 ms |
36636 KB |
Output is correct |
61 |
Correct |
202 ms |
36744 KB |
Output is correct |
62 |
Correct |
89 ms |
33448 KB |
Output is correct |
63 |
Correct |
245 ms |
36280 KB |
Output is correct |
64 |
Correct |
217 ms |
36044 KB |
Output is correct |
65 |
Correct |
255 ms |
36468 KB |
Output is correct |
66 |
Correct |
19 ms |
31636 KB |
Output is correct |
67 |
Correct |
232 ms |
36496 KB |
Output is correct |
68 |
Correct |
257 ms |
36472 KB |
Output is correct |
69 |
Correct |
280 ms |
36492 KB |
Output is correct |
70 |
Correct |
249 ms |
36496 KB |
Output is correct |
71 |
Correct |
251 ms |
36500 KB |
Output is correct |
72 |
Correct |
250 ms |
36500 KB |
Output is correct |
73 |
Correct |
133 ms |
36904 KB |
Output is correct |
74 |
Correct |
134 ms |
36884 KB |
Output is correct |
75 |
Correct |
220 ms |
36760 KB |
Output is correct |
76 |
Correct |
189 ms |
36888 KB |
Output is correct |
77 |
Correct |
128 ms |
36248 KB |
Output is correct |
78 |
Correct |
21 ms |
31612 KB |
Output is correct |
79 |
Correct |
31 ms |
31572 KB |
Output is correct |
80 |
Correct |
31 ms |
31652 KB |
Output is correct |
81 |
Correct |
24 ms |
31652 KB |
Output is correct |
82 |
Correct |
25 ms |
31572 KB |
Output is correct |
83 |
Correct |
24 ms |
31572 KB |
Output is correct |
84 |
Correct |
24 ms |
31568 KB |
Output is correct |
85 |
Correct |
42 ms |
31636 KB |
Output is correct |
86 |
Correct |
155 ms |
33080 KB |
Output is correct |
87 |
Correct |
151 ms |
33208 KB |
Output is correct |
88 |
Correct |
111 ms |
33336 KB |
Output is correct |
89 |
Correct |
89 ms |
33484 KB |
Output is correct |
90 |
Correct |
94 ms |
33356 KB |
Output is correct |
91 |
Correct |
128 ms |
33320 KB |
Output is correct |
92 |
Correct |
147 ms |
32948 KB |
Output is correct |
93 |
Correct |
104 ms |
34760 KB |
Output is correct |
94 |
Correct |
94 ms |
33956 KB |
Output is correct |
95 |
Correct |
94 ms |
34652 KB |
Output is correct |
96 |
Correct |
110 ms |
34708 KB |
Output is correct |
97 |
Correct |
88 ms |
34740 KB |
Output is correct |
98 |
Correct |
118 ms |
34764 KB |
Output is correct |
99 |
Correct |
88 ms |
34076 KB |
Output is correct |
100 |
Correct |
248 ms |
36556 KB |
Output is correct |
101 |
Correct |
250 ms |
36620 KB |
Output is correct |
102 |
Correct |
193 ms |
36660 KB |
Output is correct |
103 |
Correct |
272 ms |
36236 KB |
Output is correct |
104 |
Correct |
211 ms |
36068 KB |
Output is correct |
105 |
Correct |
257 ms |
36204 KB |
Output is correct |
106 |
Correct |
204 ms |
35944 KB |
Output is correct |
107 |
Correct |
326 ms |
35916 KB |
Output is correct |
108 |
Correct |
158 ms |
36532 KB |
Output is correct |
109 |
Correct |
168 ms |
36548 KB |
Output is correct |
110 |
Correct |
189 ms |
36428 KB |
Output is correct |
111 |
Correct |
176 ms |
36428 KB |
Output is correct |