#include <bits/stdc++.h>
using namespace std;
class RangeMin {
public:
int size_ = 1;
vector<pair<int, int>> dat;
void init(int sz) {
while (size_ <= sz) size_ *= 2;
dat.resize(size_ * 2, make_pair(0, 0));
}
void update(int pos, pair<int, int> val) {
pos += size_;
dat[pos] = val;
while (pos >= 2) {
pos >>= 1;
dat[pos] = min(dat[pos * 2 + 0], dat[pos * 2 + 1]);
}
}
pair<int, int> query_(int l, int r, int a, int b, int u) {
if (l <= a && b <= r) return dat[u];
if (r <= a || b <= l) return make_pair(2000000000, -1);
pair<int, int> v1 = query_(l, r, a, (a + b) / 2, u * 2 + 0);
pair<int, int> v2 = query_(l, r, (a + b) / 2, b, u * 2 + 1);
return min(v1, v2);
}
pair<int, int> query(int l, int r) {
return query_(l, r, 0, size_, 1);
}
};
int N, S[1 << 19], T[1 << 19];
int Q, X[1 << 19], Y[1 << 19];
int Prev[1 << 19][22];
int solve(int stt, int goa) {
if (T[stt] > T[goa]) return -1;
if (stt == goa) return 0;
if (T[stt] >= S[goa]) return 1;
// Doubling
int cx = goa, cur = 0;
for (int d = 21; d >= 0; d--) {
int nex = Prev[cx][d];
if (S[nex] > T[stt]) { cx = nex; cur += (1 << d); }
}
if (cur >= (1 << 21)) return -1;
return cur + 2;
}
int main() {
// Step 1. Input
cin >> N >> Q;
for (int i = 1; i <= N; i++) cin >> S[i] >> T[i];
for (int i = 1; i <= Q; i++) cin >> X[i] >> Y[i];
// Step 2. Sorting
vector<tuple<int, int, int>> List;
for (int i = 1; i <= N; i++) List.push_back(make_tuple(T[i], S[i], i));
sort(List.begin(), List.end());
RangeMin Z; Z.init(List.size() + 2);
for (int i = 0; i < (int)List.size(); i++) {
Z.update(i, make_pair(get<1>(List[i]), get<2>(List[i])));
}
// Step 3. Get Prev
for (int i = 0; i < (int)List.size(); i++) {
int idx = get<2>(List[i]);
int pos1 = lower_bound(List.begin(), List.end(), make_tuple(S[idx], -1, -1)) - List.begin();
pair<int, int> ret = Z.query(pos1, i);
if (ret.first == 2000000000) Prev[idx][0] = idx;
else Prev[idx][0] = ret.second;
}
// Step 4. Doubling
for (int d = 1; d <= 21; d++) {
for (int i = 1; i <= N; i++) Prev[i][d] = Prev[Prev[i][d - 1]][d - 1];
}
// Step 5. Answer Query
for (int i = 1; i <= Q; i++) {
int ret = solve(X[i], Y[i]);
if (ret == -1) cout << "impossible" << endl;
else cout << ret << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
246 ms |
26312 KB |
Output is correct |
3 |
Correct |
267 ms |
26480 KB |
Output is correct |
4 |
Correct |
276 ms |
26248 KB |
Output is correct |
5 |
Correct |
256 ms |
26388 KB |
Output is correct |
6 |
Correct |
253 ms |
26564 KB |
Output is correct |
7 |
Correct |
253 ms |
26304 KB |
Output is correct |
8 |
Correct |
248 ms |
26816 KB |
Output is correct |
9 |
Correct |
253 ms |
26820 KB |
Output is correct |
10 |
Correct |
255 ms |
26472 KB |
Output is correct |
11 |
Correct |
261 ms |
26720 KB |
Output is correct |
12 |
Correct |
270 ms |
25836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8648 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8792 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8648 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8792 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
3 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
3 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8540 KB |
Output is correct |
18 |
Correct |
2 ms |
8540 KB |
Output is correct |
19 |
Correct |
156 ms |
12628 KB |
Output is correct |
20 |
Correct |
154 ms |
12624 KB |
Output is correct |
21 |
Correct |
159 ms |
13136 KB |
Output is correct |
22 |
Correct |
152 ms |
13032 KB |
Output is correct |
23 |
Correct |
184 ms |
12832 KB |
Output is correct |
24 |
Correct |
157 ms |
12924 KB |
Output is correct |
25 |
Correct |
153 ms |
12340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8648 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8792 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8536 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
3 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8656 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
3 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8644 KB |
Output is correct |
18 |
Correct |
2 ms |
8540 KB |
Output is correct |
19 |
Correct |
97 ms |
24604 KB |
Output is correct |
20 |
Correct |
79 ms |
23756 KB |
Output is correct |
21 |
Correct |
103 ms |
24260 KB |
Output is correct |
22 |
Correct |
96 ms |
24520 KB |
Output is correct |
23 |
Correct |
89 ms |
24516 KB |
Output is correct |
24 |
Correct |
124 ms |
24344 KB |
Output is correct |
25 |
Correct |
70 ms |
23700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
251 ms |
26340 KB |
Output is correct |
2 |
Correct |
260 ms |
26308 KB |
Output is correct |
3 |
Correct |
279 ms |
26128 KB |
Output is correct |
4 |
Correct |
279 ms |
26652 KB |
Output is correct |
5 |
Correct |
261 ms |
26716 KB |
Output is correct |
6 |
Correct |
279 ms |
26348 KB |
Output is correct |
7 |
Correct |
259 ms |
26224 KB |
Output is correct |
8 |
Correct |
264 ms |
26496 KB |
Output is correct |
9 |
Correct |
89 ms |
24384 KB |
Output is correct |
10 |
Correct |
255 ms |
26056 KB |
Output is correct |
11 |
Correct |
261 ms |
25792 KB |
Output is correct |
12 |
Correct |
279 ms |
26044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
246 ms |
26312 KB |
Output is correct |
3 |
Correct |
267 ms |
26480 KB |
Output is correct |
4 |
Correct |
276 ms |
26248 KB |
Output is correct |
5 |
Correct |
256 ms |
26388 KB |
Output is correct |
6 |
Correct |
253 ms |
26564 KB |
Output is correct |
7 |
Correct |
253 ms |
26304 KB |
Output is correct |
8 |
Correct |
248 ms |
26816 KB |
Output is correct |
9 |
Correct |
253 ms |
26820 KB |
Output is correct |
10 |
Correct |
255 ms |
26472 KB |
Output is correct |
11 |
Correct |
261 ms |
26720 KB |
Output is correct |
12 |
Correct |
270 ms |
25836 KB |
Output is correct |
13 |
Correct |
1 ms |
8536 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
2 ms |
8648 KB |
Output is correct |
17 |
Correct |
2 ms |
8540 KB |
Output is correct |
18 |
Correct |
3 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8792 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
21 |
Correct |
2 ms |
8540 KB |
Output is correct |
22 |
Correct |
1 ms |
8540 KB |
Output is correct |
23 |
Correct |
1 ms |
8540 KB |
Output is correct |
24 |
Correct |
2 ms |
8796 KB |
Output is correct |
25 |
Correct |
3 ms |
8796 KB |
Output is correct |
26 |
Correct |
2 ms |
8540 KB |
Output is correct |
27 |
Correct |
2 ms |
8540 KB |
Output is correct |
28 |
Correct |
3 ms |
8796 KB |
Output is correct |
29 |
Correct |
2 ms |
8540 KB |
Output is correct |
30 |
Correct |
2 ms |
8540 KB |
Output is correct |
31 |
Correct |
156 ms |
12628 KB |
Output is correct |
32 |
Correct |
154 ms |
12624 KB |
Output is correct |
33 |
Correct |
159 ms |
13136 KB |
Output is correct |
34 |
Correct |
152 ms |
13032 KB |
Output is correct |
35 |
Correct |
184 ms |
12832 KB |
Output is correct |
36 |
Correct |
157 ms |
12924 KB |
Output is correct |
37 |
Correct |
153 ms |
12340 KB |
Output is correct |
38 |
Correct |
1 ms |
8536 KB |
Output is correct |
39 |
Correct |
1 ms |
8540 KB |
Output is correct |
40 |
Correct |
3 ms |
8540 KB |
Output is correct |
41 |
Correct |
2 ms |
8656 KB |
Output is correct |
42 |
Correct |
2 ms |
8540 KB |
Output is correct |
43 |
Correct |
3 ms |
8540 KB |
Output is correct |
44 |
Correct |
2 ms |
8796 KB |
Output is correct |
45 |
Correct |
2 ms |
8644 KB |
Output is correct |
46 |
Correct |
2 ms |
8540 KB |
Output is correct |
47 |
Correct |
97 ms |
24604 KB |
Output is correct |
48 |
Correct |
79 ms |
23756 KB |
Output is correct |
49 |
Correct |
103 ms |
24260 KB |
Output is correct |
50 |
Correct |
96 ms |
24520 KB |
Output is correct |
51 |
Correct |
89 ms |
24516 KB |
Output is correct |
52 |
Correct |
124 ms |
24344 KB |
Output is correct |
53 |
Correct |
70 ms |
23700 KB |
Output is correct |
54 |
Correct |
251 ms |
26340 KB |
Output is correct |
55 |
Correct |
260 ms |
26308 KB |
Output is correct |
56 |
Correct |
279 ms |
26128 KB |
Output is correct |
57 |
Correct |
279 ms |
26652 KB |
Output is correct |
58 |
Correct |
261 ms |
26716 KB |
Output is correct |
59 |
Correct |
279 ms |
26348 KB |
Output is correct |
60 |
Correct |
259 ms |
26224 KB |
Output is correct |
61 |
Correct |
264 ms |
26496 KB |
Output is correct |
62 |
Correct |
89 ms |
24384 KB |
Output is correct |
63 |
Correct |
255 ms |
26056 KB |
Output is correct |
64 |
Correct |
261 ms |
25792 KB |
Output is correct |
65 |
Correct |
279 ms |
26044 KB |
Output is correct |
66 |
Correct |
1 ms |
8536 KB |
Output is correct |
67 |
Correct |
278 ms |
26428 KB |
Output is correct |
68 |
Correct |
248 ms |
26308 KB |
Output is correct |
69 |
Correct |
273 ms |
26308 KB |
Output is correct |
70 |
Correct |
256 ms |
26308 KB |
Output is correct |
71 |
Correct |
256 ms |
26380 KB |
Output is correct |
72 |
Correct |
259 ms |
26580 KB |
Output is correct |
73 |
Correct |
250 ms |
26740 KB |
Output is correct |
74 |
Correct |
249 ms |
26856 KB |
Output is correct |
75 |
Correct |
270 ms |
26524 KB |
Output is correct |
76 |
Correct |
273 ms |
26628 KB |
Output is correct |
77 |
Correct |
226 ms |
25916 KB |
Output is correct |
78 |
Correct |
1 ms |
8540 KB |
Output is correct |
79 |
Correct |
2 ms |
8668 KB |
Output is correct |
80 |
Correct |
2 ms |
8796 KB |
Output is correct |
81 |
Correct |
2 ms |
8540 KB |
Output is correct |
82 |
Correct |
2 ms |
8656 KB |
Output is correct |
83 |
Correct |
2 ms |
8540 KB |
Output is correct |
84 |
Correct |
2 ms |
8796 KB |
Output is correct |
85 |
Correct |
2 ms |
8540 KB |
Output is correct |
86 |
Correct |
157 ms |
12448 KB |
Output is correct |
87 |
Correct |
152 ms |
12624 KB |
Output is correct |
88 |
Correct |
157 ms |
12888 KB |
Output is correct |
89 |
Correct |
159 ms |
12884 KB |
Output is correct |
90 |
Correct |
151 ms |
12628 KB |
Output is correct |
91 |
Correct |
152 ms |
12812 KB |
Output is correct |
92 |
Correct |
156 ms |
12564 KB |
Output is correct |
93 |
Correct |
100 ms |
24600 KB |
Output is correct |
94 |
Correct |
80 ms |
23784 KB |
Output is correct |
95 |
Correct |
104 ms |
24260 KB |
Output is correct |
96 |
Correct |
105 ms |
24836 KB |
Output is correct |
97 |
Correct |
88 ms |
24568 KB |
Output is correct |
98 |
Correct |
99 ms |
24520 KB |
Output is correct |
99 |
Correct |
70 ms |
23744 KB |
Output is correct |
100 |
Correct |
263 ms |
26632 KB |
Output is correct |
101 |
Correct |
271 ms |
26308 KB |
Output is correct |
102 |
Correct |
321 ms |
26608 KB |
Output is correct |
103 |
Correct |
264 ms |
26052 KB |
Output is correct |
104 |
Correct |
248 ms |
25732 KB |
Output is correct |
105 |
Correct |
286 ms |
26276 KB |
Output is correct |
106 |
Correct |
256 ms |
25604 KB |
Output is correct |
107 |
Correct |
277 ms |
25796 KB |
Output is correct |
108 |
Correct |
257 ms |
26312 KB |
Output is correct |
109 |
Correct |
256 ms |
26568 KB |
Output is correct |
110 |
Correct |
265 ms |
26304 KB |
Output is correct |
111 |
Correct |
263 ms |
26324 KB |
Output is correct |