/*input
8 5
1 2
3 4
1 5
6 7
5 10
10 20
15 20
999999999 1000000000
1 6
1 7
2 4
3 3
5 8
*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N, Q;
cin >> N >> Q;
pair<pair<int, int>, int>A[N];
for (int i = 0; i < N; i++)
{
cin >> A[i].first.second >> A[i].first.first;
A[i].second = i;
}
sort(A, A + N);
int p[N];
for (int i = 0; i < N; i++)
p[A[i].second] = i;
int S[N], E[N];
for (int i = 0; i < N; i++)
{
S[i] = A[i].first.second;
E[i] = A[i].first.first;
}
for (int i = 0; i < N; i++)
{
S[i] = (lower_bound(E, E + N, S[i]) - E);
}
// int D[N][N];
// for (int i = 0; i < N; i++)
// for (int j = 0; j < N; j++)
// D[i][j] = N + 10;
vector<int>V;
int pa[N][20];
for (int j = 0; j < N; j++)
{
if (S[j] >= j)
{
pa[j][0] = j;
}
else
{
auto it = lower_bound(V.begin(), V.end(), S[j]);
assert(it != V.end());
pa[j][0] = *it;
}
for (int i = 1; i < 20; i++)
pa[j][i] = pa[pa[j][i - 1]][i - 1];
while (!V.empty() && S[V.back()] >= S[j])
V.pop_back();
V.push_back(j);
}
while (Q--)
{
int s, t;
cin >> s >> t;
s = p[s - 1];
t = p[t - 1];
if (s == t)
{
cout << "0\n";
}
else {
if (A[t].first.second <= A[s].first.first && A[s].first.first <= A[t].first.first)
cout << "1\n";
else {
if (s >= t)
{
cout << "impossible\n";
continue;
}
int a = 1;
while (true)
{
if (s >= S[t])
{
cout << a << "\n";
break;
}
if (pa[t][0] == t)
{
cout << "impossible\n";
break;
}
for (int i = 19; i >= 0; i--)
if (s < S[pa[t][i]])
{
t = pa[t][i];
a += (1 << i);
}
t = pa[t][0];
a++;
}
// int k = D[t][s];
// if (k > N)
// cout << "impossible\n";
// else
// cout << k << "\n";
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
71 ms |
11672 KB |
Output is correct |
3 |
Correct |
88 ms |
12132 KB |
Output is correct |
4 |
Correct |
98 ms |
12140 KB |
Output is correct |
5 |
Correct |
77 ms |
12004 KB |
Output is correct |
6 |
Correct |
83 ms |
12184 KB |
Output is correct |
7 |
Correct |
81 ms |
11864 KB |
Output is correct |
8 |
Correct |
62 ms |
12776 KB |
Output is correct |
9 |
Correct |
73 ms |
13004 KB |
Output is correct |
10 |
Correct |
111 ms |
12724 KB |
Output is correct |
11 |
Correct |
89 ms |
12312 KB |
Output is correct |
12 |
Correct |
62 ms |
12116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
31 ms |
1316 KB |
Output is correct |
20 |
Correct |
26 ms |
1364 KB |
Output is correct |
21 |
Correct |
31 ms |
1556 KB |
Output is correct |
22 |
Correct |
25 ms |
1564 KB |
Output is correct |
23 |
Correct |
24 ms |
1364 KB |
Output is correct |
24 |
Correct |
19 ms |
1364 KB |
Output is correct |
25 |
Correct |
25 ms |
1132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
408 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
53 ms |
11072 KB |
Output is correct |
20 |
Correct |
40 ms |
11096 KB |
Output is correct |
21 |
Correct |
43 ms |
10436 KB |
Output is correct |
22 |
Correct |
41 ms |
10832 KB |
Output is correct |
23 |
Correct |
38 ms |
10448 KB |
Output is correct |
24 |
Correct |
47 ms |
11212 KB |
Output is correct |
25 |
Correct |
41 ms |
10440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
11672 KB |
Output is correct |
2 |
Correct |
74 ms |
11776 KB |
Output is correct |
3 |
Correct |
85 ms |
11736 KB |
Output is correct |
4 |
Correct |
66 ms |
12236 KB |
Output is correct |
5 |
Correct |
90 ms |
12132 KB |
Output is correct |
6 |
Correct |
105 ms |
11932 KB |
Output is correct |
7 |
Correct |
104 ms |
11924 KB |
Output is correct |
8 |
Correct |
111 ms |
11888 KB |
Output is correct |
9 |
Correct |
43 ms |
10752 KB |
Output is correct |
10 |
Correct |
82 ms |
11952 KB |
Output is correct |
11 |
Correct |
81 ms |
11632 KB |
Output is correct |
12 |
Correct |
81 ms |
11660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
71 ms |
11672 KB |
Output is correct |
3 |
Correct |
88 ms |
12132 KB |
Output is correct |
4 |
Correct |
98 ms |
12140 KB |
Output is correct |
5 |
Correct |
77 ms |
12004 KB |
Output is correct |
6 |
Correct |
83 ms |
12184 KB |
Output is correct |
7 |
Correct |
81 ms |
11864 KB |
Output is correct |
8 |
Correct |
62 ms |
12776 KB |
Output is correct |
9 |
Correct |
73 ms |
13004 KB |
Output is correct |
10 |
Correct |
111 ms |
12724 KB |
Output is correct |
11 |
Correct |
89 ms |
12312 KB |
Output is correct |
12 |
Correct |
62 ms |
12116 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
31 ms |
1316 KB |
Output is correct |
32 |
Correct |
26 ms |
1364 KB |
Output is correct |
33 |
Correct |
31 ms |
1556 KB |
Output is correct |
34 |
Correct |
25 ms |
1564 KB |
Output is correct |
35 |
Correct |
24 ms |
1364 KB |
Output is correct |
36 |
Correct |
19 ms |
1364 KB |
Output is correct |
37 |
Correct |
25 ms |
1132 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
408 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
53 ms |
11072 KB |
Output is correct |
48 |
Correct |
40 ms |
11096 KB |
Output is correct |
49 |
Correct |
43 ms |
10436 KB |
Output is correct |
50 |
Correct |
41 ms |
10832 KB |
Output is correct |
51 |
Correct |
38 ms |
10448 KB |
Output is correct |
52 |
Correct |
47 ms |
11212 KB |
Output is correct |
53 |
Correct |
41 ms |
10440 KB |
Output is correct |
54 |
Correct |
74 ms |
11672 KB |
Output is correct |
55 |
Correct |
74 ms |
11776 KB |
Output is correct |
56 |
Correct |
85 ms |
11736 KB |
Output is correct |
57 |
Correct |
66 ms |
12236 KB |
Output is correct |
58 |
Correct |
90 ms |
12132 KB |
Output is correct |
59 |
Correct |
105 ms |
11932 KB |
Output is correct |
60 |
Correct |
104 ms |
11924 KB |
Output is correct |
61 |
Correct |
111 ms |
11888 KB |
Output is correct |
62 |
Correct |
43 ms |
10752 KB |
Output is correct |
63 |
Correct |
82 ms |
11952 KB |
Output is correct |
64 |
Correct |
81 ms |
11632 KB |
Output is correct |
65 |
Correct |
81 ms |
11660 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
73 ms |
11804 KB |
Output is correct |
68 |
Correct |
76 ms |
12116 KB |
Output is correct |
69 |
Correct |
90 ms |
11804 KB |
Output is correct |
70 |
Correct |
75 ms |
12264 KB |
Output is correct |
71 |
Correct |
77 ms |
11852 KB |
Output is correct |
72 |
Correct |
76 ms |
12484 KB |
Output is correct |
73 |
Correct |
60 ms |
12364 KB |
Output is correct |
74 |
Correct |
65 ms |
12600 KB |
Output is correct |
75 |
Correct |
106 ms |
12332 KB |
Output is correct |
76 |
Correct |
88 ms |
11984 KB |
Output is correct |
77 |
Correct |
54 ms |
12060 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
340 KB |
Output is correct |
80 |
Correct |
1 ms |
340 KB |
Output is correct |
81 |
Correct |
1 ms |
340 KB |
Output is correct |
82 |
Correct |
1 ms |
340 KB |
Output is correct |
83 |
Correct |
1 ms |
332 KB |
Output is correct |
84 |
Correct |
1 ms |
344 KB |
Output is correct |
85 |
Correct |
1 ms |
340 KB |
Output is correct |
86 |
Correct |
36 ms |
1400 KB |
Output is correct |
87 |
Correct |
29 ms |
1376 KB |
Output is correct |
88 |
Correct |
32 ms |
1712 KB |
Output is correct |
89 |
Correct |
31 ms |
1704 KB |
Output is correct |
90 |
Correct |
41 ms |
1520 KB |
Output is correct |
91 |
Correct |
21 ms |
1484 KB |
Output is correct |
92 |
Correct |
27 ms |
1172 KB |
Output is correct |
93 |
Correct |
52 ms |
11364 KB |
Output is correct |
94 |
Correct |
49 ms |
11216 KB |
Output is correct |
95 |
Correct |
49 ms |
10580 KB |
Output is correct |
96 |
Correct |
44 ms |
10952 KB |
Output is correct |
97 |
Correct |
41 ms |
10936 KB |
Output is correct |
98 |
Correct |
53 ms |
11228 KB |
Output is correct |
99 |
Correct |
37 ms |
10544 KB |
Output is correct |
100 |
Correct |
104 ms |
12056 KB |
Output is correct |
101 |
Correct |
102 ms |
12408 KB |
Output is correct |
102 |
Correct |
97 ms |
13172 KB |
Output is correct |
103 |
Correct |
71 ms |
13304 KB |
Output is correct |
104 |
Correct |
69 ms |
12316 KB |
Output is correct |
105 |
Correct |
76 ms |
13416 KB |
Output is correct |
106 |
Correct |
89 ms |
13580 KB |
Output is correct |
107 |
Correct |
113 ms |
14024 KB |
Output is correct |
108 |
Correct |
76 ms |
13164 KB |
Output is correct |
109 |
Correct |
82 ms |
12648 KB |
Output is correct |
110 |
Correct |
82 ms |
11752 KB |
Output is correct |
111 |
Correct |
87 ms |
11552 KB |
Output is correct |