#include <bits/stdc++.h>
#define ll long long
#define oo 1e9
#define pii pair<int, int>
using namespace std;
const int MAX = 2e5 + 5;
const pii dummy = {oo + 5, -1};
int n, q;
pii arr[MAX];
pii tree[MAX * 4];
void update(int node, int l, int r, int pos, pii val){
if(l == r){
tree[node] = min(tree[node], val);
return;
}
int mid = (l + r) / 2;
if(pos <= mid){
update(node * 2, l, mid, pos, val);
}
else{
update(node * 2 + 1, mid + 1, r, pos, val);
}
tree[node] = min(tree[node * 2], tree[node * 2 + 1]);
}
pii ask(int node, int l, int r, int ql, int qr){
if(ql > r || qr < l) return dummy;
if(ql <= l && r <= qr) return tree[node];
int mid = (l + r) / 2;
return min(ask(node * 2, l, mid, ql, qr), ask(node * 2 + 1, mid + 1, r, ql, qr));
}
bool comp(array<int, 3> a, array<int, 3> b){
if(a[1] == b[1]) return a[0] < b[0];
return a[1] < b[1];
}
int par[18][MAX];
int main()
{
fill(tree, tree + MAX * 4, dummy);
cin >> n >> q;
map<int, int> mp;
for (int i = 0; i < n; i++)
{
cin >> arr[i].first >> arr[i].second;
mp[arr[i].first];
mp[arr[i].second];
}
int c = 0;
for(auto& p:mp){
p.second = ++c;
}
vector<array<int, 3>> v;
for (int i = 0; i < n; i++)
{
arr[i].first = mp[arr[i].first];
arr[i].second = mp[arr[i].second];
v.push_back({arr[i].first, arr[i].second, i});
}
sort(v.begin(), v.end(), comp);
for (int i = 0; i < n; i++)
{
update(1, 1, MAX, v[i][1], {v[i][0], v[i][2]});
par[0][v[i][2]] = ask(1, 1, MAX, v[i][0], v[i][1]).second;
}
for (int j = 1; j < 18; j++)
{
for (int i = 0; i < n; i++)
{
par[j][i] = par[j - 1][par[j - 1][i]];
}
}
while(q--){
int s, e; cin >> s >> e;
s--;
e--;
if(s == e){
cout << 0 << '\n';
continue;
}
int dist = 0;
int best = oo;
int pos = false;
for (int j = 17; j >= 0; j--)
{
int idx = par[j][e];
if(arr[s].second < arr[idx].first){
dist += (1 << j);
e = idx;
}
else if(arr[s].second > arr[idx].second){
continue;
}
else{
pos = true;
best = min(best, dist + (1 << j) + 1);
}
}
if(arr[s].second >= arr[e].first && arr[s].second <= arr[e].second){
pos = true;
best = 1;
}
if(pos){
cout << best << '\n';
}
else{
cout << "impossible\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Correct |
348 ms |
24172 KB |
Output is correct |
3 |
Correct |
330 ms |
24092 KB |
Output is correct |
4 |
Correct |
360 ms |
24144 KB |
Output is correct |
5 |
Correct |
330 ms |
24672 KB |
Output is correct |
6 |
Correct |
346 ms |
25064 KB |
Output is correct |
7 |
Correct |
345 ms |
25296 KB |
Output is correct |
8 |
Correct |
350 ms |
29268 KB |
Output is correct |
9 |
Correct |
352 ms |
29284 KB |
Output is correct |
10 |
Correct |
328 ms |
24484 KB |
Output is correct |
11 |
Correct |
350 ms |
26356 KB |
Output is correct |
12 |
Correct |
255 ms |
23896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
6 ms |
6740 KB |
Output is correct |
4 |
Correct |
4 ms |
6740 KB |
Output is correct |
5 |
Correct |
4 ms |
6740 KB |
Output is correct |
6 |
Correct |
4 ms |
6740 KB |
Output is correct |
7 |
Correct |
5 ms |
6856 KB |
Output is correct |
8 |
Correct |
4 ms |
6868 KB |
Output is correct |
9 |
Correct |
4 ms |
6720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
6 ms |
6740 KB |
Output is correct |
4 |
Correct |
4 ms |
6740 KB |
Output is correct |
5 |
Correct |
4 ms |
6740 KB |
Output is correct |
6 |
Correct |
4 ms |
6740 KB |
Output is correct |
7 |
Correct |
5 ms |
6856 KB |
Output is correct |
8 |
Correct |
4 ms |
6868 KB |
Output is correct |
9 |
Correct |
4 ms |
6720 KB |
Output is correct |
10 |
Correct |
4 ms |
6612 KB |
Output is correct |
11 |
Correct |
4 ms |
6572 KB |
Output is correct |
12 |
Correct |
4 ms |
6740 KB |
Output is correct |
13 |
Correct |
4 ms |
6804 KB |
Output is correct |
14 |
Correct |
5 ms |
6740 KB |
Output is correct |
15 |
Correct |
4 ms |
6740 KB |
Output is correct |
16 |
Correct |
5 ms |
6868 KB |
Output is correct |
17 |
Correct |
4 ms |
6784 KB |
Output is correct |
18 |
Correct |
4 ms |
6752 KB |
Output is correct |
19 |
Correct |
156 ms |
8912 KB |
Output is correct |
20 |
Correct |
175 ms |
8928 KB |
Output is correct |
21 |
Correct |
156 ms |
9128 KB |
Output is correct |
22 |
Correct |
155 ms |
9280 KB |
Output is correct |
23 |
Correct |
158 ms |
9288 KB |
Output is correct |
24 |
Correct |
155 ms |
9360 KB |
Output is correct |
25 |
Correct |
159 ms |
9004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
6 ms |
6740 KB |
Output is correct |
4 |
Correct |
4 ms |
6740 KB |
Output is correct |
5 |
Correct |
4 ms |
6740 KB |
Output is correct |
6 |
Correct |
4 ms |
6740 KB |
Output is correct |
7 |
Correct |
5 ms |
6856 KB |
Output is correct |
8 |
Correct |
4 ms |
6868 KB |
Output is correct |
9 |
Correct |
4 ms |
6720 KB |
Output is correct |
10 |
Correct |
4 ms |
6612 KB |
Output is correct |
11 |
Correct |
4 ms |
6572 KB |
Output is correct |
12 |
Correct |
6 ms |
6716 KB |
Output is correct |
13 |
Correct |
5 ms |
6848 KB |
Output is correct |
14 |
Correct |
4 ms |
6740 KB |
Output is correct |
15 |
Correct |
5 ms |
6740 KB |
Output is correct |
16 |
Correct |
6 ms |
6868 KB |
Output is correct |
17 |
Correct |
4 ms |
6868 KB |
Output is correct |
18 |
Correct |
5 ms |
6740 KB |
Output is correct |
19 |
Correct |
182 ms |
22352 KB |
Output is correct |
20 |
Correct |
162 ms |
21540 KB |
Output is correct |
21 |
Correct |
168 ms |
22256 KB |
Output is correct |
22 |
Correct |
170 ms |
24152 KB |
Output is correct |
23 |
Correct |
275 ms |
27020 KB |
Output is correct |
24 |
Correct |
241 ms |
27036 KB |
Output is correct |
25 |
Correct |
76 ms |
16956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
344 ms |
24124 KB |
Output is correct |
2 |
Correct |
352 ms |
24088 KB |
Output is correct |
3 |
Correct |
392 ms |
24152 KB |
Output is correct |
4 |
Correct |
401 ms |
29356 KB |
Output is correct |
5 |
Correct |
366 ms |
24476 KB |
Output is correct |
6 |
Correct |
394 ms |
28916 KB |
Output is correct |
7 |
Correct |
434 ms |
28948 KB |
Output is correct |
8 |
Correct |
391 ms |
29124 KB |
Output is correct |
9 |
Correct |
195 ms |
27100 KB |
Output is correct |
10 |
Correct |
378 ms |
28548 KB |
Output is correct |
11 |
Correct |
421 ms |
28332 KB |
Output is correct |
12 |
Correct |
415 ms |
28636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Correct |
348 ms |
24172 KB |
Output is correct |
3 |
Correct |
330 ms |
24092 KB |
Output is correct |
4 |
Correct |
360 ms |
24144 KB |
Output is correct |
5 |
Correct |
330 ms |
24672 KB |
Output is correct |
6 |
Correct |
346 ms |
25064 KB |
Output is correct |
7 |
Correct |
345 ms |
25296 KB |
Output is correct |
8 |
Correct |
350 ms |
29268 KB |
Output is correct |
9 |
Correct |
352 ms |
29284 KB |
Output is correct |
10 |
Correct |
328 ms |
24484 KB |
Output is correct |
11 |
Correct |
350 ms |
26356 KB |
Output is correct |
12 |
Correct |
255 ms |
23896 KB |
Output is correct |
13 |
Correct |
3 ms |
6612 KB |
Output is correct |
14 |
Correct |
3 ms |
6612 KB |
Output is correct |
15 |
Correct |
6 ms |
6740 KB |
Output is correct |
16 |
Correct |
4 ms |
6740 KB |
Output is correct |
17 |
Correct |
4 ms |
6740 KB |
Output is correct |
18 |
Correct |
4 ms |
6740 KB |
Output is correct |
19 |
Correct |
5 ms |
6856 KB |
Output is correct |
20 |
Correct |
4 ms |
6868 KB |
Output is correct |
21 |
Correct |
4 ms |
6720 KB |
Output is correct |
22 |
Correct |
4 ms |
6612 KB |
Output is correct |
23 |
Correct |
4 ms |
6572 KB |
Output is correct |
24 |
Correct |
4 ms |
6740 KB |
Output is correct |
25 |
Correct |
4 ms |
6804 KB |
Output is correct |
26 |
Correct |
5 ms |
6740 KB |
Output is correct |
27 |
Correct |
4 ms |
6740 KB |
Output is correct |
28 |
Correct |
5 ms |
6868 KB |
Output is correct |
29 |
Correct |
4 ms |
6784 KB |
Output is correct |
30 |
Correct |
4 ms |
6752 KB |
Output is correct |
31 |
Correct |
156 ms |
8912 KB |
Output is correct |
32 |
Correct |
175 ms |
8928 KB |
Output is correct |
33 |
Correct |
156 ms |
9128 KB |
Output is correct |
34 |
Correct |
155 ms |
9280 KB |
Output is correct |
35 |
Correct |
158 ms |
9288 KB |
Output is correct |
36 |
Correct |
155 ms |
9360 KB |
Output is correct |
37 |
Correct |
159 ms |
9004 KB |
Output is correct |
38 |
Correct |
4 ms |
6612 KB |
Output is correct |
39 |
Correct |
4 ms |
6572 KB |
Output is correct |
40 |
Correct |
6 ms |
6716 KB |
Output is correct |
41 |
Correct |
5 ms |
6848 KB |
Output is correct |
42 |
Correct |
4 ms |
6740 KB |
Output is correct |
43 |
Correct |
5 ms |
6740 KB |
Output is correct |
44 |
Correct |
6 ms |
6868 KB |
Output is correct |
45 |
Correct |
4 ms |
6868 KB |
Output is correct |
46 |
Correct |
5 ms |
6740 KB |
Output is correct |
47 |
Correct |
182 ms |
22352 KB |
Output is correct |
48 |
Correct |
162 ms |
21540 KB |
Output is correct |
49 |
Correct |
168 ms |
22256 KB |
Output is correct |
50 |
Correct |
170 ms |
24152 KB |
Output is correct |
51 |
Correct |
275 ms |
27020 KB |
Output is correct |
52 |
Correct |
241 ms |
27036 KB |
Output is correct |
53 |
Correct |
76 ms |
16956 KB |
Output is correct |
54 |
Correct |
344 ms |
24124 KB |
Output is correct |
55 |
Correct |
352 ms |
24088 KB |
Output is correct |
56 |
Correct |
392 ms |
24152 KB |
Output is correct |
57 |
Correct |
401 ms |
29356 KB |
Output is correct |
58 |
Correct |
366 ms |
24476 KB |
Output is correct |
59 |
Correct |
394 ms |
28916 KB |
Output is correct |
60 |
Correct |
434 ms |
28948 KB |
Output is correct |
61 |
Correct |
391 ms |
29124 KB |
Output is correct |
62 |
Correct |
195 ms |
27100 KB |
Output is correct |
63 |
Correct |
378 ms |
28548 KB |
Output is correct |
64 |
Correct |
421 ms |
28332 KB |
Output is correct |
65 |
Correct |
415 ms |
28636 KB |
Output is correct |
66 |
Correct |
3 ms |
6612 KB |
Output is correct |
67 |
Correct |
324 ms |
24184 KB |
Output is correct |
68 |
Correct |
338 ms |
24076 KB |
Output is correct |
69 |
Correct |
374 ms |
24104 KB |
Output is correct |
70 |
Correct |
331 ms |
24664 KB |
Output is correct |
71 |
Correct |
344 ms |
25060 KB |
Output is correct |
72 |
Correct |
367 ms |
25264 KB |
Output is correct |
73 |
Correct |
365 ms |
29260 KB |
Output is correct |
74 |
Correct |
361 ms |
29312 KB |
Output is correct |
75 |
Correct |
335 ms |
24552 KB |
Output is correct |
76 |
Correct |
364 ms |
26436 KB |
Output is correct |
77 |
Correct |
262 ms |
23832 KB |
Output is correct |
78 |
Correct |
3 ms |
6640 KB |
Output is correct |
79 |
Correct |
6 ms |
6740 KB |
Output is correct |
80 |
Correct |
5 ms |
6764 KB |
Output is correct |
81 |
Correct |
4 ms |
6720 KB |
Output is correct |
82 |
Correct |
6 ms |
6740 KB |
Output is correct |
83 |
Correct |
6 ms |
6848 KB |
Output is correct |
84 |
Correct |
6 ms |
6844 KB |
Output is correct |
85 |
Correct |
5 ms |
6688 KB |
Output is correct |
86 |
Correct |
175 ms |
8956 KB |
Output is correct |
87 |
Correct |
157 ms |
8856 KB |
Output is correct |
88 |
Correct |
166 ms |
9176 KB |
Output is correct |
89 |
Correct |
161 ms |
9164 KB |
Output is correct |
90 |
Correct |
165 ms |
9348 KB |
Output is correct |
91 |
Correct |
164 ms |
9268 KB |
Output is correct |
92 |
Correct |
154 ms |
8972 KB |
Output is correct |
93 |
Correct |
162 ms |
22416 KB |
Output is correct |
94 |
Correct |
145 ms |
21560 KB |
Output is correct |
95 |
Correct |
172 ms |
22252 KB |
Output is correct |
96 |
Correct |
171 ms |
24104 KB |
Output is correct |
97 |
Correct |
218 ms |
26992 KB |
Output is correct |
98 |
Correct |
210 ms |
27032 KB |
Output is correct |
99 |
Correct |
75 ms |
16960 KB |
Output is correct |
100 |
Correct |
429 ms |
28920 KB |
Output is correct |
101 |
Correct |
419 ms |
29040 KB |
Output is correct |
102 |
Correct |
366 ms |
29132 KB |
Output is correct |
103 |
Correct |
411 ms |
28568 KB |
Output is correct |
104 |
Correct |
423 ms |
28372 KB |
Output is correct |
105 |
Correct |
434 ms |
28580 KB |
Output is correct |
106 |
Correct |
305 ms |
22420 KB |
Output is correct |
107 |
Correct |
340 ms |
23488 KB |
Output is correct |
108 |
Correct |
331 ms |
24228 KB |
Output is correct |
109 |
Correct |
362 ms |
24368 KB |
Output is correct |
110 |
Correct |
386 ms |
28832 KB |
Output is correct |
111 |
Correct |
425 ms |
28972 KB |
Output is correct |