#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
const int P = 17;
int N;
struct sparse
{
int table[P][100005];
void build(int arr[])
{
for(int i = 1; i <= N; i++)
table[0][i] = arr[i];
for(int p = 0; p + 1 < P; p++)
for(int i = 1; i <= N - (1 << p); i++)
table[p + 1][i] = min(table[p][i], table[p][i + (1 << p)]);
}
int query(int l, int r)
{
int p = __lg(r - l + 1);
return min(table[p][l], table[p][r - (1 << p) + 1]);
}
}st;
int Q, rk[100005], order[100005], table[20][100005], last[100005], ans[100005], start[100005];
pii p[100005], range[100005];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> N >> Q;
for(int i = 1; i <= N; i++)
cin >> p[i].F >> p[i].S;
for(int i = 1; i <= N; i++)
rk[i] = i;
sort(rk + 1, rk + 1 + N, [&](int i, int j){return p[i].S < p[j].S;});
for(int i = 1; i <= N; i++)
order[rk[i]] = i;
sort(p + 1, p + 1 + N, [&](pii i, pii j){return i.S < j.S; });
for(int i = 1; i <= N; i++)
table[0][i] = lower_bound(p + 1, p + 1 + N, pii(0, p[i].F), [](pii i, pii j){return i.S < j.S; }) - p;
for(int i = 1; i <= N; i++)
last[i] = upper_bound(p + 1, p + 1 + N, pii(0, p[i].S), [](pii i, pii j){return i.S < j.S; }) - p - 1;
for(int p = 0; p + 1 < P; p++)
{
st.build(table[p]);
for(int i = 1; i <= N; i++)
table[p + 1][i] = st.query(table[p][i], last[i]);
}
// for(int p = 0; p < P; p++)
// for(int i = 1; i <= N; i++)
// cout << table[p][i] << " \n"[i == N];
for(int i = 1; i <= Q; i++)
{
int u, v;
cin >> u >> v;
if(u == v) ans[i] = -1;
start[i] = order[u];
range[i] = pii(order[v], order[v]);
}
for(int p = P - 1; p >= 0; p--)
{
st.build(table[p]);
for(int i = 1; i <= Q; i++)
{
pii nrange = pii(st.query(range[i].F, range[i].S), last[range[i].S]);
// if(i == 1 || i == 2) cout << "query " << i << " " << p << " range " << range[i].F << " " << range[i].S << " -> " << nrange.F << " " << nrange.S << '\n';
if(start[i] < nrange.F || nrange.S < start[i])
{
ans[i] += (1 << p);
range[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 |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
142 ms |
20576 KB |
Output is correct |
3 |
Correct |
133 ms |
20596 KB |
Output is correct |
4 |
Correct |
150 ms |
20568 KB |
Output is correct |
5 |
Correct |
151 ms |
20664 KB |
Output is correct |
6 |
Correct |
139 ms |
20612 KB |
Output is correct |
7 |
Correct |
146 ms |
20636 KB |
Output is correct |
8 |
Correct |
137 ms |
21004 KB |
Output is correct |
9 |
Correct |
153 ms |
21068 KB |
Output is correct |
10 |
Correct |
155 ms |
20924 KB |
Output is correct |
11 |
Correct |
169 ms |
20940 KB |
Output is correct |
12 |
Correct |
145 ms |
20264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
632 KB |
Output is correct |
4 |
Correct |
2 ms |
608 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
632 KB |
Output is correct |
4 |
Correct |
2 ms |
608 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
58 ms |
4172 KB |
Output is correct |
20 |
Correct |
36 ms |
4172 KB |
Output is correct |
21 |
Correct |
44 ms |
4428 KB |
Output is correct |
22 |
Correct |
43 ms |
4520 KB |
Output is correct |
23 |
Correct |
50 ms |
4396 KB |
Output is correct |
24 |
Correct |
47 ms |
4392 KB |
Output is correct |
25 |
Correct |
33 ms |
3988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
632 KB |
Output is correct |
4 |
Correct |
2 ms |
608 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
632 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
628 KB |
Output is correct |
16 |
Correct |
2 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
108 ms |
17320 KB |
Output is correct |
20 |
Correct |
111 ms |
16460 KB |
Output is correct |
21 |
Correct |
127 ms |
17336 KB |
Output is correct |
22 |
Correct |
134 ms |
17328 KB |
Output is correct |
23 |
Correct |
107 ms |
17216 KB |
Output is correct |
24 |
Correct |
137 ms |
17296 KB |
Output is correct |
25 |
Correct |
109 ms |
16620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
20560 KB |
Output is correct |
2 |
Correct |
172 ms |
20592 KB |
Output is correct |
3 |
Correct |
149 ms |
20596 KB |
Output is correct |
4 |
Correct |
149 ms |
21096 KB |
Output is correct |
5 |
Correct |
187 ms |
20916 KB |
Output is correct |
6 |
Correct |
179 ms |
20712 KB |
Output is correct |
7 |
Correct |
188 ms |
20636 KB |
Output is correct |
8 |
Correct |
189 ms |
20812 KB |
Output is correct |
9 |
Correct |
125 ms |
17296 KB |
Output is correct |
10 |
Correct |
154 ms |
20316 KB |
Output is correct |
11 |
Correct |
177 ms |
20120 KB |
Output is correct |
12 |
Correct |
156 ms |
20320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
142 ms |
20576 KB |
Output is correct |
3 |
Correct |
133 ms |
20596 KB |
Output is correct |
4 |
Correct |
150 ms |
20568 KB |
Output is correct |
5 |
Correct |
151 ms |
20664 KB |
Output is correct |
6 |
Correct |
139 ms |
20612 KB |
Output is correct |
7 |
Correct |
146 ms |
20636 KB |
Output is correct |
8 |
Correct |
137 ms |
21004 KB |
Output is correct |
9 |
Correct |
153 ms |
21068 KB |
Output is correct |
10 |
Correct |
155 ms |
20924 KB |
Output is correct |
11 |
Correct |
169 ms |
20940 KB |
Output is correct |
12 |
Correct |
145 ms |
20264 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
632 KB |
Output is correct |
16 |
Correct |
2 ms |
608 KB |
Output is correct |
17 |
Correct |
2 ms |
592 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
640 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
2 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
58 ms |
4172 KB |
Output is correct |
32 |
Correct |
36 ms |
4172 KB |
Output is correct |
33 |
Correct |
44 ms |
4428 KB |
Output is correct |
34 |
Correct |
43 ms |
4520 KB |
Output is correct |
35 |
Correct |
50 ms |
4396 KB |
Output is correct |
36 |
Correct |
47 ms |
4392 KB |
Output is correct |
37 |
Correct |
33 ms |
3988 KB |
Output is correct |
38 |
Correct |
1 ms |
460 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
2 ms |
596 KB |
Output is correct |
41 |
Correct |
2 ms |
632 KB |
Output is correct |
42 |
Correct |
2 ms |
596 KB |
Output is correct |
43 |
Correct |
2 ms |
628 KB |
Output is correct |
44 |
Correct |
2 ms |
600 KB |
Output is correct |
45 |
Correct |
1 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
108 ms |
17320 KB |
Output is correct |
48 |
Correct |
111 ms |
16460 KB |
Output is correct |
49 |
Correct |
127 ms |
17336 KB |
Output is correct |
50 |
Correct |
134 ms |
17328 KB |
Output is correct |
51 |
Correct |
107 ms |
17216 KB |
Output is correct |
52 |
Correct |
137 ms |
17296 KB |
Output is correct |
53 |
Correct |
109 ms |
16620 KB |
Output is correct |
54 |
Correct |
156 ms |
20560 KB |
Output is correct |
55 |
Correct |
172 ms |
20592 KB |
Output is correct |
56 |
Correct |
149 ms |
20596 KB |
Output is correct |
57 |
Correct |
149 ms |
21096 KB |
Output is correct |
58 |
Correct |
187 ms |
20916 KB |
Output is correct |
59 |
Correct |
179 ms |
20712 KB |
Output is correct |
60 |
Correct |
188 ms |
20636 KB |
Output is correct |
61 |
Correct |
189 ms |
20812 KB |
Output is correct |
62 |
Correct |
125 ms |
17296 KB |
Output is correct |
63 |
Correct |
154 ms |
20316 KB |
Output is correct |
64 |
Correct |
177 ms |
20120 KB |
Output is correct |
65 |
Correct |
156 ms |
20320 KB |
Output is correct |
66 |
Correct |
1 ms |
468 KB |
Output is correct |
67 |
Correct |
151 ms |
20552 KB |
Output is correct |
68 |
Correct |
170 ms |
20524 KB |
Output is correct |
69 |
Correct |
157 ms |
20596 KB |
Output is correct |
70 |
Correct |
176 ms |
20656 KB |
Output is correct |
71 |
Correct |
163 ms |
20660 KB |
Output is correct |
72 |
Correct |
160 ms |
20668 KB |
Output is correct |
73 |
Correct |
171 ms |
21040 KB |
Output is correct |
74 |
Correct |
165 ms |
21016 KB |
Output is correct |
75 |
Correct |
188 ms |
20944 KB |
Output is correct |
76 |
Correct |
185 ms |
20988 KB |
Output is correct |
77 |
Correct |
139 ms |
20312 KB |
Output is correct |
78 |
Correct |
1 ms |
468 KB |
Output is correct |
79 |
Correct |
2 ms |
596 KB |
Output is correct |
80 |
Correct |
2 ms |
596 KB |
Output is correct |
81 |
Correct |
2 ms |
596 KB |
Output is correct |
82 |
Correct |
2 ms |
628 KB |
Output is correct |
83 |
Correct |
2 ms |
596 KB |
Output is correct |
84 |
Correct |
1 ms |
600 KB |
Output is correct |
85 |
Correct |
1 ms |
600 KB |
Output is correct |
86 |
Correct |
52 ms |
4156 KB |
Output is correct |
87 |
Correct |
37 ms |
4140 KB |
Output is correct |
88 |
Correct |
50 ms |
4448 KB |
Output is correct |
89 |
Correct |
44 ms |
4516 KB |
Output is correct |
90 |
Correct |
47 ms |
4372 KB |
Output is correct |
91 |
Correct |
49 ms |
4304 KB |
Output is correct |
92 |
Correct |
41 ms |
4048 KB |
Output is correct |
93 |
Correct |
148 ms |
17332 KB |
Output is correct |
94 |
Correct |
125 ms |
16460 KB |
Output is correct |
95 |
Correct |
141 ms |
17108 KB |
Output is correct |
96 |
Correct |
121 ms |
17232 KB |
Output is correct |
97 |
Correct |
126 ms |
17212 KB |
Output is correct |
98 |
Correct |
124 ms |
17260 KB |
Output is correct |
99 |
Correct |
105 ms |
16632 KB |
Output is correct |
100 |
Correct |
198 ms |
20728 KB |
Output is correct |
101 |
Correct |
180 ms |
20692 KB |
Output is correct |
102 |
Correct |
180 ms |
20832 KB |
Output is correct |
103 |
Correct |
161 ms |
20312 KB |
Output is correct |
104 |
Correct |
162 ms |
20124 KB |
Output is correct |
105 |
Correct |
159 ms |
20380 KB |
Output is correct |
106 |
Correct |
179 ms |
19968 KB |
Output is correct |
107 |
Correct |
170 ms |
20028 KB |
Output is correct |
108 |
Correct |
202 ms |
20648 KB |
Output is correct |
109 |
Correct |
170 ms |
20676 KB |
Output is correct |
110 |
Correct |
183 ms |
20644 KB |
Output is correct |
111 |
Correct |
191 ms |
20632 KB |
Output is correct |