#include <bits/stdc++.h>
using namespace std;
#define int long long
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1e5 + 5, INF = 1e15;
int s[N], e[N];
int dp[20][N];
vector<pair<int, int> > seg(N * 8, {INF, -1});
void update(int l, int r, int pos, int qpos, pair<int, int> qval)
{
if(l == r)
{
seg[pos] = min(seg[pos], qval);
return;
}
int mid = (l + r) >> 1;
if(qpos <= mid) update(l, mid, pos * 2, qpos, qval);
else update(mid + 1, r, pos * 2 + 1, qpos, qval);
seg[pos] = min(seg[pos * 2], seg[pos * 2 + 1]);
}
pair<int, int> query(int l, int r, int pos, int ql, int qr)
{
if(ql > r || l > qr) return {INF, -1};
if(ql <= l && r <= qr) return seg[pos];
int mid = (l + r) >> 1;
return min(query(l, mid, pos * 2, ql, qr), query(mid + 1, r, pos * 2 + 1, ql, qr));
}
void Solve()
{
int n, q;
cin >> n >> q;
map<int, int> cc;
for(int i = 1; i <= n; i++)
{
cin >> s[i] >> e[i];
cc[s[i]]++;
cc[e[i]]++;
}
int cnt = 0;
for(auto i: cc)
{
cc[i.first] = ++cnt;
}
for(int i = 1; i <= n; i++)
{
s[i] = cc[s[i]];
e[i] = cc[e[i]];
update(1, cnt, 1, e[i], {s[i], i});
}
for(int i = 1; i <= n; i++)
{
pair<int, int> res = query(1, cnt, 1, s[i], e[i]);
if(res.first >= s[i]) dp[0][i] = -1;
else dp[0][i] = res.second;
}
for(int i = 1; i <= 19; i++)
{
for(int j = 1; j <= n; j++)
{
if(dp[i - 1][j] == -1) dp[i][j] = -1;
else dp[i][j] = dp[i - 1][dp[i - 1][j]];
}
}
while(q--)
{
int qs, qe;
cin >> qs >> qe;
if(qs == qe)
{
cout << "0\n";
continue;
}
if(e[qs] > e[qe])
{
cout << "impossible\n";
continue;
}
if(s[qe] <= e[qs] && e[qs] <= e[qe])
{
cout << "1\n";
continue;
}
int cur = qe, ans = 1;
for(int i = 19; i >= 0; i--)
{
if(dp[i][cur] != -1 && s[dp[i][cur]] > e[qs])
{
cur = dp[i][cur];
ans += (1 << i);
}
}
if(dp[0][cur] == -1)
{
cout << "impossible\n";
}
else
{
cout << ans + 1 << "\n";
}
}
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12884 KB |
Output is correct |
2 |
Correct |
195 ms |
36936 KB |
Output is correct |
3 |
Correct |
235 ms |
36844 KB |
Output is correct |
4 |
Correct |
272 ms |
36904 KB |
Output is correct |
5 |
Correct |
266 ms |
37600 KB |
Output is correct |
6 |
Correct |
232 ms |
38348 KB |
Output is correct |
7 |
Correct |
284 ms |
38448 KB |
Output is correct |
8 |
Correct |
254 ms |
43628 KB |
Output is correct |
9 |
Correct |
247 ms |
43628 KB |
Output is correct |
10 |
Correct |
265 ms |
37252 KB |
Output is correct |
11 |
Correct |
253 ms |
39824 KB |
Output is correct |
12 |
Correct |
131 ms |
37324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12884 KB |
Output is correct |
2 |
Correct |
5 ms |
12884 KB |
Output is correct |
3 |
Correct |
7 ms |
13156 KB |
Output is correct |
4 |
Correct |
9 ms |
13156 KB |
Output is correct |
5 |
Correct |
7 ms |
13140 KB |
Output is correct |
6 |
Correct |
9 ms |
13140 KB |
Output is correct |
7 |
Correct |
7 ms |
13292 KB |
Output is correct |
8 |
Correct |
8 ms |
13268 KB |
Output is correct |
9 |
Correct |
6 ms |
13140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12884 KB |
Output is correct |
2 |
Correct |
5 ms |
12884 KB |
Output is correct |
3 |
Correct |
7 ms |
13156 KB |
Output is correct |
4 |
Correct |
9 ms |
13156 KB |
Output is correct |
5 |
Correct |
7 ms |
13140 KB |
Output is correct |
6 |
Correct |
9 ms |
13140 KB |
Output is correct |
7 |
Correct |
7 ms |
13292 KB |
Output is correct |
8 |
Correct |
8 ms |
13268 KB |
Output is correct |
9 |
Correct |
6 ms |
13140 KB |
Output is correct |
10 |
Correct |
6 ms |
12928 KB |
Output is correct |
11 |
Correct |
6 ms |
12920 KB |
Output is correct |
12 |
Correct |
6 ms |
13140 KB |
Output is correct |
13 |
Correct |
6 ms |
13140 KB |
Output is correct |
14 |
Correct |
8 ms |
13140 KB |
Output is correct |
15 |
Correct |
6 ms |
13140 KB |
Output is correct |
16 |
Correct |
7 ms |
13268 KB |
Output is correct |
17 |
Correct |
7 ms |
13268 KB |
Output is correct |
18 |
Correct |
6 ms |
13140 KB |
Output is correct |
19 |
Correct |
41 ms |
14752 KB |
Output is correct |
20 |
Correct |
37 ms |
14540 KB |
Output is correct |
21 |
Correct |
43 ms |
14924 KB |
Output is correct |
22 |
Correct |
39 ms |
15080 KB |
Output is correct |
23 |
Correct |
33 ms |
15052 KB |
Output is correct |
24 |
Correct |
30 ms |
15136 KB |
Output is correct |
25 |
Correct |
41 ms |
14764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12884 KB |
Output is correct |
2 |
Correct |
5 ms |
12884 KB |
Output is correct |
3 |
Correct |
7 ms |
13156 KB |
Output is correct |
4 |
Correct |
9 ms |
13156 KB |
Output is correct |
5 |
Correct |
7 ms |
13140 KB |
Output is correct |
6 |
Correct |
9 ms |
13140 KB |
Output is correct |
7 |
Correct |
7 ms |
13292 KB |
Output is correct |
8 |
Correct |
8 ms |
13268 KB |
Output is correct |
9 |
Correct |
6 ms |
13140 KB |
Output is correct |
10 |
Correct |
5 ms |
12952 KB |
Output is correct |
11 |
Correct |
7 ms |
12884 KB |
Output is correct |
12 |
Correct |
6 ms |
13140 KB |
Output is correct |
13 |
Correct |
7 ms |
13188 KB |
Output is correct |
14 |
Correct |
9 ms |
13140 KB |
Output is correct |
15 |
Correct |
9 ms |
13140 KB |
Output is correct |
16 |
Correct |
9 ms |
13264 KB |
Output is correct |
17 |
Correct |
7 ms |
13268 KB |
Output is correct |
18 |
Correct |
6 ms |
13140 KB |
Output is correct |
19 |
Correct |
173 ms |
36464 KB |
Output is correct |
20 |
Correct |
166 ms |
36456 KB |
Output is correct |
21 |
Correct |
219 ms |
36280 KB |
Output is correct |
22 |
Correct |
197 ms |
38604 KB |
Output is correct |
23 |
Correct |
277 ms |
42716 KB |
Output is correct |
24 |
Correct |
306 ms |
42584 KB |
Output is correct |
25 |
Correct |
32 ms |
30036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
205 ms |
36944 KB |
Output is correct |
2 |
Correct |
224 ms |
36836 KB |
Output is correct |
3 |
Correct |
295 ms |
36876 KB |
Output is correct |
4 |
Correct |
274 ms |
43672 KB |
Output is correct |
5 |
Correct |
247 ms |
37252 KB |
Output is correct |
6 |
Correct |
419 ms |
43348 KB |
Output is correct |
7 |
Correct |
370 ms |
43332 KB |
Output is correct |
8 |
Correct |
345 ms |
43444 KB |
Output is correct |
9 |
Correct |
266 ms |
42576 KB |
Output is correct |
10 |
Correct |
337 ms |
43004 KB |
Output is correct |
11 |
Correct |
421 ms |
42752 KB |
Output is correct |
12 |
Correct |
341 ms |
42940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12884 KB |
Output is correct |
2 |
Correct |
195 ms |
36936 KB |
Output is correct |
3 |
Correct |
235 ms |
36844 KB |
Output is correct |
4 |
Correct |
272 ms |
36904 KB |
Output is correct |
5 |
Correct |
266 ms |
37600 KB |
Output is correct |
6 |
Correct |
232 ms |
38348 KB |
Output is correct |
7 |
Correct |
284 ms |
38448 KB |
Output is correct |
8 |
Correct |
254 ms |
43628 KB |
Output is correct |
9 |
Correct |
247 ms |
43628 KB |
Output is correct |
10 |
Correct |
265 ms |
37252 KB |
Output is correct |
11 |
Correct |
253 ms |
39824 KB |
Output is correct |
12 |
Correct |
131 ms |
37324 KB |
Output is correct |
13 |
Correct |
6 ms |
12884 KB |
Output is correct |
14 |
Correct |
5 ms |
12884 KB |
Output is correct |
15 |
Correct |
7 ms |
13156 KB |
Output is correct |
16 |
Correct |
9 ms |
13156 KB |
Output is correct |
17 |
Correct |
7 ms |
13140 KB |
Output is correct |
18 |
Correct |
9 ms |
13140 KB |
Output is correct |
19 |
Correct |
7 ms |
13292 KB |
Output is correct |
20 |
Correct |
8 ms |
13268 KB |
Output is correct |
21 |
Correct |
6 ms |
13140 KB |
Output is correct |
22 |
Correct |
6 ms |
12928 KB |
Output is correct |
23 |
Correct |
6 ms |
12920 KB |
Output is correct |
24 |
Correct |
6 ms |
13140 KB |
Output is correct |
25 |
Correct |
6 ms |
13140 KB |
Output is correct |
26 |
Correct |
8 ms |
13140 KB |
Output is correct |
27 |
Correct |
6 ms |
13140 KB |
Output is correct |
28 |
Correct |
7 ms |
13268 KB |
Output is correct |
29 |
Correct |
7 ms |
13268 KB |
Output is correct |
30 |
Correct |
6 ms |
13140 KB |
Output is correct |
31 |
Correct |
41 ms |
14752 KB |
Output is correct |
32 |
Correct |
37 ms |
14540 KB |
Output is correct |
33 |
Correct |
43 ms |
14924 KB |
Output is correct |
34 |
Correct |
39 ms |
15080 KB |
Output is correct |
35 |
Correct |
33 ms |
15052 KB |
Output is correct |
36 |
Correct |
30 ms |
15136 KB |
Output is correct |
37 |
Correct |
41 ms |
14764 KB |
Output is correct |
38 |
Correct |
5 ms |
12952 KB |
Output is correct |
39 |
Correct |
7 ms |
12884 KB |
Output is correct |
40 |
Correct |
6 ms |
13140 KB |
Output is correct |
41 |
Correct |
7 ms |
13188 KB |
Output is correct |
42 |
Correct |
9 ms |
13140 KB |
Output is correct |
43 |
Correct |
9 ms |
13140 KB |
Output is correct |
44 |
Correct |
9 ms |
13264 KB |
Output is correct |
45 |
Correct |
7 ms |
13268 KB |
Output is correct |
46 |
Correct |
6 ms |
13140 KB |
Output is correct |
47 |
Correct |
173 ms |
36464 KB |
Output is correct |
48 |
Correct |
166 ms |
36456 KB |
Output is correct |
49 |
Correct |
219 ms |
36280 KB |
Output is correct |
50 |
Correct |
197 ms |
38604 KB |
Output is correct |
51 |
Correct |
277 ms |
42716 KB |
Output is correct |
52 |
Correct |
306 ms |
42584 KB |
Output is correct |
53 |
Correct |
32 ms |
30036 KB |
Output is correct |
54 |
Correct |
205 ms |
36944 KB |
Output is correct |
55 |
Correct |
224 ms |
36836 KB |
Output is correct |
56 |
Correct |
295 ms |
36876 KB |
Output is correct |
57 |
Correct |
274 ms |
43672 KB |
Output is correct |
58 |
Correct |
247 ms |
37252 KB |
Output is correct |
59 |
Correct |
419 ms |
43348 KB |
Output is correct |
60 |
Correct |
370 ms |
43332 KB |
Output is correct |
61 |
Correct |
345 ms |
43444 KB |
Output is correct |
62 |
Correct |
266 ms |
42576 KB |
Output is correct |
63 |
Correct |
337 ms |
43004 KB |
Output is correct |
64 |
Correct |
421 ms |
42752 KB |
Output is correct |
65 |
Correct |
341 ms |
42940 KB |
Output is correct |
66 |
Correct |
6 ms |
12884 KB |
Output is correct |
67 |
Correct |
226 ms |
36836 KB |
Output is correct |
68 |
Correct |
221 ms |
36872 KB |
Output is correct |
69 |
Correct |
274 ms |
36908 KB |
Output is correct |
70 |
Correct |
251 ms |
37516 KB |
Output is correct |
71 |
Correct |
245 ms |
38204 KB |
Output is correct |
72 |
Correct |
272 ms |
38424 KB |
Output is correct |
73 |
Correct |
261 ms |
43604 KB |
Output is correct |
74 |
Correct |
272 ms |
43612 KB |
Output is correct |
75 |
Correct |
257 ms |
37268 KB |
Output is correct |
76 |
Correct |
259 ms |
39804 KB |
Output is correct |
77 |
Correct |
145 ms |
37340 KB |
Output is correct |
78 |
Correct |
7 ms |
12864 KB |
Output is correct |
79 |
Correct |
7 ms |
13224 KB |
Output is correct |
80 |
Correct |
6 ms |
13116 KB |
Output is correct |
81 |
Correct |
6 ms |
13132 KB |
Output is correct |
82 |
Correct |
6 ms |
13140 KB |
Output is correct |
83 |
Correct |
7 ms |
13268 KB |
Output is correct |
84 |
Correct |
7 ms |
13268 KB |
Output is correct |
85 |
Correct |
6 ms |
13140 KB |
Output is correct |
86 |
Correct |
45 ms |
14728 KB |
Output is correct |
87 |
Correct |
37 ms |
14588 KB |
Output is correct |
88 |
Correct |
45 ms |
14916 KB |
Output is correct |
89 |
Correct |
38 ms |
14888 KB |
Output is correct |
90 |
Correct |
33 ms |
15108 KB |
Output is correct |
91 |
Correct |
28 ms |
15120 KB |
Output is correct |
92 |
Correct |
34 ms |
14684 KB |
Output is correct |
93 |
Correct |
206 ms |
36240 KB |
Output is correct |
94 |
Correct |
188 ms |
36376 KB |
Output is correct |
95 |
Correct |
192 ms |
36432 KB |
Output is correct |
96 |
Correct |
244 ms |
38648 KB |
Output is correct |
97 |
Correct |
337 ms |
42576 KB |
Output is correct |
98 |
Correct |
307 ms |
42560 KB |
Output is correct |
99 |
Correct |
38 ms |
29980 KB |
Output is correct |
100 |
Correct |
384 ms |
43328 KB |
Output is correct |
101 |
Correct |
460 ms |
43328 KB |
Output is correct |
102 |
Correct |
337 ms |
43600 KB |
Output is correct |
103 |
Correct |
390 ms |
42968 KB |
Output is correct |
104 |
Correct |
350 ms |
42948 KB |
Output is correct |
105 |
Correct |
358 ms |
42912 KB |
Output is correct |
106 |
Correct |
212 ms |
35476 KB |
Output is correct |
107 |
Correct |
321 ms |
37048 KB |
Output is correct |
108 |
Correct |
218 ms |
37164 KB |
Output is correct |
109 |
Correct |
241 ms |
37052 KB |
Output is correct |
110 |
Correct |
335 ms |
43220 KB |
Output is correct |
111 |
Correct |
337 ms |
43192 KB |
Output is correct |