#include <iostream>
using namespace std;
#include <vector>
#include <set>
#define pi pair<int,int>
#define vi vector<int>
#include <queue>
#include <algorithm>
#define ll long long
const int siz = 1e5;
int lii[siz];
int rii[siz];
int bin[siz][20];
int in[siz];
struct node {
int l, r, mid;
pi maxi;
node* sl, * sr;
node(int li, int ri) {
l = li, r = ri, mid = (l + r) / 2;
if (l < r) {
sl = new node(l, mid);
sr = new node(mid + 1, r);
maxi = max(sl->maxi, sr->maxi);
}
else {
maxi = { rii[l],l };
}
}
pi query(int a, int b) {
if (a > r || b < l)return { 0,0 };
if (l >= a && r <= b)return maxi;
return max(sl->query(a, b), sr->query(a, b));
}
};
int main()
{
int n, q;
cin >> n >> q;
vector<vi> vec;
vi v1, v2;
for (int i = 0; i < n; i++) {
int s, e;
cin >> s >> e;
vec.push_back({ e,s,i });//
}
sort(vec.begin(), vec.end());
for (vi p : vec) {
v1.push_back(p[0]); v2.push_back(p[1]);
}
for (int i = 0; i < n; i++) {
lii[i] = lower_bound(v1.begin(), v1.end(), v2[i]) - v1.begin();
in[vec[i][2]] = i;
}
for (int i = 0; i < n; i++) {
rii[n - 1 - i] = n - 1 - (lii[i]);
in[i] = n - 1 - in[i];
}
node* seg = new node(0, n - 1);
for (int i = 0; i < n; i++) {
bin[i][0] = seg->query(i, rii[i]).second;
}
for (int i = 1; i < 20; i++) {
for (int v = 0; v < n; v++) {
bin[v][i] = bin[bin[v][i - 1]][i - 1];
}
}
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
swap(a, b);
a--, b--;
a = in[a], b = in[b];
if (a == b)cout << 0 << "\n";
else if (v1[n - 1 - a] == v1[n - 1 - b]) cout << 1 << "\n";
else if (a > b)cout << "impossible" << "\n";
else if (rii[a] >= b) cout << 1 << "\n";
else {
int ans = 0;
for (int i = 19; i >= 0; i--) {
if (rii[bin[a][i]] < b) {
a = bin[a][i];
ans += 1 << i;
}
}
if (rii[bin[a][0]] >= b)cout << ans + 2 << "\n";
else cout << "impossible" << "\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
311 ms |
26496 KB |
Output is correct |
3 |
Correct |
317 ms |
28380 KB |
Output is correct |
4 |
Correct |
335 ms |
28312 KB |
Output is correct |
5 |
Correct |
320 ms |
28740 KB |
Output is correct |
6 |
Correct |
320 ms |
28292 KB |
Output is correct |
7 |
Correct |
316 ms |
28596 KB |
Output is correct |
8 |
Correct |
312 ms |
28612 KB |
Output is correct |
9 |
Correct |
318 ms |
28644 KB |
Output is correct |
10 |
Correct |
357 ms |
28540 KB |
Output is correct |
11 |
Correct |
336 ms |
28580 KB |
Output is correct |
12 |
Correct |
275 ms |
28492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 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 |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
572 KB |
Output is correct |
16 |
Correct |
3 ms |
576 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
192 ms |
3144 KB |
Output is correct |
20 |
Correct |
188 ms |
3020 KB |
Output is correct |
21 |
Correct |
181 ms |
3248 KB |
Output is correct |
22 |
Correct |
181 ms |
3404 KB |
Output is correct |
23 |
Correct |
176 ms |
3276 KB |
Output is correct |
24 |
Correct |
178 ms |
3244 KB |
Output is correct |
25 |
Correct |
183 ms |
2888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 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 |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
568 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
580 KB |
Output is correct |
18 |
Correct |
2 ms |
576 KB |
Output is correct |
19 |
Correct |
163 ms |
27012 KB |
Output is correct |
20 |
Correct |
108 ms |
26236 KB |
Output is correct |
21 |
Correct |
141 ms |
26900 KB |
Output is correct |
22 |
Correct |
133 ms |
26948 KB |
Output is correct |
23 |
Correct |
128 ms |
26940 KB |
Output is correct |
24 |
Correct |
138 ms |
27020 KB |
Output is correct |
25 |
Correct |
106 ms |
26284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
313 ms |
26644 KB |
Output is correct |
2 |
Correct |
319 ms |
28668 KB |
Output is correct |
3 |
Correct |
320 ms |
28672 KB |
Output is correct |
4 |
Correct |
320 ms |
29252 KB |
Output is correct |
5 |
Correct |
344 ms |
29048 KB |
Output is correct |
6 |
Correct |
347 ms |
28860 KB |
Output is correct |
7 |
Correct |
347 ms |
28892 KB |
Output is correct |
8 |
Correct |
343 ms |
29116 KB |
Output is correct |
9 |
Correct |
135 ms |
27104 KB |
Output is correct |
10 |
Correct |
320 ms |
28520 KB |
Output is correct |
11 |
Correct |
315 ms |
28380 KB |
Output is correct |
12 |
Correct |
333 ms |
28512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
311 ms |
26496 KB |
Output is correct |
3 |
Correct |
317 ms |
28380 KB |
Output is correct |
4 |
Correct |
335 ms |
28312 KB |
Output is correct |
5 |
Correct |
320 ms |
28740 KB |
Output is correct |
6 |
Correct |
320 ms |
28292 KB |
Output is correct |
7 |
Correct |
316 ms |
28596 KB |
Output is correct |
8 |
Correct |
312 ms |
28612 KB |
Output is correct |
9 |
Correct |
318 ms |
28644 KB |
Output is correct |
10 |
Correct |
357 ms |
28540 KB |
Output is correct |
11 |
Correct |
336 ms |
28580 KB |
Output is correct |
12 |
Correct |
275 ms |
28492 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 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 |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
572 KB |
Output is correct |
28 |
Correct |
3 ms |
576 KB |
Output is correct |
29 |
Correct |
2 ms |
596 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
192 ms |
3144 KB |
Output is correct |
32 |
Correct |
188 ms |
3020 KB |
Output is correct |
33 |
Correct |
181 ms |
3248 KB |
Output is correct |
34 |
Correct |
181 ms |
3404 KB |
Output is correct |
35 |
Correct |
176 ms |
3276 KB |
Output is correct |
36 |
Correct |
178 ms |
3244 KB |
Output is correct |
37 |
Correct |
183 ms |
2888 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 |
596 KB |
Output is correct |
41 |
Correct |
2 ms |
596 KB |
Output is correct |
42 |
Correct |
2 ms |
568 KB |
Output is correct |
43 |
Correct |
2 ms |
596 KB |
Output is correct |
44 |
Correct |
2 ms |
596 KB |
Output is correct |
45 |
Correct |
2 ms |
580 KB |
Output is correct |
46 |
Correct |
2 ms |
576 KB |
Output is correct |
47 |
Correct |
163 ms |
27012 KB |
Output is correct |
48 |
Correct |
108 ms |
26236 KB |
Output is correct |
49 |
Correct |
141 ms |
26900 KB |
Output is correct |
50 |
Correct |
133 ms |
26948 KB |
Output is correct |
51 |
Correct |
128 ms |
26940 KB |
Output is correct |
52 |
Correct |
138 ms |
27020 KB |
Output is correct |
53 |
Correct |
106 ms |
26284 KB |
Output is correct |
54 |
Correct |
313 ms |
26644 KB |
Output is correct |
55 |
Correct |
319 ms |
28668 KB |
Output is correct |
56 |
Correct |
320 ms |
28672 KB |
Output is correct |
57 |
Correct |
320 ms |
29252 KB |
Output is correct |
58 |
Correct |
344 ms |
29048 KB |
Output is correct |
59 |
Correct |
347 ms |
28860 KB |
Output is correct |
60 |
Correct |
347 ms |
28892 KB |
Output is correct |
61 |
Correct |
343 ms |
29116 KB |
Output is correct |
62 |
Correct |
135 ms |
27104 KB |
Output is correct |
63 |
Correct |
320 ms |
28520 KB |
Output is correct |
64 |
Correct |
315 ms |
28380 KB |
Output is correct |
65 |
Correct |
333 ms |
28512 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
322 ms |
28916 KB |
Output is correct |
68 |
Correct |
312 ms |
28736 KB |
Output is correct |
69 |
Correct |
326 ms |
28844 KB |
Output is correct |
70 |
Correct |
324 ms |
28876 KB |
Output is correct |
71 |
Correct |
311 ms |
28816 KB |
Output is correct |
72 |
Correct |
311 ms |
28788 KB |
Output is correct |
73 |
Correct |
305 ms |
29172 KB |
Output is correct |
74 |
Correct |
308 ms |
29192 KB |
Output is correct |
75 |
Correct |
327 ms |
29044 KB |
Output is correct |
76 |
Correct |
321 ms |
29176 KB |
Output is correct |
77 |
Correct |
276 ms |
28448 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
2 ms |
596 KB |
Output is correct |
80 |
Correct |
2 ms |
580 KB |
Output is correct |
81 |
Correct |
1 ms |
572 KB |
Output is correct |
82 |
Correct |
2 ms |
500 KB |
Output is correct |
83 |
Correct |
2 ms |
596 KB |
Output is correct |
84 |
Correct |
2 ms |
596 KB |
Output is correct |
85 |
Correct |
2 ms |
596 KB |
Output is correct |
86 |
Correct |
183 ms |
3012 KB |
Output is correct |
87 |
Correct |
176 ms |
3084 KB |
Output is correct |
88 |
Correct |
186 ms |
3332 KB |
Output is correct |
89 |
Correct |
178 ms |
3444 KB |
Output is correct |
90 |
Correct |
175 ms |
3176 KB |
Output is correct |
91 |
Correct |
178 ms |
3232 KB |
Output is correct |
92 |
Correct |
178 ms |
2912 KB |
Output is correct |
93 |
Correct |
132 ms |
26992 KB |
Output is correct |
94 |
Correct |
106 ms |
26140 KB |
Output is correct |
95 |
Correct |
145 ms |
26832 KB |
Output is correct |
96 |
Correct |
133 ms |
26956 KB |
Output is correct |
97 |
Correct |
130 ms |
26920 KB |
Output is correct |
98 |
Correct |
140 ms |
26992 KB |
Output is correct |
99 |
Correct |
97 ms |
26228 KB |
Output is correct |
100 |
Correct |
364 ms |
28924 KB |
Output is correct |
101 |
Correct |
359 ms |
28784 KB |
Output is correct |
102 |
Correct |
320 ms |
29044 KB |
Output is correct |
103 |
Correct |
313 ms |
28500 KB |
Output is correct |
104 |
Correct |
313 ms |
28356 KB |
Output is correct |
105 |
Correct |
309 ms |
28508 KB |
Output is correct |
106 |
Correct |
320 ms |
28208 KB |
Output is correct |
107 |
Correct |
308 ms |
28188 KB |
Output is correct |
108 |
Correct |
356 ms |
28800 KB |
Output is correct |
109 |
Correct |
318 ms |
28860 KB |
Output is correct |
110 |
Correct |
360 ms |
28804 KB |
Output is correct |
111 |
Correct |
343 ms |
28756 KB |
Output is correct |