#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
int N, Q, S[100005], E[100005], to[100005], dep[100005], anc[100005][25];
bool is_root[100005];
vector<int> disc, adj[100005];
void dfs(int n) {
for (int k = 1; k < 20; k++)
if (anc[n][k - 1] != -1) anc[n][k] = anc[anc[n][k - 1]][k - 1];
for (auto u : adj[n]) {
dep[u] = dep[n] + 1;
anc[u][0] = n;
dfs(u);
}
}
struct node {
node *left, *right;
int S, E;
ii val;
node(int _s, int _e) : S(_s), E(_e), val(LLONG_MAX, -1ll) {
if (S == E) return;
int M = (S + E) >> 1;
left = new node(S, M);
right = new node(M + 1, E);
}
void upd(int p, int v) {
if (S == E) {
val = min(val, mp(::S[v], v));
return;
}
int M = (S + E) >> 1;
if (p <= M) left->upd(p, v);
else right->upd(p, v);
val = min(left->val, right->val);
}
ii qry(int l, int r) {
if (l > E || r < S) return mp(LLONG_MAX, -1ll);
if (l <= S && E <= r) return val;
return min(left->qry(l, r), right->qry(l, r));
}
} *root;
main() {
memset(anc, -1, sizeof anc);
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> Q;
for (int i = 1; i <= N; i++) {
cin >> S[i] >> E[i];
disc.pb(S[i]);
disc.pb(E[i]);
is_root[i] = 1;
}
sort(disc.begin(), disc.end());
disc.erase(unique(disc.begin(), disc.end()), disc.end());
root = new node(1, 2 * N);
for (int i = 1; i <= N; i++) {
S[i] = lower_bound(disc.begin(), disc.end(), S[i]) - disc.begin() + 1;
E[i] = lower_bound(disc.begin(), disc.end(), E[i]) - disc.begin() + 1;
root->upd(E[i], i);
}
for (int i = 1; i <= N; i++) {
to[i] = root->qry(S[i], E[i]).second;
if (S[to[i]] >= S[i]) to[i] = -1;
if (to[i] != -1) {
adj[to[i]].pb(i);
is_root[i] = 0;
}
}
for (int i = 1; i <= N; i++)
if (is_root[i]) dfs(i);
for (int i = 1, s, e; i <= Q; i++) {
cin >> s >> e;
if (E[e] < E[s]) {
cout << "impossible\n";
continue;
} else if (e == s) {
cout << "0\n";
continue;
} else if (S[e] <= E[s] && E[s] <= E[e]) {
cout << "1\n";
continue;
}
int jumps = 0;
for (int k = 19; k >= 0; k--)
if (anc[e][k] != -1 && S[anc[e][k]] > E[s]) {
jumps += dep[e] - dep[anc[e][k]];
e = anc[e][k];
}
e = anc[e][0];
if (e == -1) cout << "impossible\n";
else cout << jumps + 2 << '\n';
}
}
Compilation message
events.cpp:70:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
70 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
22228 KB |
Output is correct |
2 |
Correct |
286 ms |
60484 KB |
Output is correct |
3 |
Correct |
301 ms |
60476 KB |
Output is correct |
4 |
Correct |
349 ms |
60476 KB |
Output is correct |
5 |
Correct |
303 ms |
59836 KB |
Output is correct |
6 |
Correct |
298 ms |
59016 KB |
Output is correct |
7 |
Correct |
316 ms |
58812 KB |
Output is correct |
8 |
Correct |
239 ms |
52384 KB |
Output is correct |
9 |
Correct |
252 ms |
52316 KB |
Output is correct |
10 |
Correct |
312 ms |
58468 KB |
Output is correct |
11 |
Correct |
314 ms |
56668 KB |
Output is correct |
12 |
Correct |
109 ms |
52320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22268 KB |
Output is correct |
2 |
Correct |
13 ms |
22200 KB |
Output is correct |
3 |
Correct |
12 ms |
22612 KB |
Output is correct |
4 |
Correct |
12 ms |
22612 KB |
Output is correct |
5 |
Correct |
14 ms |
22612 KB |
Output is correct |
6 |
Correct |
12 ms |
22484 KB |
Output is correct |
7 |
Correct |
12 ms |
22484 KB |
Output is correct |
8 |
Correct |
14 ms |
22468 KB |
Output is correct |
9 |
Correct |
10 ms |
22484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22268 KB |
Output is correct |
2 |
Correct |
13 ms |
22200 KB |
Output is correct |
3 |
Correct |
12 ms |
22612 KB |
Output is correct |
4 |
Correct |
12 ms |
22612 KB |
Output is correct |
5 |
Correct |
14 ms |
22612 KB |
Output is correct |
6 |
Correct |
12 ms |
22484 KB |
Output is correct |
7 |
Correct |
12 ms |
22484 KB |
Output is correct |
8 |
Correct |
14 ms |
22468 KB |
Output is correct |
9 |
Correct |
10 ms |
22484 KB |
Output is correct |
10 |
Correct |
14 ms |
22260 KB |
Output is correct |
11 |
Correct |
11 ms |
22228 KB |
Output is correct |
12 |
Correct |
12 ms |
22612 KB |
Output is correct |
13 |
Correct |
14 ms |
22512 KB |
Output is correct |
14 |
Correct |
11 ms |
22612 KB |
Output is correct |
15 |
Correct |
12 ms |
22460 KB |
Output is correct |
16 |
Correct |
11 ms |
22484 KB |
Output is correct |
17 |
Correct |
14 ms |
22512 KB |
Output is correct |
18 |
Correct |
14 ms |
22496 KB |
Output is correct |
19 |
Correct |
48 ms |
24332 KB |
Output is correct |
20 |
Correct |
43 ms |
24592 KB |
Output is correct |
21 |
Correct |
46 ms |
24828 KB |
Output is correct |
22 |
Correct |
48 ms |
24576 KB |
Output is correct |
23 |
Correct |
40 ms |
24372 KB |
Output is correct |
24 |
Correct |
41 ms |
24504 KB |
Output is correct |
25 |
Correct |
41 ms |
24140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22268 KB |
Output is correct |
2 |
Correct |
13 ms |
22200 KB |
Output is correct |
3 |
Correct |
12 ms |
22612 KB |
Output is correct |
4 |
Correct |
12 ms |
22612 KB |
Output is correct |
5 |
Correct |
14 ms |
22612 KB |
Output is correct |
6 |
Correct |
12 ms |
22484 KB |
Output is correct |
7 |
Correct |
12 ms |
22484 KB |
Output is correct |
8 |
Correct |
14 ms |
22468 KB |
Output is correct |
9 |
Correct |
10 ms |
22484 KB |
Output is correct |
10 |
Correct |
9 ms |
22228 KB |
Output is correct |
11 |
Correct |
10 ms |
22188 KB |
Output is correct |
12 |
Correct |
12 ms |
22568 KB |
Output is correct |
13 |
Correct |
11 ms |
22612 KB |
Output is correct |
14 |
Correct |
12 ms |
22612 KB |
Output is correct |
15 |
Correct |
11 ms |
22484 KB |
Output is correct |
16 |
Correct |
14 ms |
22572 KB |
Output is correct |
17 |
Correct |
12 ms |
22484 KB |
Output is correct |
18 |
Correct |
11 ms |
22484 KB |
Output is correct |
19 |
Correct |
252 ms |
59844 KB |
Output is correct |
20 |
Correct |
237 ms |
59868 KB |
Output is correct |
21 |
Correct |
218 ms |
52784 KB |
Output is correct |
22 |
Correct |
244 ms |
55708 KB |
Output is correct |
23 |
Correct |
240 ms |
53888 KB |
Output is correct |
24 |
Correct |
317 ms |
55184 KB |
Output is correct |
25 |
Correct |
71 ms |
51248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
286 ms |
60496 KB |
Output is correct |
2 |
Correct |
280 ms |
60504 KB |
Output is correct |
3 |
Correct |
342 ms |
60408 KB |
Output is correct |
4 |
Correct |
234 ms |
52324 KB |
Output is correct |
5 |
Correct |
315 ms |
58408 KB |
Output is correct |
6 |
Correct |
393 ms |
56004 KB |
Output is correct |
7 |
Correct |
376 ms |
55900 KB |
Output is correct |
8 |
Correct |
313 ms |
54788 KB |
Output is correct |
9 |
Correct |
253 ms |
53868 KB |
Output is correct |
10 |
Correct |
340 ms |
55596 KB |
Output is correct |
11 |
Correct |
349 ms |
54832 KB |
Output is correct |
12 |
Correct |
370 ms |
55588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
22228 KB |
Output is correct |
2 |
Correct |
286 ms |
60484 KB |
Output is correct |
3 |
Correct |
301 ms |
60476 KB |
Output is correct |
4 |
Correct |
349 ms |
60476 KB |
Output is correct |
5 |
Correct |
303 ms |
59836 KB |
Output is correct |
6 |
Correct |
298 ms |
59016 KB |
Output is correct |
7 |
Correct |
316 ms |
58812 KB |
Output is correct |
8 |
Correct |
239 ms |
52384 KB |
Output is correct |
9 |
Correct |
252 ms |
52316 KB |
Output is correct |
10 |
Correct |
312 ms |
58468 KB |
Output is correct |
11 |
Correct |
314 ms |
56668 KB |
Output is correct |
12 |
Correct |
109 ms |
52320 KB |
Output is correct |
13 |
Correct |
11 ms |
22268 KB |
Output is correct |
14 |
Correct |
13 ms |
22200 KB |
Output is correct |
15 |
Correct |
12 ms |
22612 KB |
Output is correct |
16 |
Correct |
12 ms |
22612 KB |
Output is correct |
17 |
Correct |
14 ms |
22612 KB |
Output is correct |
18 |
Correct |
12 ms |
22484 KB |
Output is correct |
19 |
Correct |
12 ms |
22484 KB |
Output is correct |
20 |
Correct |
14 ms |
22468 KB |
Output is correct |
21 |
Correct |
10 ms |
22484 KB |
Output is correct |
22 |
Correct |
14 ms |
22260 KB |
Output is correct |
23 |
Correct |
11 ms |
22228 KB |
Output is correct |
24 |
Correct |
12 ms |
22612 KB |
Output is correct |
25 |
Correct |
14 ms |
22512 KB |
Output is correct |
26 |
Correct |
11 ms |
22612 KB |
Output is correct |
27 |
Correct |
12 ms |
22460 KB |
Output is correct |
28 |
Correct |
11 ms |
22484 KB |
Output is correct |
29 |
Correct |
14 ms |
22512 KB |
Output is correct |
30 |
Correct |
14 ms |
22496 KB |
Output is correct |
31 |
Correct |
48 ms |
24332 KB |
Output is correct |
32 |
Correct |
43 ms |
24592 KB |
Output is correct |
33 |
Correct |
46 ms |
24828 KB |
Output is correct |
34 |
Correct |
48 ms |
24576 KB |
Output is correct |
35 |
Correct |
40 ms |
24372 KB |
Output is correct |
36 |
Correct |
41 ms |
24504 KB |
Output is correct |
37 |
Correct |
41 ms |
24140 KB |
Output is correct |
38 |
Correct |
9 ms |
22228 KB |
Output is correct |
39 |
Correct |
10 ms |
22188 KB |
Output is correct |
40 |
Correct |
12 ms |
22568 KB |
Output is correct |
41 |
Correct |
11 ms |
22612 KB |
Output is correct |
42 |
Correct |
12 ms |
22612 KB |
Output is correct |
43 |
Correct |
11 ms |
22484 KB |
Output is correct |
44 |
Correct |
14 ms |
22572 KB |
Output is correct |
45 |
Correct |
12 ms |
22484 KB |
Output is correct |
46 |
Correct |
11 ms |
22484 KB |
Output is correct |
47 |
Correct |
252 ms |
59844 KB |
Output is correct |
48 |
Correct |
237 ms |
59868 KB |
Output is correct |
49 |
Correct |
218 ms |
52784 KB |
Output is correct |
50 |
Correct |
244 ms |
55708 KB |
Output is correct |
51 |
Correct |
240 ms |
53888 KB |
Output is correct |
52 |
Correct |
317 ms |
55184 KB |
Output is correct |
53 |
Correct |
71 ms |
51248 KB |
Output is correct |
54 |
Correct |
286 ms |
60496 KB |
Output is correct |
55 |
Correct |
280 ms |
60504 KB |
Output is correct |
56 |
Correct |
342 ms |
60408 KB |
Output is correct |
57 |
Correct |
234 ms |
52324 KB |
Output is correct |
58 |
Correct |
315 ms |
58408 KB |
Output is correct |
59 |
Correct |
393 ms |
56004 KB |
Output is correct |
60 |
Correct |
376 ms |
55900 KB |
Output is correct |
61 |
Correct |
313 ms |
54788 KB |
Output is correct |
62 |
Correct |
253 ms |
53868 KB |
Output is correct |
63 |
Correct |
340 ms |
55596 KB |
Output is correct |
64 |
Correct |
349 ms |
54832 KB |
Output is correct |
65 |
Correct |
370 ms |
55588 KB |
Output is correct |
66 |
Correct |
12 ms |
22228 KB |
Output is correct |
67 |
Correct |
277 ms |
60436 KB |
Output is correct |
68 |
Correct |
300 ms |
60432 KB |
Output is correct |
69 |
Correct |
340 ms |
60480 KB |
Output is correct |
70 |
Correct |
292 ms |
59696 KB |
Output is correct |
71 |
Correct |
284 ms |
59060 KB |
Output is correct |
72 |
Correct |
310 ms |
58824 KB |
Output is correct |
73 |
Correct |
238 ms |
52336 KB |
Output is correct |
74 |
Correct |
271 ms |
52316 KB |
Output is correct |
75 |
Correct |
338 ms |
58432 KB |
Output is correct |
76 |
Correct |
301 ms |
56668 KB |
Output is correct |
77 |
Correct |
105 ms |
52352 KB |
Output is correct |
78 |
Correct |
13 ms |
22228 KB |
Output is correct |
79 |
Correct |
12 ms |
22612 KB |
Output is correct |
80 |
Correct |
11 ms |
22612 KB |
Output is correct |
81 |
Correct |
11 ms |
22612 KB |
Output is correct |
82 |
Correct |
11 ms |
22484 KB |
Output is correct |
83 |
Correct |
11 ms |
22484 KB |
Output is correct |
84 |
Correct |
11 ms |
22556 KB |
Output is correct |
85 |
Correct |
12 ms |
22612 KB |
Output is correct |
86 |
Correct |
62 ms |
24316 KB |
Output is correct |
87 |
Correct |
46 ms |
24544 KB |
Output is correct |
88 |
Correct |
46 ms |
24908 KB |
Output is correct |
89 |
Correct |
36 ms |
24500 KB |
Output is correct |
90 |
Correct |
38 ms |
24436 KB |
Output is correct |
91 |
Correct |
33 ms |
24412 KB |
Output is correct |
92 |
Correct |
48 ms |
24172 KB |
Output is correct |
93 |
Correct |
244 ms |
59820 KB |
Output is correct |
94 |
Correct |
254 ms |
59812 KB |
Output is correct |
95 |
Correct |
222 ms |
52840 KB |
Output is correct |
96 |
Correct |
231 ms |
55744 KB |
Output is correct |
97 |
Correct |
247 ms |
53816 KB |
Output is correct |
98 |
Correct |
297 ms |
55108 KB |
Output is correct |
99 |
Correct |
78 ms |
51264 KB |
Output is correct |
100 |
Correct |
337 ms |
55940 KB |
Output is correct |
101 |
Correct |
364 ms |
56060 KB |
Output is correct |
102 |
Correct |
325 ms |
54848 KB |
Output is correct |
103 |
Correct |
346 ms |
55656 KB |
Output is correct |
104 |
Correct |
330 ms |
54812 KB |
Output is correct |
105 |
Correct |
348 ms |
55648 KB |
Output is correct |
106 |
Correct |
284 ms |
58916 KB |
Output is correct |
107 |
Correct |
342 ms |
60640 KB |
Output is correct |
108 |
Correct |
245 ms |
53588 KB |
Output is correct |
109 |
Correct |
238 ms |
53688 KB |
Output is correct |
110 |
Correct |
318 ms |
53896 KB |
Output is correct |
111 |
Correct |
309 ms |
53800 KB |
Output is correct |