#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5;
const int MAX_LOG_N = 16;
const int MAX_T = 2e5;
const int MAX_LOG_T = 17;
struct valpos {
int val, pos;
bool operator < ( const valpos &x ) const {
return val < x.val;
}
};
struct event {
int i, s, e;
};
int prv[MAX_LOG_N + 1][MAX_N + 1];
valpos minim[MAX_LOG_T + 1][MAX_T + 1];
event events[MAX_N + 1];
map<int, int> nrm;
bool canSwitch( event a, event b ) {
return (b.s <= a.e && a.e <= b.e);
}
valpos queryMinim( int l, int r ) {
int p = log2( r - l + 1 );
return min( minim[p][l], minim[p][r - (1 << p) + 1] );
}
int main() {
int n, q;
cin >> n >> q;
for ( int i = 1; i <= n; i++ ) {
cin >> events[i].s >> events[i].e;
nrm[events[i].s] = nrm[events[i].e] = 1;
events[i].i = i + 1;
}
int t = 0;
for ( auto p: nrm )
nrm[p.first] = ++t;
for ( int i = 1; i <= n; i++ ) {
events[i].s = nrm[events[i].s];
events[i].e = nrm[events[i].e];
}
for ( int i = 1; i <= t; i++ )
minim[0][i] = { t + 1, 0 };
for ( int i = 1; i <= n; i++ )
minim[0][events[i].e] = min( minim[0][events[i].e], { events[i].s, i } );
for ( int p = 1; p <= log2( t ); p++ ) {
for ( int i = 1; i <= t - (1 << p) + 1; i++ )
minim[p][i] = min( minim[p - 1][i], minim[p - 1][i + (1 << (p - 1))] );
}
for ( int i = 1; i <= n; i++ )
prv[0][i] = queryMinim( events[i].s, events[i].e ).pos;
for ( int p = 1; p <= log2( n ); p++ ) {
for ( int i = 1; i <= n; i++ )
prv[p][i] = prv[p - 1][prv[p - 1][i]];
}
while ( q-- ) {
int s, e;
cin >> s >> e;
int i = e, x = 0;
for ( int p = log2( n ); p >= 0; p-- ) {
if ( events[prv[p][i]].s > events[s].e ) {
i = prv[p][i];
x += (1 << p);
}
}
if ( events[i].s > events[s].e ) {
i = prv[0][i];
x++;
}
if ( s == e )
cout << 0 << "\n";
else if ( canSwitch( events[s], events[i] ) )
cout << x + 1 << "\n";
else
cout << "impossible\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
303 ms |
40600 KB |
Output is correct |
3 |
Correct |
308 ms |
42736 KB |
Output is correct |
4 |
Correct |
381 ms |
42792 KB |
Output is correct |
5 |
Correct |
315 ms |
43092 KB |
Output is correct |
6 |
Correct |
321 ms |
43640 KB |
Output is correct |
7 |
Correct |
328 ms |
43688 KB |
Output is correct |
8 |
Correct |
346 ms |
49776 KB |
Output is correct |
9 |
Correct |
332 ms |
50008 KB |
Output is correct |
10 |
Correct |
311 ms |
43092 KB |
Output is correct |
11 |
Correct |
320 ms |
46932 KB |
Output is correct |
12 |
Correct |
259 ms |
42624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
4 ms |
21012 KB |
Output is correct |
4 |
Correct |
4 ms |
20828 KB |
Output is correct |
5 |
Correct |
3 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20828 KB |
Output is correct |
7 |
Correct |
4 ms |
22876 KB |
Output is correct |
8 |
Correct |
5 ms |
23096 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
4 ms |
21012 KB |
Output is correct |
4 |
Correct |
4 ms |
20828 KB |
Output is correct |
5 |
Correct |
3 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20828 KB |
Output is correct |
7 |
Correct |
4 ms |
22876 KB |
Output is correct |
8 |
Correct |
5 ms |
23096 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
4 ms |
20828 KB |
Output is correct |
13 |
Correct |
4 ms |
20824 KB |
Output is correct |
14 |
Correct |
3 ms |
20824 KB |
Output is correct |
15 |
Correct |
4 ms |
20824 KB |
Output is correct |
16 |
Correct |
4 ms |
22876 KB |
Output is correct |
17 |
Correct |
4 ms |
22876 KB |
Output is correct |
18 |
Correct |
2 ms |
8540 KB |
Output is correct |
19 |
Correct |
165 ms |
27840 KB |
Output is correct |
20 |
Correct |
164 ms |
27836 KB |
Output is correct |
21 |
Correct |
165 ms |
27992 KB |
Output is correct |
22 |
Correct |
160 ms |
28088 KB |
Output is correct |
23 |
Correct |
164 ms |
30424 KB |
Output is correct |
24 |
Correct |
163 ms |
30288 KB |
Output is correct |
25 |
Correct |
198 ms |
29976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
4 ms |
21012 KB |
Output is correct |
4 |
Correct |
4 ms |
20828 KB |
Output is correct |
5 |
Correct |
3 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20828 KB |
Output is correct |
7 |
Correct |
4 ms |
22876 KB |
Output is correct |
8 |
Correct |
5 ms |
23096 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8536 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
4 ms |
20828 KB |
Output is correct |
13 |
Correct |
4 ms |
20876 KB |
Output is correct |
14 |
Correct |
3 ms |
20828 KB |
Output is correct |
15 |
Correct |
4 ms |
20828 KB |
Output is correct |
16 |
Correct |
5 ms |
22876 KB |
Output is correct |
17 |
Correct |
4 ms |
22876 KB |
Output is correct |
18 |
Correct |
2 ms |
8540 KB |
Output is correct |
19 |
Correct |
141 ms |
39048 KB |
Output is correct |
20 |
Correct |
120 ms |
40020 KB |
Output is correct |
21 |
Correct |
153 ms |
40724 KB |
Output is correct |
22 |
Correct |
160 ms |
44576 KB |
Output is correct |
23 |
Correct |
175 ms |
47700 KB |
Output is correct |
24 |
Correct |
179 ms |
47696 KB |
Output is correct |
25 |
Correct |
46 ms |
12872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
298 ms |
39508 KB |
Output is correct |
2 |
Correct |
326 ms |
42772 KB |
Output is correct |
3 |
Correct |
327 ms |
42660 KB |
Output is correct |
4 |
Correct |
346 ms |
50156 KB |
Output is correct |
5 |
Correct |
335 ms |
42948 KB |
Output is correct |
6 |
Correct |
410 ms |
49572 KB |
Output is correct |
7 |
Correct |
410 ms |
47444 KB |
Output is correct |
8 |
Correct |
379 ms |
49920 KB |
Output is correct |
9 |
Correct |
212 ms |
47576 KB |
Output is correct |
10 |
Correct |
377 ms |
49284 KB |
Output is correct |
11 |
Correct |
405 ms |
48972 KB |
Output is correct |
12 |
Correct |
365 ms |
49236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
303 ms |
40600 KB |
Output is correct |
3 |
Correct |
308 ms |
42736 KB |
Output is correct |
4 |
Correct |
381 ms |
42792 KB |
Output is correct |
5 |
Correct |
315 ms |
43092 KB |
Output is correct |
6 |
Correct |
321 ms |
43640 KB |
Output is correct |
7 |
Correct |
328 ms |
43688 KB |
Output is correct |
8 |
Correct |
346 ms |
49776 KB |
Output is correct |
9 |
Correct |
332 ms |
50008 KB |
Output is correct |
10 |
Correct |
311 ms |
43092 KB |
Output is correct |
11 |
Correct |
320 ms |
46932 KB |
Output is correct |
12 |
Correct |
259 ms |
42624 KB |
Output is correct |
13 |
Correct |
1 ms |
8540 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
4 ms |
21012 KB |
Output is correct |
16 |
Correct |
4 ms |
20828 KB |
Output is correct |
17 |
Correct |
3 ms |
20828 KB |
Output is correct |
18 |
Correct |
4 ms |
20828 KB |
Output is correct |
19 |
Correct |
4 ms |
22876 KB |
Output is correct |
20 |
Correct |
5 ms |
23096 KB |
Output is correct |
21 |
Correct |
2 ms |
8540 KB |
Output is correct |
22 |
Correct |
1 ms |
8540 KB |
Output is correct |
23 |
Correct |
1 ms |
8540 KB |
Output is correct |
24 |
Correct |
4 ms |
20828 KB |
Output is correct |
25 |
Correct |
4 ms |
20824 KB |
Output is correct |
26 |
Correct |
3 ms |
20824 KB |
Output is correct |
27 |
Correct |
4 ms |
20824 KB |
Output is correct |
28 |
Correct |
4 ms |
22876 KB |
Output is correct |
29 |
Correct |
4 ms |
22876 KB |
Output is correct |
30 |
Correct |
2 ms |
8540 KB |
Output is correct |
31 |
Correct |
165 ms |
27840 KB |
Output is correct |
32 |
Correct |
164 ms |
27836 KB |
Output is correct |
33 |
Correct |
165 ms |
27992 KB |
Output is correct |
34 |
Correct |
160 ms |
28088 KB |
Output is correct |
35 |
Correct |
164 ms |
30424 KB |
Output is correct |
36 |
Correct |
163 ms |
30288 KB |
Output is correct |
37 |
Correct |
198 ms |
29976 KB |
Output is correct |
38 |
Correct |
2 ms |
8536 KB |
Output is correct |
39 |
Correct |
1 ms |
8540 KB |
Output is correct |
40 |
Correct |
4 ms |
20828 KB |
Output is correct |
41 |
Correct |
4 ms |
20876 KB |
Output is correct |
42 |
Correct |
3 ms |
20828 KB |
Output is correct |
43 |
Correct |
4 ms |
20828 KB |
Output is correct |
44 |
Correct |
5 ms |
22876 KB |
Output is correct |
45 |
Correct |
4 ms |
22876 KB |
Output is correct |
46 |
Correct |
2 ms |
8540 KB |
Output is correct |
47 |
Correct |
141 ms |
39048 KB |
Output is correct |
48 |
Correct |
120 ms |
40020 KB |
Output is correct |
49 |
Correct |
153 ms |
40724 KB |
Output is correct |
50 |
Correct |
160 ms |
44576 KB |
Output is correct |
51 |
Correct |
175 ms |
47700 KB |
Output is correct |
52 |
Correct |
179 ms |
47696 KB |
Output is correct |
53 |
Correct |
46 ms |
12872 KB |
Output is correct |
54 |
Correct |
298 ms |
39508 KB |
Output is correct |
55 |
Correct |
326 ms |
42772 KB |
Output is correct |
56 |
Correct |
327 ms |
42660 KB |
Output is correct |
57 |
Correct |
346 ms |
50156 KB |
Output is correct |
58 |
Correct |
335 ms |
42948 KB |
Output is correct |
59 |
Correct |
410 ms |
49572 KB |
Output is correct |
60 |
Correct |
410 ms |
47444 KB |
Output is correct |
61 |
Correct |
379 ms |
49920 KB |
Output is correct |
62 |
Correct |
212 ms |
47576 KB |
Output is correct |
63 |
Correct |
377 ms |
49284 KB |
Output is correct |
64 |
Correct |
405 ms |
48972 KB |
Output is correct |
65 |
Correct |
365 ms |
49236 KB |
Output is correct |
66 |
Correct |
1 ms |
8540 KB |
Output is correct |
67 |
Correct |
341 ms |
42656 KB |
Output is correct |
68 |
Correct |
339 ms |
42736 KB |
Output is correct |
69 |
Correct |
338 ms |
42704 KB |
Output is correct |
70 |
Correct |
341 ms |
43080 KB |
Output is correct |
71 |
Correct |
331 ms |
43588 KB |
Output is correct |
72 |
Correct |
330 ms |
43824 KB |
Output is correct |
73 |
Correct |
357 ms |
50004 KB |
Output is correct |
74 |
Correct |
357 ms |
49808 KB |
Output is correct |
75 |
Correct |
308 ms |
43096 KB |
Output is correct |
76 |
Correct |
327 ms |
47056 KB |
Output is correct |
77 |
Correct |
247 ms |
42480 KB |
Output is correct |
78 |
Correct |
1 ms |
8536 KB |
Output is correct |
79 |
Correct |
3 ms |
21000 KB |
Output is correct |
80 |
Correct |
4 ms |
20828 KB |
Output is correct |
81 |
Correct |
4 ms |
20828 KB |
Output is correct |
82 |
Correct |
4 ms |
20828 KB |
Output is correct |
83 |
Correct |
4 ms |
23132 KB |
Output is correct |
84 |
Correct |
5 ms |
22988 KB |
Output is correct |
85 |
Correct |
2 ms |
8540 KB |
Output is correct |
86 |
Correct |
198 ms |
28752 KB |
Output is correct |
87 |
Correct |
179 ms |
28752 KB |
Output is correct |
88 |
Correct |
168 ms |
28952 KB |
Output is correct |
89 |
Correct |
161 ms |
29012 KB |
Output is correct |
90 |
Correct |
162 ms |
31276 KB |
Output is correct |
91 |
Correct |
163 ms |
31312 KB |
Output is correct |
92 |
Correct |
163 ms |
30800 KB |
Output is correct |
93 |
Correct |
140 ms |
41296 KB |
Output is correct |
94 |
Correct |
119 ms |
40064 KB |
Output is correct |
95 |
Correct |
142 ms |
40688 KB |
Output is correct |
96 |
Correct |
152 ms |
44584 KB |
Output is correct |
97 |
Correct |
172 ms |
47700 KB |
Output is correct |
98 |
Correct |
211 ms |
47528 KB |
Output is correct |
99 |
Correct |
49 ms |
12884 KB |
Output is correct |
100 |
Correct |
366 ms |
49532 KB |
Output is correct |
101 |
Correct |
358 ms |
49488 KB |
Output is correct |
102 |
Correct |
379 ms |
49740 KB |
Output is correct |
103 |
Correct |
367 ms |
49228 KB |
Output is correct |
104 |
Correct |
374 ms |
49060 KB |
Output is correct |
105 |
Correct |
368 ms |
49236 KB |
Output is correct |
106 |
Correct |
305 ms |
41044 KB |
Output is correct |
107 |
Correct |
333 ms |
42152 KB |
Output is correct |
108 |
Correct |
347 ms |
42900 KB |
Output is correct |
109 |
Correct |
313 ms |
42828 KB |
Output is correct |
110 |
Correct |
481 ms |
49412 KB |
Output is correct |
111 |
Correct |
415 ms |
49480 KB |
Output is correct |