#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define fr first
#define sc second
const int B = 17;
int n;
struct Rmq
{
int rmq[B][100005];
void build(int arr[])
{
for(int i = 1; i <= n; i ++)
rmq[0][i] = arr[i];
for(int p = 0; p + 1 < B; p ++)
for(int i = 1; i <= n - (1 << p); i ++)
rmq[p + 1][i] = min(rmq[p][i], rmq[p][i + (1 << p)]);
}
int query(int l, int r)
{
int p = __lg(r - l + 1);
return min(rmq[p][l], rmq[p][r - (1 << p) + 1]);
}
} ds;
int q, rk[100005], ord[100005], lift[20][100005], lst[100005], ans[100005], start[100005];
pii p[100005], rng[100005];
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> q;
for(int i = 1; i <= n; i ++)
cin >> p[i].fr >> p[i].sc;
for(int i = 1; i <= n; i ++)
rk[i] = i;
sort(rk + 1, rk + 1 + n, [&](int i, int j)
{
return p[i].sc < p[j].sc;
});
for(int i = 1; i <= n; i ++)
ord[rk[i]] = i;
sort(p + 1, p + 1 + n, [&](pii i, pii j)
{
return i.sc < j.sc;
});
for(int i = 1; i <= n; i ++)
lift[0][i] = lower_bound(p + 1, p + 1 + n, pii(0, p[i].fr), [](pii i, pii j)
{
return i.sc < j.sc;
}) - p;
for(int i = 1; i <= n; i ++)
lst[i] = upper_bound(p + 1, p + 1 + n, pii(0, p[i].sc), [](pii i, pii j)
{
return i.sc < j.sc;
}) - p - 1;
for(int p = 0; p + 1 < B; p ++)
{
ds.build(lift[p]);
for(int i = 1; i <= n; i ++)
lift[p + 1][i] = ds.query(lift[p][i], lst[i]);
}
for(int i = 1; i <= q; i ++)
{
int u, v;
cin >> u >> v;
if(u == v)
ans[i] = -1;
start[i] = ord[u];
rng[i] = pii(ord[v], ord[v]);
}
for(int p = B - 1; p >= 0; p --)
{
ds.build(lift[p]);
for(int i = 1; i <= q; i ++)
{
pii nrange = pii(ds.query(rng[i].fr, rng[i].sc), lst[rng[i].sc]);
if(start[i] < nrange.fr || nrange.sc < start[i])
{
ans[i] += (1 << p);
rng[i] = nrange;
}
}
}
for(int i = 1; i <= q; i ++)
if(ans[i] >= n)
cout << "impossible\n";
else
cout << ans[i] + 1 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14676 KB |
Output is correct |
2 |
Correct |
104 ms |
21960 KB |
Output is correct |
3 |
Correct |
101 ms |
21836 KB |
Output is correct |
4 |
Correct |
112 ms |
21836 KB |
Output is correct |
5 |
Correct |
104 ms |
21836 KB |
Output is correct |
6 |
Correct |
102 ms |
21832 KB |
Output is correct |
7 |
Correct |
104 ms |
21964 KB |
Output is correct |
8 |
Correct |
101 ms |
22344 KB |
Output is correct |
9 |
Correct |
101 ms |
22348 KB |
Output is correct |
10 |
Correct |
114 ms |
22092 KB |
Output is correct |
11 |
Correct |
114 ms |
21328 KB |
Output is correct |
12 |
Correct |
88 ms |
20556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10580 KB |
Output is correct |
2 |
Correct |
2 ms |
10580 KB |
Output is correct |
3 |
Correct |
2 ms |
10836 KB |
Output is correct |
4 |
Correct |
3 ms |
10836 KB |
Output is correct |
5 |
Correct |
2 ms |
10836 KB |
Output is correct |
6 |
Correct |
2 ms |
10836 KB |
Output is correct |
7 |
Correct |
2 ms |
10836 KB |
Output is correct |
8 |
Correct |
2 ms |
10836 KB |
Output is correct |
9 |
Correct |
2 ms |
10932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10580 KB |
Output is correct |
2 |
Correct |
2 ms |
10580 KB |
Output is correct |
3 |
Correct |
2 ms |
10836 KB |
Output is correct |
4 |
Correct |
3 ms |
10836 KB |
Output is correct |
5 |
Correct |
2 ms |
10836 KB |
Output is correct |
6 |
Correct |
2 ms |
10836 KB |
Output is correct |
7 |
Correct |
2 ms |
10836 KB |
Output is correct |
8 |
Correct |
2 ms |
10836 KB |
Output is correct |
9 |
Correct |
2 ms |
10932 KB |
Output is correct |
10 |
Correct |
2 ms |
10580 KB |
Output is correct |
11 |
Correct |
1 ms |
10580 KB |
Output is correct |
12 |
Correct |
2 ms |
10836 KB |
Output is correct |
13 |
Correct |
2 ms |
10836 KB |
Output is correct |
14 |
Correct |
2 ms |
10836 KB |
Output is correct |
15 |
Correct |
3 ms |
16888 KB |
Output is correct |
16 |
Correct |
2 ms |
10836 KB |
Output is correct |
17 |
Correct |
3 ms |
11092 KB |
Output is correct |
18 |
Correct |
2 ms |
10836 KB |
Output is correct |
19 |
Correct |
24 ms |
12668 KB |
Output is correct |
20 |
Correct |
25 ms |
12620 KB |
Output is correct |
21 |
Correct |
35 ms |
14676 KB |
Output is correct |
22 |
Correct |
34 ms |
18708 KB |
Output is correct |
23 |
Correct |
32 ms |
18508 KB |
Output is correct |
24 |
Correct |
32 ms |
18596 KB |
Output is correct |
25 |
Correct |
24 ms |
18252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10580 KB |
Output is correct |
2 |
Correct |
2 ms |
10580 KB |
Output is correct |
3 |
Correct |
2 ms |
10836 KB |
Output is correct |
4 |
Correct |
3 ms |
10836 KB |
Output is correct |
5 |
Correct |
2 ms |
10836 KB |
Output is correct |
6 |
Correct |
2 ms |
10836 KB |
Output is correct |
7 |
Correct |
2 ms |
10836 KB |
Output is correct |
8 |
Correct |
2 ms |
10836 KB |
Output is correct |
9 |
Correct |
2 ms |
10932 KB |
Output is correct |
10 |
Correct |
2 ms |
14676 KB |
Output is correct |
11 |
Correct |
1 ms |
10580 KB |
Output is correct |
12 |
Correct |
2 ms |
10836 KB |
Output is correct |
13 |
Correct |
2 ms |
10836 KB |
Output is correct |
14 |
Correct |
2 ms |
10836 KB |
Output is correct |
15 |
Correct |
2 ms |
10836 KB |
Output is correct |
16 |
Correct |
2 ms |
10848 KB |
Output is correct |
17 |
Correct |
2 ms |
10836 KB |
Output is correct |
18 |
Correct |
2 ms |
10836 KB |
Output is correct |
19 |
Correct |
77 ms |
18764 KB |
Output is correct |
20 |
Correct |
74 ms |
19036 KB |
Output is correct |
21 |
Correct |
83 ms |
18764 KB |
Output is correct |
22 |
Correct |
77 ms |
18776 KB |
Output is correct |
23 |
Correct |
74 ms |
19912 KB |
Output is correct |
24 |
Correct |
76 ms |
19788 KB |
Output is correct |
25 |
Correct |
65 ms |
19276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
20808 KB |
Output is correct |
2 |
Correct |
113 ms |
20812 KB |
Output is correct |
3 |
Correct |
111 ms |
20812 KB |
Output is correct |
4 |
Correct |
110 ms |
21336 KB |
Output is correct |
5 |
Correct |
123 ms |
21068 KB |
Output is correct |
6 |
Correct |
122 ms |
21836 KB |
Output is correct |
7 |
Correct |
116 ms |
21976 KB |
Output is correct |
8 |
Correct |
114 ms |
22092 KB |
Output is correct |
9 |
Correct |
74 ms |
19820 KB |
Output is correct |
10 |
Correct |
101 ms |
21584 KB |
Output is correct |
11 |
Correct |
99 ms |
21324 KB |
Output is correct |
12 |
Correct |
109 ms |
21592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14676 KB |
Output is correct |
2 |
Correct |
104 ms |
21960 KB |
Output is correct |
3 |
Correct |
101 ms |
21836 KB |
Output is correct |
4 |
Correct |
112 ms |
21836 KB |
Output is correct |
5 |
Correct |
104 ms |
21836 KB |
Output is correct |
6 |
Correct |
102 ms |
21832 KB |
Output is correct |
7 |
Correct |
104 ms |
21964 KB |
Output is correct |
8 |
Correct |
101 ms |
22344 KB |
Output is correct |
9 |
Correct |
101 ms |
22348 KB |
Output is correct |
10 |
Correct |
114 ms |
22092 KB |
Output is correct |
11 |
Correct |
114 ms |
21328 KB |
Output is correct |
12 |
Correct |
88 ms |
20556 KB |
Output is correct |
13 |
Correct |
2 ms |
10580 KB |
Output is correct |
14 |
Correct |
2 ms |
10580 KB |
Output is correct |
15 |
Correct |
2 ms |
10836 KB |
Output is correct |
16 |
Correct |
3 ms |
10836 KB |
Output is correct |
17 |
Correct |
2 ms |
10836 KB |
Output is correct |
18 |
Correct |
2 ms |
10836 KB |
Output is correct |
19 |
Correct |
2 ms |
10836 KB |
Output is correct |
20 |
Correct |
2 ms |
10836 KB |
Output is correct |
21 |
Correct |
2 ms |
10932 KB |
Output is correct |
22 |
Correct |
2 ms |
10580 KB |
Output is correct |
23 |
Correct |
1 ms |
10580 KB |
Output is correct |
24 |
Correct |
2 ms |
10836 KB |
Output is correct |
25 |
Correct |
2 ms |
10836 KB |
Output is correct |
26 |
Correct |
2 ms |
10836 KB |
Output is correct |
27 |
Correct |
3 ms |
16888 KB |
Output is correct |
28 |
Correct |
2 ms |
10836 KB |
Output is correct |
29 |
Correct |
3 ms |
11092 KB |
Output is correct |
30 |
Correct |
2 ms |
10836 KB |
Output is correct |
31 |
Correct |
24 ms |
12668 KB |
Output is correct |
32 |
Correct |
25 ms |
12620 KB |
Output is correct |
33 |
Correct |
35 ms |
14676 KB |
Output is correct |
34 |
Correct |
34 ms |
18708 KB |
Output is correct |
35 |
Correct |
32 ms |
18508 KB |
Output is correct |
36 |
Correct |
32 ms |
18596 KB |
Output is correct |
37 |
Correct |
24 ms |
18252 KB |
Output is correct |
38 |
Correct |
2 ms |
14676 KB |
Output is correct |
39 |
Correct |
1 ms |
10580 KB |
Output is correct |
40 |
Correct |
2 ms |
10836 KB |
Output is correct |
41 |
Correct |
2 ms |
10836 KB |
Output is correct |
42 |
Correct |
2 ms |
10836 KB |
Output is correct |
43 |
Correct |
2 ms |
10836 KB |
Output is correct |
44 |
Correct |
2 ms |
10848 KB |
Output is correct |
45 |
Correct |
2 ms |
10836 KB |
Output is correct |
46 |
Correct |
2 ms |
10836 KB |
Output is correct |
47 |
Correct |
77 ms |
18764 KB |
Output is correct |
48 |
Correct |
74 ms |
19036 KB |
Output is correct |
49 |
Correct |
83 ms |
18764 KB |
Output is correct |
50 |
Correct |
77 ms |
18776 KB |
Output is correct |
51 |
Correct |
74 ms |
19912 KB |
Output is correct |
52 |
Correct |
76 ms |
19788 KB |
Output is correct |
53 |
Correct |
65 ms |
19276 KB |
Output is correct |
54 |
Correct |
101 ms |
20808 KB |
Output is correct |
55 |
Correct |
113 ms |
20812 KB |
Output is correct |
56 |
Correct |
111 ms |
20812 KB |
Output is correct |
57 |
Correct |
110 ms |
21336 KB |
Output is correct |
58 |
Correct |
123 ms |
21068 KB |
Output is correct |
59 |
Correct |
122 ms |
21836 KB |
Output is correct |
60 |
Correct |
116 ms |
21976 KB |
Output is correct |
61 |
Correct |
114 ms |
22092 KB |
Output is correct |
62 |
Correct |
74 ms |
19820 KB |
Output is correct |
63 |
Correct |
101 ms |
21584 KB |
Output is correct |
64 |
Correct |
99 ms |
21324 KB |
Output is correct |
65 |
Correct |
109 ms |
21592 KB |
Output is correct |
66 |
Correct |
2 ms |
14676 KB |
Output is correct |
67 |
Correct |
102 ms |
21752 KB |
Output is correct |
68 |
Correct |
107 ms |
21700 KB |
Output is correct |
69 |
Correct |
117 ms |
21836 KB |
Output is correct |
70 |
Correct |
102 ms |
21840 KB |
Output is correct |
71 |
Correct |
108 ms |
21836 KB |
Output is correct |
72 |
Correct |
103 ms |
21836 KB |
Output is correct |
73 |
Correct |
100 ms |
22348 KB |
Output is correct |
74 |
Correct |
102 ms |
22236 KB |
Output is correct |
75 |
Correct |
115 ms |
22092 KB |
Output is correct |
76 |
Correct |
117 ms |
21324 KB |
Output is correct |
77 |
Correct |
86 ms |
20560 KB |
Output is correct |
78 |
Correct |
2 ms |
10744 KB |
Output is correct |
79 |
Correct |
2 ms |
10732 KB |
Output is correct |
80 |
Correct |
3 ms |
16724 KB |
Output is correct |
81 |
Correct |
3 ms |
16724 KB |
Output is correct |
82 |
Correct |
3 ms |
16724 KB |
Output is correct |
83 |
Correct |
3 ms |
16724 KB |
Output is correct |
84 |
Correct |
3 ms |
11000 KB |
Output is correct |
85 |
Correct |
2 ms |
10836 KB |
Output is correct |
86 |
Correct |
28 ms |
18380 KB |
Output is correct |
87 |
Correct |
28 ms |
14412 KB |
Output is correct |
88 |
Correct |
43 ms |
12972 KB |
Output is correct |
89 |
Correct |
37 ms |
18848 KB |
Output is correct |
90 |
Correct |
35 ms |
18604 KB |
Output is correct |
91 |
Correct |
36 ms |
18508 KB |
Output is correct |
92 |
Correct |
28 ms |
12340 KB |
Output is correct |
93 |
Correct |
81 ms |
19788 KB |
Output is correct |
94 |
Correct |
76 ms |
19212 KB |
Output is correct |
95 |
Correct |
80 ms |
19788 KB |
Output is correct |
96 |
Correct |
83 ms |
19788 KB |
Output is correct |
97 |
Correct |
77 ms |
19792 KB |
Output is correct |
98 |
Correct |
79 ms |
19804 KB |
Output is correct |
99 |
Correct |
63 ms |
19284 KB |
Output is correct |
100 |
Correct |
124 ms |
21836 KB |
Output is correct |
101 |
Correct |
113 ms |
21848 KB |
Output is correct |
102 |
Correct |
118 ms |
22092 KB |
Output is correct |
103 |
Correct |
102 ms |
21580 KB |
Output is correct |
104 |
Correct |
102 ms |
21324 KB |
Output is correct |
105 |
Correct |
106 ms |
21580 KB |
Output is correct |
106 |
Correct |
108 ms |
21196 KB |
Output is correct |
107 |
Correct |
108 ms |
21332 KB |
Output is correct |
108 |
Correct |
111 ms |
21944 KB |
Output is correct |
109 |
Correct |
118 ms |
21836 KB |
Output is correct |
110 |
Correct |
110 ms |
21840 KB |
Output is correct |
111 |
Correct |
118 ms |
21836 KB |
Output is correct |