#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10 , L = 18;
int n , q , l[N] , r[N] , mv[L][N];
vector <int> vec , st;
bool cmp(int a , int b)
{
if(r[a] != r[b])
return r[a] < r[b];
return l[a] < l[b];
}
int Bs(int x)
{
int low = -1 , high = st.size();
while(high - low > 1)
{
int mid = (low + high) >> 1;
if(r[st[mid]] >= x)
high = mid;
else
low = mid;
}
return st[high];
}
int solve(int from , int to , int bit)
{
if(bit == -1)
return 0;
int nex = mv[bit][from];
if(l[nex] > r[to])
return (1 << bit) + solve(nex , to , bit - 1);
return solve(from , to , bit - 1);
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> q;
for(int i = 1 ; i <= n ; i++)
{
cin >> l[i] >> r[i];
vec.push_back(i);
}
sort(vec.begin() , vec.end() , cmp);
for(int i = 1 ; i <= n ; i++)
{
int now = vec[i];
while(!st.empty() && l[st.back()] >= l[now])
st.pop_back();
st.push_back(now);
mv[0][now] = Bs(l[now]);
}
for(int i = 1 ; i < L ; i++) for(int j = 1 ; j <= n ; j++)
mv[i][j] = mv[i - 1][mv[i - 1][j]];
while(q--)
{
int s , e; cin >> s >> e;
if(l[mv[L - 1][e]] > r[s] || r[e] < r[s])
{
cout << "impossible" << '\n';
continue;
}
if(s == e)
{
cout << 0 << '\n';
continue;
}
if(l[e] <= r[s])
{
cout << 1 << '\n';
continue;
}
cout << solve(e , s , L - 1) + 2 << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
63 ms |
12796 KB |
Output is correct |
3 |
Correct |
76 ms |
12724 KB |
Output is correct |
4 |
Correct |
90 ms |
12732 KB |
Output is correct |
5 |
Correct |
78 ms |
12760 KB |
Output is correct |
6 |
Correct |
75 ms |
12748 KB |
Output is correct |
7 |
Correct |
74 ms |
12724 KB |
Output is correct |
8 |
Correct |
65 ms |
13236 KB |
Output is correct |
9 |
Correct |
58 ms |
13240 KB |
Output is correct |
10 |
Correct |
74 ms |
13108 KB |
Output is correct |
11 |
Correct |
73 ms |
13144 KB |
Output is correct |
12 |
Correct |
43 ms |
12360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
476 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
476 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
472 KB |
Output is correct |
13 |
Correct |
1 ms |
472 KB |
Output is correct |
14 |
Correct |
1 ms |
472 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
472 KB |
Output is correct |
19 |
Correct |
25 ms |
2260 KB |
Output is correct |
20 |
Correct |
24 ms |
2388 KB |
Output is correct |
21 |
Correct |
27 ms |
2584 KB |
Output is correct |
22 |
Correct |
24 ms |
2604 KB |
Output is correct |
23 |
Correct |
20 ms |
2476 KB |
Output is correct |
24 |
Correct |
20 ms |
2512 KB |
Output is correct |
25 |
Correct |
22 ms |
2132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
476 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
472 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
42 ms |
10960 KB |
Output is correct |
20 |
Correct |
38 ms |
10100 KB |
Output is correct |
21 |
Correct |
33 ms |
10400 KB |
Output is correct |
22 |
Correct |
41 ms |
10964 KB |
Output is correct |
23 |
Correct |
38 ms |
11004 KB |
Output is correct |
24 |
Correct |
42 ms |
10976 KB |
Output is correct |
25 |
Correct |
19 ms |
9832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
12868 KB |
Output is correct |
2 |
Correct |
81 ms |
12736 KB |
Output is correct |
3 |
Correct |
89 ms |
12828 KB |
Output is correct |
4 |
Correct |
61 ms |
13140 KB |
Output is correct |
5 |
Correct |
74 ms |
13128 KB |
Output is correct |
6 |
Correct |
73 ms |
12900 KB |
Output is correct |
7 |
Correct |
75 ms |
12828 KB |
Output is correct |
8 |
Correct |
69 ms |
12956 KB |
Output is correct |
9 |
Correct |
38 ms |
10968 KB |
Output is correct |
10 |
Correct |
70 ms |
12456 KB |
Output is correct |
11 |
Correct |
68 ms |
12276 KB |
Output is correct |
12 |
Correct |
78 ms |
12668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
63 ms |
12796 KB |
Output is correct |
3 |
Correct |
76 ms |
12724 KB |
Output is correct |
4 |
Correct |
90 ms |
12732 KB |
Output is correct |
5 |
Correct |
78 ms |
12760 KB |
Output is correct |
6 |
Correct |
75 ms |
12748 KB |
Output is correct |
7 |
Correct |
74 ms |
12724 KB |
Output is correct |
8 |
Correct |
65 ms |
13236 KB |
Output is correct |
9 |
Correct |
58 ms |
13240 KB |
Output is correct |
10 |
Correct |
74 ms |
13108 KB |
Output is correct |
11 |
Correct |
73 ms |
13144 KB |
Output is correct |
12 |
Correct |
43 ms |
12360 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
476 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
472 KB |
Output is correct |
25 |
Correct |
1 ms |
472 KB |
Output is correct |
26 |
Correct |
1 ms |
472 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
472 KB |
Output is correct |
31 |
Correct |
25 ms |
2260 KB |
Output is correct |
32 |
Correct |
24 ms |
2388 KB |
Output is correct |
33 |
Correct |
27 ms |
2584 KB |
Output is correct |
34 |
Correct |
24 ms |
2604 KB |
Output is correct |
35 |
Correct |
20 ms |
2476 KB |
Output is correct |
36 |
Correct |
20 ms |
2512 KB |
Output is correct |
37 |
Correct |
22 ms |
2132 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
328 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
472 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
42 ms |
10960 KB |
Output is correct |
48 |
Correct |
38 ms |
10100 KB |
Output is correct |
49 |
Correct |
33 ms |
10400 KB |
Output is correct |
50 |
Correct |
41 ms |
10964 KB |
Output is correct |
51 |
Correct |
38 ms |
11004 KB |
Output is correct |
52 |
Correct |
42 ms |
10976 KB |
Output is correct |
53 |
Correct |
19 ms |
9832 KB |
Output is correct |
54 |
Correct |
67 ms |
12868 KB |
Output is correct |
55 |
Correct |
81 ms |
12736 KB |
Output is correct |
56 |
Correct |
89 ms |
12828 KB |
Output is correct |
57 |
Correct |
61 ms |
13140 KB |
Output is correct |
58 |
Correct |
74 ms |
13128 KB |
Output is correct |
59 |
Correct |
73 ms |
12900 KB |
Output is correct |
60 |
Correct |
75 ms |
12828 KB |
Output is correct |
61 |
Correct |
69 ms |
12956 KB |
Output is correct |
62 |
Correct |
38 ms |
10968 KB |
Output is correct |
63 |
Correct |
70 ms |
12456 KB |
Output is correct |
64 |
Correct |
68 ms |
12276 KB |
Output is correct |
65 |
Correct |
78 ms |
12668 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
64 ms |
12764 KB |
Output is correct |
68 |
Correct |
78 ms |
12756 KB |
Output is correct |
69 |
Correct |
78 ms |
12844 KB |
Output is correct |
70 |
Correct |
75 ms |
12752 KB |
Output is correct |
71 |
Correct |
83 ms |
12720 KB |
Output is correct |
72 |
Correct |
70 ms |
12760 KB |
Output is correct |
73 |
Correct |
58 ms |
13196 KB |
Output is correct |
74 |
Correct |
59 ms |
13144 KB |
Output is correct |
75 |
Correct |
72 ms |
13116 KB |
Output is correct |
76 |
Correct |
78 ms |
13288 KB |
Output is correct |
77 |
Correct |
44 ms |
12432 KB |
Output is correct |
78 |
Correct |
1 ms |
316 KB |
Output is correct |
79 |
Correct |
1 ms |
472 KB |
Output is correct |
80 |
Correct |
1 ms |
468 KB |
Output is correct |
81 |
Correct |
1 ms |
468 KB |
Output is correct |
82 |
Correct |
1 ms |
468 KB |
Output is correct |
83 |
Correct |
1 ms |
468 KB |
Output is correct |
84 |
Correct |
1 ms |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
26 ms |
2244 KB |
Output is correct |
87 |
Correct |
24 ms |
2392 KB |
Output is correct |
88 |
Correct |
28 ms |
2584 KB |
Output is correct |
89 |
Correct |
20 ms |
2764 KB |
Output is correct |
90 |
Correct |
24 ms |
2540 KB |
Output is correct |
91 |
Correct |
19 ms |
2516 KB |
Output is correct |
92 |
Correct |
29 ms |
2096 KB |
Output is correct |
93 |
Correct |
41 ms |
11044 KB |
Output is correct |
94 |
Correct |
41 ms |
10212 KB |
Output is correct |
95 |
Correct |
50 ms |
10400 KB |
Output is correct |
96 |
Correct |
41 ms |
10876 KB |
Output is correct |
97 |
Correct |
42 ms |
10988 KB |
Output is correct |
98 |
Correct |
42 ms |
10880 KB |
Output is correct |
99 |
Correct |
19 ms |
9924 KB |
Output is correct |
100 |
Correct |
82 ms |
12844 KB |
Output is correct |
101 |
Correct |
82 ms |
12884 KB |
Output is correct |
102 |
Correct |
73 ms |
12992 KB |
Output is correct |
103 |
Correct |
75 ms |
12608 KB |
Output is correct |
104 |
Correct |
80 ms |
12348 KB |
Output is correct |
105 |
Correct |
78 ms |
12536 KB |
Output is correct |
106 |
Correct |
72 ms |
12108 KB |
Output is correct |
107 |
Correct |
83 ms |
12160 KB |
Output is correct |
108 |
Correct |
55 ms |
12396 KB |
Output is correct |
109 |
Correct |
57 ms |
12412 KB |
Output is correct |
110 |
Correct |
59 ms |
12312 KB |
Output is correct |
111 |
Correct |
54 ms |
12400 KB |
Output is correct |