/*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;
for (int i = 19; i >= 0; i--)
if (s < S[pa[t][i]])
{
t = pa[t][i];
a += (1 << i);
}
if (s < S[t])
{
t = pa[t][0];
a++;
}
if (s >= S[t])
{
cout << a << "\n";
}
else
{
cout << "impossible\n";
}
// 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 |
69 ms |
11592 KB |
Output is correct |
3 |
Correct |
68 ms |
11568 KB |
Output is correct |
4 |
Correct |
84 ms |
11624 KB |
Output is correct |
5 |
Correct |
73 ms |
11584 KB |
Output is correct |
6 |
Correct |
82 ms |
11600 KB |
Output is correct |
7 |
Correct |
69 ms |
11604 KB |
Output is correct |
8 |
Correct |
77 ms |
12064 KB |
Output is correct |
9 |
Correct |
70 ms |
12108 KB |
Output is correct |
10 |
Correct |
101 ms |
11912 KB |
Output is correct |
11 |
Correct |
85 ms |
11772 KB |
Output is correct |
12 |
Correct |
54 ms |
11852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 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 |
1 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 |
36 ms |
1236 KB |
Output is correct |
20 |
Correct |
28 ms |
1316 KB |
Output is correct |
21 |
Correct |
31 ms |
1556 KB |
Output is correct |
22 |
Correct |
28 ms |
1720 KB |
Output is correct |
23 |
Correct |
26 ms |
1476 KB |
Output is correct |
24 |
Correct |
24 ms |
1356 KB |
Output is correct |
25 |
Correct |
25 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
2 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 |
48 ms |
11088 KB |
Output is correct |
20 |
Correct |
39 ms |
11084 KB |
Output is correct |
21 |
Correct |
44 ms |
10452 KB |
Output is correct |
22 |
Correct |
47 ms |
10868 KB |
Output is correct |
23 |
Correct |
37 ms |
10436 KB |
Output is correct |
24 |
Correct |
44 ms |
11108 KB |
Output is correct |
25 |
Correct |
33 ms |
10440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
11580 KB |
Output is correct |
2 |
Correct |
71 ms |
11516 KB |
Output is correct |
3 |
Correct |
76 ms |
11592 KB |
Output is correct |
4 |
Correct |
64 ms |
12092 KB |
Output is correct |
5 |
Correct |
97 ms |
11848 KB |
Output is correct |
6 |
Correct |
101 ms |
11704 KB |
Output is correct |
7 |
Correct |
104 ms |
11668 KB |
Output is correct |
8 |
Correct |
83 ms |
11632 KB |
Output is correct |
9 |
Correct |
36 ms |
10444 KB |
Output is correct |
10 |
Correct |
71 ms |
11364 KB |
Output is correct |
11 |
Correct |
65 ms |
11104 KB |
Output is correct |
12 |
Correct |
70 ms |
11340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
69 ms |
11592 KB |
Output is correct |
3 |
Correct |
68 ms |
11568 KB |
Output is correct |
4 |
Correct |
84 ms |
11624 KB |
Output is correct |
5 |
Correct |
73 ms |
11584 KB |
Output is correct |
6 |
Correct |
82 ms |
11600 KB |
Output is correct |
7 |
Correct |
69 ms |
11604 KB |
Output is correct |
8 |
Correct |
77 ms |
12064 KB |
Output is correct |
9 |
Correct |
70 ms |
12108 KB |
Output is correct |
10 |
Correct |
101 ms |
11912 KB |
Output is correct |
11 |
Correct |
85 ms |
11772 KB |
Output is correct |
12 |
Correct |
54 ms |
11852 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 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 |
1 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 |
36 ms |
1236 KB |
Output is correct |
32 |
Correct |
28 ms |
1316 KB |
Output is correct |
33 |
Correct |
31 ms |
1556 KB |
Output is correct |
34 |
Correct |
28 ms |
1720 KB |
Output is correct |
35 |
Correct |
26 ms |
1476 KB |
Output is correct |
36 |
Correct |
24 ms |
1356 KB |
Output is correct |
37 |
Correct |
25 ms |
1100 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 |
2 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
48 ms |
11088 KB |
Output is correct |
48 |
Correct |
39 ms |
11084 KB |
Output is correct |
49 |
Correct |
44 ms |
10452 KB |
Output is correct |
50 |
Correct |
47 ms |
10868 KB |
Output is correct |
51 |
Correct |
37 ms |
10436 KB |
Output is correct |
52 |
Correct |
44 ms |
11108 KB |
Output is correct |
53 |
Correct |
33 ms |
10440 KB |
Output is correct |
54 |
Correct |
92 ms |
11580 KB |
Output is correct |
55 |
Correct |
71 ms |
11516 KB |
Output is correct |
56 |
Correct |
76 ms |
11592 KB |
Output is correct |
57 |
Correct |
64 ms |
12092 KB |
Output is correct |
58 |
Correct |
97 ms |
11848 KB |
Output is correct |
59 |
Correct |
101 ms |
11704 KB |
Output is correct |
60 |
Correct |
104 ms |
11668 KB |
Output is correct |
61 |
Correct |
83 ms |
11632 KB |
Output is correct |
62 |
Correct |
36 ms |
10444 KB |
Output is correct |
63 |
Correct |
71 ms |
11364 KB |
Output is correct |
64 |
Correct |
65 ms |
11104 KB |
Output is correct |
65 |
Correct |
70 ms |
11340 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
69 ms |
11592 KB |
Output is correct |
68 |
Correct |
68 ms |
11548 KB |
Output is correct |
69 |
Correct |
76 ms |
11488 KB |
Output is correct |
70 |
Correct |
73 ms |
11552 KB |
Output is correct |
71 |
Correct |
74 ms |
11616 KB |
Output is correct |
72 |
Correct |
70 ms |
11596 KB |
Output is correct |
73 |
Correct |
66 ms |
11996 KB |
Output is correct |
74 |
Correct |
64 ms |
12060 KB |
Output is correct |
75 |
Correct |
100 ms |
11848 KB |
Output is correct |
76 |
Correct |
89 ms |
11808 KB |
Output is correct |
77 |
Correct |
54 ms |
11856 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
340 KB |
Output is correct |
80 |
Correct |
2 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 |
340 KB |
Output is correct |
84 |
Correct |
1 ms |
340 KB |
Output is correct |
85 |
Correct |
1 ms |
340 KB |
Output is correct |
86 |
Correct |
39 ms |
1224 KB |
Output is correct |
87 |
Correct |
26 ms |
1380 KB |
Output is correct |
88 |
Correct |
30 ms |
1596 KB |
Output is correct |
89 |
Correct |
23 ms |
1536 KB |
Output is correct |
90 |
Correct |
29 ms |
1400 KB |
Output is correct |
91 |
Correct |
19 ms |
1364 KB |
Output is correct |
92 |
Correct |
27 ms |
1084 KB |
Output is correct |
93 |
Correct |
44 ms |
11072 KB |
Output is correct |
94 |
Correct |
39 ms |
11096 KB |
Output is correct |
95 |
Correct |
44 ms |
10440 KB |
Output is correct |
96 |
Correct |
45 ms |
10832 KB |
Output is correct |
97 |
Correct |
36 ms |
10444 KB |
Output is correct |
98 |
Correct |
44 ms |
11088 KB |
Output is correct |
99 |
Correct |
30 ms |
10452 KB |
Output is correct |
100 |
Correct |
98 ms |
11712 KB |
Output is correct |
101 |
Correct |
106 ms |
11708 KB |
Output is correct |
102 |
Correct |
82 ms |
11596 KB |
Output is correct |
103 |
Correct |
67 ms |
11320 KB |
Output is correct |
104 |
Correct |
71 ms |
11080 KB |
Output is correct |
105 |
Correct |
74 ms |
11336 KB |
Output is correct |
106 |
Correct |
84 ms |
11592 KB |
Output is correct |
107 |
Correct |
111 ms |
11852 KB |
Output is correct |
108 |
Correct |
73 ms |
11208 KB |
Output is correct |
109 |
Correct |
84 ms |
11276 KB |
Output is correct |
110 |
Correct |
72 ms |
11076 KB |
Output is correct |
111 |
Correct |
70 ms |
11076 KB |
Output is correct |