#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
const int maxn = 2e5 + 2;
const int lg = 20;
struct node {
int l,r;
pair <int, int> v;
node* left;
node* right;
node(int _l, int _r, pair <int, int> _v): l(_l), r(_r), v(_v) {};
inline void extend() {
if (left != NULL) return;
int mid = (l + r) / 2;
left = new node(l, mid, {1e9, -1});
right = new node(mid + 1, r, {1e9, -1});
}
inline pair <int, int> query (int il, int ir) {
if (il <= l && r <= ir) return v;
if (l > ir || r < il) return {1e9, -1};
extend();
return min(left->query(il, ir), right->query(il, ir));
}
inline void update (int x, pair <int, int> nval) {
if (l == r && l == x) return void(v = min(nval, v));
if (l > x || r < x) return;
extend();
left->update(x, nval);
right->update(x, nval);
v = min(left->v, right->v);
}
};
struct nd {
int x;
int y;
} a [maxn];
int bin[maxn][lg];
int n,q;
int ps[maxn];
node *sgt = new node(1, 1e9, {1e15, -1});
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> q;
for (int i = 0; i < n; i++){
cin >> a[i].x >> a[i].y;
}
for (int i = 0; i < n; i++){
sgt->update(a[i].y, {a[i].x, i});
}
for (int i = 0; i < n; i++){
bin[i][0] = sgt->query(a[i].x, a[i].y).second;
}
for (int k = 1; k < lg; k++){
for (int i = 0; i < n; i++){
bin[i][k] = bin[bin[i][k-1]][k-1];
}
}
while (q--){
int x,y, rs = 2;
cin >> x >> y;
x--, y--;
if (x == y) cout << "0\n";
else if (a[x].y > a[y].y) cout << "impossible\n";
else if (a[x].y >= a[y].x) cout << "1\n";
else {
for (int i = lg - 1; i >= 0; i--){
if (a[bin[y][i]].x > a[x].y) y = bin[y][i], rs+=(1 << i);
}
if (a[bin[y][0]].x > a[x].y) cout << "impossible\n";
else cout << rs << "\n";
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
439 ms |
129572 KB |
Output is correct |
3 |
Correct |
419 ms |
133096 KB |
Output is correct |
4 |
Correct |
514 ms |
135728 KB |
Output is correct |
5 |
Correct |
484 ms |
145568 KB |
Output is correct |
6 |
Correct |
466 ms |
152784 KB |
Output is correct |
7 |
Correct |
445 ms |
149248 KB |
Output is correct |
8 |
Correct |
479 ms |
203084 KB |
Output is correct |
9 |
Correct |
475 ms |
235764 KB |
Output is correct |
10 |
Correct |
380 ms |
126848 KB |
Output is correct |
11 |
Correct |
404 ms |
154364 KB |
Output is correct |
12 |
Correct |
125 ms |
28280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
236 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
5 ms |
2320 KB |
Output is correct |
4 |
Correct |
3 ms |
2152 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
1260 KB |
Output is correct |
7 |
Correct |
5 ms |
3924 KB |
Output is correct |
8 |
Correct |
5 ms |
3824 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
236 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
5 ms |
2320 KB |
Output is correct |
4 |
Correct |
3 ms |
2152 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
1260 KB |
Output is correct |
7 |
Correct |
5 ms |
3924 KB |
Output is correct |
8 |
Correct |
5 ms |
3824 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
5 ms |
2156 KB |
Output is correct |
13 |
Correct |
3 ms |
2260 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
1364 KB |
Output is correct |
16 |
Correct |
7 ms |
3924 KB |
Output is correct |
17 |
Correct |
6 ms |
3924 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
44 ms |
2784 KB |
Output is correct |
20 |
Correct |
40 ms |
9884 KB |
Output is correct |
21 |
Correct |
32 ms |
2912 KB |
Output is correct |
22 |
Correct |
31 ms |
6588 KB |
Output is correct |
23 |
Correct |
40 ms |
17612 KB |
Output is correct |
24 |
Correct |
42 ms |
17752 KB |
Output is correct |
25 |
Correct |
47 ms |
17312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
236 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
5 ms |
2320 KB |
Output is correct |
4 |
Correct |
3 ms |
2152 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
1260 KB |
Output is correct |
7 |
Correct |
5 ms |
3924 KB |
Output is correct |
8 |
Correct |
5 ms |
3824 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
2132 KB |
Output is correct |
13 |
Correct |
4 ms |
2260 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
1256 KB |
Output is correct |
16 |
Correct |
5 ms |
3924 KB |
Output is correct |
17 |
Correct |
5 ms |
3924 KB |
Output is correct |
18 |
Correct |
1 ms |
356 KB |
Output is correct |
19 |
Correct |
341 ms |
129440 KB |
Output is correct |
20 |
Correct |
159 ms |
19544 KB |
Output is correct |
21 |
Correct |
307 ms |
70492 KB |
Output is correct |
22 |
Correct |
410 ms |
142328 KB |
Output is correct |
23 |
Correct |
474 ms |
234340 KB |
Output is correct |
24 |
Correct |
467 ms |
234304 KB |
Output is correct |
25 |
Correct |
49 ms |
10060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
358 ms |
129504 KB |
Output is correct |
2 |
Correct |
390 ms |
133076 KB |
Output is correct |
3 |
Correct |
422 ms |
135744 KB |
Output is correct |
4 |
Correct |
448 ms |
235732 KB |
Output is correct |
5 |
Correct |
386 ms |
126816 KB |
Output is correct |
6 |
Correct |
512 ms |
235384 KB |
Output is correct |
7 |
Correct |
536 ms |
235300 KB |
Output is correct |
8 |
Correct |
507 ms |
235572 KB |
Output is correct |
9 |
Correct |
483 ms |
234644 KB |
Output is correct |
10 |
Correct |
505 ms |
235056 KB |
Output is correct |
11 |
Correct |
481 ms |
234844 KB |
Output is correct |
12 |
Correct |
519 ms |
234908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
439 ms |
129572 KB |
Output is correct |
3 |
Correct |
419 ms |
133096 KB |
Output is correct |
4 |
Correct |
514 ms |
135728 KB |
Output is correct |
5 |
Correct |
484 ms |
145568 KB |
Output is correct |
6 |
Correct |
466 ms |
152784 KB |
Output is correct |
7 |
Correct |
445 ms |
149248 KB |
Output is correct |
8 |
Correct |
479 ms |
203084 KB |
Output is correct |
9 |
Correct |
475 ms |
235764 KB |
Output is correct |
10 |
Correct |
380 ms |
126848 KB |
Output is correct |
11 |
Correct |
404 ms |
154364 KB |
Output is correct |
12 |
Correct |
125 ms |
28280 KB |
Output is correct |
13 |
Correct |
0 ms |
236 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
5 ms |
2320 KB |
Output is correct |
16 |
Correct |
3 ms |
2152 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
3 ms |
1260 KB |
Output is correct |
19 |
Correct |
5 ms |
3924 KB |
Output is correct |
20 |
Correct |
5 ms |
3824 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
5 ms |
2156 KB |
Output is correct |
25 |
Correct |
3 ms |
2260 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
3 ms |
1364 KB |
Output is correct |
28 |
Correct |
7 ms |
3924 KB |
Output is correct |
29 |
Correct |
6 ms |
3924 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
44 ms |
2784 KB |
Output is correct |
32 |
Correct |
40 ms |
9884 KB |
Output is correct |
33 |
Correct |
32 ms |
2912 KB |
Output is correct |
34 |
Correct |
31 ms |
6588 KB |
Output is correct |
35 |
Correct |
40 ms |
17612 KB |
Output is correct |
36 |
Correct |
42 ms |
17752 KB |
Output is correct |
37 |
Correct |
47 ms |
17312 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
3 ms |
2132 KB |
Output is correct |
41 |
Correct |
4 ms |
2260 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
3 ms |
1256 KB |
Output is correct |
44 |
Correct |
5 ms |
3924 KB |
Output is correct |
45 |
Correct |
5 ms |
3924 KB |
Output is correct |
46 |
Correct |
1 ms |
356 KB |
Output is correct |
47 |
Correct |
341 ms |
129440 KB |
Output is correct |
48 |
Correct |
159 ms |
19544 KB |
Output is correct |
49 |
Correct |
307 ms |
70492 KB |
Output is correct |
50 |
Correct |
410 ms |
142328 KB |
Output is correct |
51 |
Correct |
474 ms |
234340 KB |
Output is correct |
52 |
Correct |
467 ms |
234304 KB |
Output is correct |
53 |
Correct |
49 ms |
10060 KB |
Output is correct |
54 |
Correct |
358 ms |
129504 KB |
Output is correct |
55 |
Correct |
390 ms |
133076 KB |
Output is correct |
56 |
Correct |
422 ms |
135744 KB |
Output is correct |
57 |
Correct |
448 ms |
235732 KB |
Output is correct |
58 |
Correct |
386 ms |
126816 KB |
Output is correct |
59 |
Correct |
512 ms |
235384 KB |
Output is correct |
60 |
Correct |
536 ms |
235300 KB |
Output is correct |
61 |
Correct |
507 ms |
235572 KB |
Output is correct |
62 |
Correct |
483 ms |
234644 KB |
Output is correct |
63 |
Correct |
505 ms |
235056 KB |
Output is correct |
64 |
Correct |
481 ms |
234844 KB |
Output is correct |
65 |
Correct |
519 ms |
234908 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
375 ms |
128968 KB |
Output is correct |
68 |
Correct |
367 ms |
133168 KB |
Output is correct |
69 |
Correct |
398 ms |
135844 KB |
Output is correct |
70 |
Correct |
425 ms |
145660 KB |
Output is correct |
71 |
Correct |
392 ms |
152884 KB |
Output is correct |
72 |
Correct |
409 ms |
149268 KB |
Output is correct |
73 |
Correct |
415 ms |
203100 KB |
Output is correct |
74 |
Correct |
420 ms |
235792 KB |
Output is correct |
75 |
Correct |
400 ms |
126872 KB |
Output is correct |
76 |
Correct |
440 ms |
154324 KB |
Output is correct |
77 |
Correct |
121 ms |
28228 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
3 ms |
2132 KB |
Output is correct |
80 |
Correct |
4 ms |
2260 KB |
Output is correct |
81 |
Correct |
1 ms |
468 KB |
Output is correct |
82 |
Correct |
3 ms |
1364 KB |
Output is correct |
83 |
Correct |
6 ms |
3816 KB |
Output is correct |
84 |
Correct |
5 ms |
3924 KB |
Output is correct |
85 |
Correct |
1 ms |
364 KB |
Output is correct |
86 |
Correct |
45 ms |
2816 KB |
Output is correct |
87 |
Correct |
42 ms |
9932 KB |
Output is correct |
88 |
Correct |
31 ms |
2960 KB |
Output is correct |
89 |
Correct |
33 ms |
6524 KB |
Output is correct |
90 |
Correct |
44 ms |
17680 KB |
Output is correct |
91 |
Correct |
42 ms |
17712 KB |
Output is correct |
92 |
Correct |
50 ms |
17332 KB |
Output is correct |
93 |
Correct |
331 ms |
129772 KB |
Output is correct |
94 |
Correct |
136 ms |
19472 KB |
Output is correct |
95 |
Correct |
350 ms |
70848 KB |
Output is correct |
96 |
Correct |
363 ms |
142592 KB |
Output is correct |
97 |
Correct |
487 ms |
234552 KB |
Output is correct |
98 |
Correct |
544 ms |
234508 KB |
Output is correct |
99 |
Correct |
43 ms |
10228 KB |
Output is correct |
100 |
Correct |
509 ms |
235332 KB |
Output is correct |
101 |
Correct |
532 ms |
235484 KB |
Output is correct |
102 |
Correct |
497 ms |
235608 KB |
Output is correct |
103 |
Correct |
512 ms |
235020 KB |
Output is correct |
104 |
Correct |
494 ms |
234876 KB |
Output is correct |
105 |
Correct |
523 ms |
234956 KB |
Output is correct |
106 |
Correct |
192 ms |
18376 KB |
Output is correct |
107 |
Correct |
171 ms |
20792 KB |
Output is correct |
108 |
Correct |
337 ms |
71872 KB |
Output is correct |
109 |
Correct |
341 ms |
71768 KB |
Output is correct |
110 |
Correct |
546 ms |
235172 KB |
Output is correct |
111 |
Correct |
509 ms |
235396 KB |
Output is correct |