#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> PII;
int main() {
int N, Q;
cin >> N >> Q;
vector<PII> ints(N);
for (int i = 0; i < N; ++i) {
cin >> ints[i].first >> ints[i].second;
}
vector<PII> sweep(2 * N);
for (int i = 0; i < N; ++i) {
sweep[2 * i] = {ints[i].first, i};
// We need all closing to appear after all opening.
sweep[2 * i + 1] = {ints[i].second, N + i};
}
sort(sweep.begin(), sweep.end());
vector<int> parent(N);
vector<int> anc(17 * N);
// {closing pos, index}, sorted decreasing closing pos. In addition, we
// only keep those candidates that are useful: the ones that have smallest
// open positions are allowed to have largest closing pos.
vector<PII> candidates;
for (int i = 2 * N - 1; i >= 0; --i) {
auto& p = sweep[i];
bool close = p.second >= N;
int x = p.second % N; // index
if (close) { // [?, ints[x].second]
// cout << "[?, " << ints[x].second << "]" << endl;
// remove useless candidate, add new candidate
while (!candidates.empty()) {
auto& c = candidates.back();
if (ints[x].first <= ints[c.second].first) {
candidates.pop_back();
} else {
break;
}
}
if (candidates.empty() ||
ints[x].second < ints[candidates.back().second].second) {
candidates.emplace_back(ints[x].second, x);
}
// // cout << "candidates: ";
// for (auto& c : candidates) {
// cout << c.first << "," << c.second << " ";
// }
// cout << endl;
} else { // [ints[x].first, ?]
// cout << "[" << ints[x].first << ",?]" << endl;
// We compute x's parent: the candidate with largest second, but
// smaller or equal than x's second.
// (careful: we are using greater; also, since we will never match,
// we don't care whether to use upper_bound or lower_bound). This
// returns the left-most valid candidate, or .end() if it does not exist.
auto it = upper_bound(candidates.begin(), candidates.end(),
PII{ints[x].second + 1, -1}, greater<PII>());
if (it == candidates.end()) {
parent[x] = x; // all are invalid
} else {
// cout << "Trobat it: " << it->first << "," << it->second <<
// endl;
parent[x] = it->second;
}
}
}
// for (int i = 0; i < N; ++i) {
// cout << "parent " << i << ": " << parent[i] << endl;
// }
for (int i = 0; i < N; ++i) {
anc[i] = parent[i];
}
for (int lev = 1; lev < 17; ++lev) {
for (int i = 0; i < N; ++i) {
anc[lev * N + i] = anc[(lev - 1) * N + anc[(lev - 1) * N + i]];
}
}
// for (int lev = 0; lev < 17; ++lev) {
// for (int i = 0; i < N; ++i) {
// cout << " " << anc[lev * N + i];
// }
// cout << endl;
// }
// cout << endl;
for (int i = 0; i < Q; ++i) {
int s, e;
cin >> s >> e;
--s;
--e;
if (s == e) {
cout << 0 << endl;
} else if (ints[e].first <= ints[s].second &&
ints[s].second <= ints[e].second) {
cout << 1 << endl;
} else if (ints[s].second > ints[e].second) {
cout << "impossible" << endl;
} else {
int x = e;
int steps = 0;
for (int l = 16; l >= 0; --l) {
// cout << x << ": ";
int x2 = anc[l * N + x];
if (ints[s].second < ints[x2].first) {
// x guaranteed to never contain s.
x = x2;
steps += (1 << l);
}
// cout << x << "... ";
}
// cout << endl;
if (parent[x] != x) {
// 1 extra step possible
cout << (steps + 2) << endl;
} else {
cout << "impossible" << endl;
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
234 ms |
11204 KB |
Output is correct |
3 |
Correct |
261 ms |
13488 KB |
Output is correct |
4 |
Correct |
262 ms |
13728 KB |
Output is correct |
5 |
Correct |
253 ms |
13612 KB |
Output is correct |
6 |
Correct |
246 ms |
13584 KB |
Output is correct |
7 |
Correct |
247 ms |
13604 KB |
Output is correct |
8 |
Correct |
255 ms |
14036 KB |
Output is correct |
9 |
Correct |
251 ms |
13904 KB |
Output is correct |
10 |
Correct |
255 ms |
14120 KB |
Output is correct |
11 |
Correct |
249 ms |
13800 KB |
Output is correct |
12 |
Correct |
211 ms |
13136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
161 ms |
2032 KB |
Output is correct |
20 |
Correct |
158 ms |
2028 KB |
Output is correct |
21 |
Correct |
171 ms |
2596 KB |
Output is correct |
22 |
Correct |
156 ms |
2640 KB |
Output is correct |
23 |
Correct |
154 ms |
2492 KB |
Output is correct |
24 |
Correct |
157 ms |
2384 KB |
Output is correct |
25 |
Correct |
154 ms |
2132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
74 ms |
11060 KB |
Output is correct |
20 |
Correct |
63 ms |
10428 KB |
Output is correct |
21 |
Correct |
75 ms |
10940 KB |
Output is correct |
22 |
Correct |
74 ms |
11088 KB |
Output is correct |
23 |
Correct |
73 ms |
10968 KB |
Output is correct |
24 |
Correct |
75 ms |
11092 KB |
Output is correct |
25 |
Correct |
52 ms |
10436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
236 ms |
11208 KB |
Output is correct |
2 |
Correct |
253 ms |
13472 KB |
Output is correct |
3 |
Correct |
263 ms |
13404 KB |
Output is correct |
4 |
Correct |
253 ms |
13908 KB |
Output is correct |
5 |
Correct |
250 ms |
13856 KB |
Output is correct |
6 |
Correct |
251 ms |
13588 KB |
Output is correct |
7 |
Correct |
255 ms |
13776 KB |
Output is correct |
8 |
Correct |
240 ms |
13636 KB |
Output is correct |
9 |
Correct |
73 ms |
11696 KB |
Output is correct |
10 |
Correct |
240 ms |
13116 KB |
Output is correct |
11 |
Correct |
231 ms |
13104 KB |
Output is correct |
12 |
Correct |
243 ms |
13100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
234 ms |
11204 KB |
Output is correct |
3 |
Correct |
261 ms |
13488 KB |
Output is correct |
4 |
Correct |
262 ms |
13728 KB |
Output is correct |
5 |
Correct |
253 ms |
13612 KB |
Output is correct |
6 |
Correct |
246 ms |
13584 KB |
Output is correct |
7 |
Correct |
247 ms |
13604 KB |
Output is correct |
8 |
Correct |
255 ms |
14036 KB |
Output is correct |
9 |
Correct |
251 ms |
13904 KB |
Output is correct |
10 |
Correct |
255 ms |
14120 KB |
Output is correct |
11 |
Correct |
249 ms |
13800 KB |
Output is correct |
12 |
Correct |
211 ms |
13136 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
161 ms |
2032 KB |
Output is correct |
32 |
Correct |
158 ms |
2028 KB |
Output is correct |
33 |
Correct |
171 ms |
2596 KB |
Output is correct |
34 |
Correct |
156 ms |
2640 KB |
Output is correct |
35 |
Correct |
154 ms |
2492 KB |
Output is correct |
36 |
Correct |
157 ms |
2384 KB |
Output is correct |
37 |
Correct |
154 ms |
2132 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
74 ms |
11060 KB |
Output is correct |
48 |
Correct |
63 ms |
10428 KB |
Output is correct |
49 |
Correct |
75 ms |
10940 KB |
Output is correct |
50 |
Correct |
74 ms |
11088 KB |
Output is correct |
51 |
Correct |
73 ms |
10968 KB |
Output is correct |
52 |
Correct |
75 ms |
11092 KB |
Output is correct |
53 |
Correct |
52 ms |
10436 KB |
Output is correct |
54 |
Correct |
236 ms |
11208 KB |
Output is correct |
55 |
Correct |
253 ms |
13472 KB |
Output is correct |
56 |
Correct |
263 ms |
13404 KB |
Output is correct |
57 |
Correct |
253 ms |
13908 KB |
Output is correct |
58 |
Correct |
250 ms |
13856 KB |
Output is correct |
59 |
Correct |
251 ms |
13588 KB |
Output is correct |
60 |
Correct |
255 ms |
13776 KB |
Output is correct |
61 |
Correct |
240 ms |
13636 KB |
Output is correct |
62 |
Correct |
73 ms |
11696 KB |
Output is correct |
63 |
Correct |
240 ms |
13116 KB |
Output is correct |
64 |
Correct |
231 ms |
13104 KB |
Output is correct |
65 |
Correct |
243 ms |
13100 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
247 ms |
13396 KB |
Output is correct |
68 |
Correct |
245 ms |
13340 KB |
Output is correct |
69 |
Correct |
269 ms |
13512 KB |
Output is correct |
70 |
Correct |
244 ms |
13392 KB |
Output is correct |
71 |
Correct |
253 ms |
13908 KB |
Output is correct |
72 |
Correct |
244 ms |
13800 KB |
Output is correct |
73 |
Correct |
247 ms |
13864 KB |
Output is correct |
74 |
Correct |
250 ms |
14256 KB |
Output is correct |
75 |
Correct |
249 ms |
13908 KB |
Output is correct |
76 |
Correct |
258 ms |
13904 KB |
Output is correct |
77 |
Correct |
207 ms |
13216 KB |
Output is correct |
78 |
Correct |
1 ms |
348 KB |
Output is correct |
79 |
Correct |
1 ms |
348 KB |
Output is correct |
80 |
Correct |
1 ms |
348 KB |
Output is correct |
81 |
Correct |
1 ms |
348 KB |
Output is correct |
82 |
Correct |
1 ms |
448 KB |
Output is correct |
83 |
Correct |
1 ms |
464 KB |
Output is correct |
84 |
Correct |
1 ms |
460 KB |
Output is correct |
85 |
Correct |
1 ms |
348 KB |
Output is correct |
86 |
Correct |
159 ms |
2360 KB |
Output is correct |
87 |
Correct |
169 ms |
2400 KB |
Output is correct |
88 |
Correct |
169 ms |
2644 KB |
Output is correct |
89 |
Correct |
163 ms |
2696 KB |
Output is correct |
90 |
Correct |
156 ms |
2492 KB |
Output is correct |
91 |
Correct |
161 ms |
2452 KB |
Output is correct |
92 |
Correct |
155 ms |
2144 KB |
Output is correct |
93 |
Correct |
75 ms |
11604 KB |
Output is correct |
94 |
Correct |
59 ms |
10840 KB |
Output is correct |
95 |
Correct |
75 ms |
11600 KB |
Output is correct |
96 |
Correct |
74 ms |
11608 KB |
Output is correct |
97 |
Correct |
72 ms |
11620 KB |
Output is correct |
98 |
Correct |
72 ms |
11600 KB |
Output is correct |
99 |
Correct |
53 ms |
10932 KB |
Output is correct |
100 |
Correct |
249 ms |
13848 KB |
Output is correct |
101 |
Correct |
240 ms |
13488 KB |
Output is correct |
102 |
Correct |
237 ms |
13724 KB |
Output is correct |
103 |
Correct |
243 ms |
13140 KB |
Output is correct |
104 |
Correct |
236 ms |
13120 KB |
Output is correct |
105 |
Correct |
241 ms |
13304 KB |
Output is correct |
106 |
Correct |
230 ms |
12884 KB |
Output is correct |
107 |
Correct |
261 ms |
12928 KB |
Output is correct |
108 |
Correct |
238 ms |
13548 KB |
Output is correct |
109 |
Correct |
240 ms |
13608 KB |
Output is correct |
110 |
Correct |
245 ms |
13560 KB |
Output is correct |
111 |
Correct |
234 ms |
13396 KB |
Output is correct |