#include <bits/stdc++.h>
using namespace std;
#define int int64_t
struct seg {
vector<pair<int, int>> tree;
seg(int n) {
tree.assign(n*4, {1e18, -1});
}
void update(int v, int rl, int rr, int pos, pair<int, int> x) {
if (rl == rr) {
tree[v] = x;
return;
}
int rm = (rl + rr) / 2;
if (pos <= rm) update(v*2, rl, rm, pos, x);
else update(v*2+1, rm+1, rr, pos, x);
tree[v] = min(tree[v*2], tree[v*2+1]);
}
pair<int, int> query(int v, int rl, int rr, int ql, int qr) {
if (ql > qr) return {1e18, -1};
if (rl == ql && rr == qr) return tree[v];
int rm = (rl + rr) / 2;
return min(query(v*2, rl, rm, ql, min(rm, qr)), query(v*2+1, rm+1, rr, max(rm+1, ql), qr));
}
};
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int n, q; cin >> n >> q;
vector<tuple<int, int, int>> events(n);
for (int i = 0; i<n; i++) {
cin >> get<1>(events[i]) >> get<0>(events[i]);
get<2>(events[i]) = i;
}
sort(events.begin(), events.end());
vector<pair<int, int>> segtreeRanges;
for (auto i: events) {
int l = upper_bound(events.begin(), events.end(), tuple<int, int, int>{get<1>(i)-1, 1e18, 1e18}) - events.begin();
int r = upper_bound(events.begin(), events.end(), tuple<int, int, int>{get<0>(i), 1e18, 1e18}) - events.begin() - 1;
segtreeRanges.push_back({l, r});
}
for (auto &i: events) swap(get<0>(i), get<1>(i));
seg tree(n);
for (int i = 0; i<n; i++) {
tree.update(1, 0, n-1, i, {get<0>(events[i]), get<2>(events[i])});
}
vector<pair<int, int>> parents(n);
for (int i = 0; i<n; i++) {
parents[get<2>(events[i])] = tree.query(1, 0, n-1, segtreeRanges[i].first, segtreeRanges[i].second);
}
// for (auto i: parents) {
// cerr << i.first << " " << i.second << "\n";
// }
for (auto &i: events) swap(get<0>(i), get<2>(i));
sort(events.begin(), events.end());
for (auto &i: events) swap(get<0>(i), get<2>(i));
vector<vector<int>> jump(n, vector<int>(20));
for (int i = 0; i<20; i++) {
for (int j = 0; j<n; j++) {
if (i == 0) {jump[j][i] = parents[j].second; continue;}
jump[j][i] = jump[jump[j][i-1]][i-1];
}
}
for (int i = 0; i<q; i++) {
int s, e; cin >> s >> e;
s--;
e--;
if (s == e) {cout << 0 << "\n"; continue;}
int cnt = 1;
if (get<0>(events[e]) > get<1>(events[s])) {
for (int i = 19; i>=0; i--) {
if (get<0>(events[jump[e][i]]) > get<1>(events[s])) {
e = jump[e][i];
cnt += pow(2, i);
}
}
e = parents[e].second;
cnt++;
}
if (!(get<0>(events[e]) <= get<1>(events[s]) && get<1>(events[s]) <= get<1>(events[e]))) cout << "impossible\n";
else cout << cnt << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
178 ms |
32248 KB |
Output is correct |
3 |
Correct |
198 ms |
32156 KB |
Output is correct |
4 |
Correct |
285 ms |
32176 KB |
Output is correct |
5 |
Correct |
203 ms |
32240 KB |
Output is correct |
6 |
Correct |
202 ms |
32328 KB |
Output is correct |
7 |
Correct |
244 ms |
32244 KB |
Output is correct |
8 |
Correct |
181 ms |
32644 KB |
Output is correct |
9 |
Correct |
188 ms |
32636 KB |
Output is correct |
10 |
Correct |
232 ms |
32652 KB |
Output is correct |
11 |
Correct |
226 ms |
32588 KB |
Output is correct |
12 |
Correct |
154 ms |
32720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
64 ms |
2272 KB |
Output is correct |
20 |
Correct |
43 ms |
2356 KB |
Output is correct |
21 |
Correct |
42 ms |
2672 KB |
Output is correct |
22 |
Correct |
41 ms |
2624 KB |
Output is correct |
23 |
Correct |
26 ms |
2504 KB |
Output is correct |
24 |
Correct |
24 ms |
2516 KB |
Output is correct |
25 |
Correct |
33 ms |
2120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
124 ms |
31588 KB |
Output is correct |
20 |
Correct |
116 ms |
31520 KB |
Output is correct |
21 |
Correct |
117 ms |
31560 KB |
Output is correct |
22 |
Correct |
122 ms |
31628 KB |
Output is correct |
23 |
Correct |
115 ms |
31560 KB |
Output is correct |
24 |
Correct |
135 ms |
31704 KB |
Output is correct |
25 |
Correct |
82 ms |
31524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
157 ms |
32344 KB |
Output is correct |
2 |
Correct |
186 ms |
32140 KB |
Output is correct |
3 |
Correct |
290 ms |
32324 KB |
Output is correct |
4 |
Correct |
171 ms |
32656 KB |
Output is correct |
5 |
Correct |
230 ms |
32548 KB |
Output is correct |
6 |
Correct |
240 ms |
32476 KB |
Output is correct |
7 |
Correct |
208 ms |
32372 KB |
Output is correct |
8 |
Correct |
239 ms |
32476 KB |
Output is correct |
9 |
Correct |
103 ms |
31652 KB |
Output is correct |
10 |
Correct |
185 ms |
32032 KB |
Output is correct |
11 |
Correct |
175 ms |
31776 KB |
Output is correct |
12 |
Correct |
209 ms |
32016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
178 ms |
32248 KB |
Output is correct |
3 |
Correct |
198 ms |
32156 KB |
Output is correct |
4 |
Correct |
285 ms |
32176 KB |
Output is correct |
5 |
Correct |
203 ms |
32240 KB |
Output is correct |
6 |
Correct |
202 ms |
32328 KB |
Output is correct |
7 |
Correct |
244 ms |
32244 KB |
Output is correct |
8 |
Correct |
181 ms |
32644 KB |
Output is correct |
9 |
Correct |
188 ms |
32636 KB |
Output is correct |
10 |
Correct |
232 ms |
32652 KB |
Output is correct |
11 |
Correct |
226 ms |
32588 KB |
Output is correct |
12 |
Correct |
154 ms |
32720 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
64 ms |
2272 KB |
Output is correct |
32 |
Correct |
43 ms |
2356 KB |
Output is correct |
33 |
Correct |
42 ms |
2672 KB |
Output is correct |
34 |
Correct |
41 ms |
2624 KB |
Output is correct |
35 |
Correct |
26 ms |
2504 KB |
Output is correct |
36 |
Correct |
24 ms |
2516 KB |
Output is correct |
37 |
Correct |
33 ms |
2120 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
2 ms |
596 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
596 KB |
Output is correct |
43 |
Correct |
2 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
124 ms |
31588 KB |
Output is correct |
48 |
Correct |
116 ms |
31520 KB |
Output is correct |
49 |
Correct |
117 ms |
31560 KB |
Output is correct |
50 |
Correct |
122 ms |
31628 KB |
Output is correct |
51 |
Correct |
115 ms |
31560 KB |
Output is correct |
52 |
Correct |
135 ms |
31704 KB |
Output is correct |
53 |
Correct |
82 ms |
31524 KB |
Output is correct |
54 |
Correct |
157 ms |
32344 KB |
Output is correct |
55 |
Correct |
186 ms |
32140 KB |
Output is correct |
56 |
Correct |
290 ms |
32324 KB |
Output is correct |
57 |
Correct |
171 ms |
32656 KB |
Output is correct |
58 |
Correct |
230 ms |
32548 KB |
Output is correct |
59 |
Correct |
240 ms |
32476 KB |
Output is correct |
60 |
Correct |
208 ms |
32372 KB |
Output is correct |
61 |
Correct |
239 ms |
32476 KB |
Output is correct |
62 |
Correct |
103 ms |
31652 KB |
Output is correct |
63 |
Correct |
185 ms |
32032 KB |
Output is correct |
64 |
Correct |
175 ms |
31776 KB |
Output is correct |
65 |
Correct |
209 ms |
32016 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
169 ms |
32232 KB |
Output is correct |
68 |
Correct |
208 ms |
32244 KB |
Output is correct |
69 |
Correct |
287 ms |
32148 KB |
Output is correct |
70 |
Correct |
202 ms |
32192 KB |
Output is correct |
71 |
Correct |
206 ms |
32272 KB |
Output is correct |
72 |
Correct |
190 ms |
32256 KB |
Output is correct |
73 |
Correct |
176 ms |
32644 KB |
Output is correct |
74 |
Correct |
252 ms |
32816 KB |
Output is correct |
75 |
Correct |
196 ms |
32720 KB |
Output is correct |
76 |
Correct |
223 ms |
32616 KB |
Output is correct |
77 |
Correct |
177 ms |
32700 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
596 KB |
Output is correct |
80 |
Correct |
1 ms |
596 KB |
Output is correct |
81 |
Correct |
1 ms |
596 KB |
Output is correct |
82 |
Correct |
1 ms |
596 KB |
Output is correct |
83 |
Correct |
2 ms |
596 KB |
Output is correct |
84 |
Correct |
2 ms |
596 KB |
Output is correct |
85 |
Correct |
1 ms |
596 KB |
Output is correct |
86 |
Correct |
67 ms |
2304 KB |
Output is correct |
87 |
Correct |
59 ms |
2352 KB |
Output is correct |
88 |
Correct |
48 ms |
2584 KB |
Output is correct |
89 |
Correct |
36 ms |
2660 KB |
Output is correct |
90 |
Correct |
25 ms |
2468 KB |
Output is correct |
91 |
Correct |
25 ms |
2508 KB |
Output is correct |
92 |
Correct |
32 ms |
2152 KB |
Output is correct |
93 |
Correct |
117 ms |
31628 KB |
Output is correct |
94 |
Correct |
110 ms |
31528 KB |
Output is correct |
95 |
Correct |
147 ms |
31732 KB |
Output is correct |
96 |
Correct |
117 ms |
31556 KB |
Output is correct |
97 |
Correct |
108 ms |
31604 KB |
Output is correct |
98 |
Correct |
126 ms |
31596 KB |
Output is correct |
99 |
Correct |
79 ms |
31628 KB |
Output is correct |
100 |
Correct |
214 ms |
32396 KB |
Output is correct |
101 |
Correct |
216 ms |
32808 KB |
Output is correct |
102 |
Correct |
195 ms |
33504 KB |
Output is correct |
103 |
Correct |
191 ms |
33540 KB |
Output is correct |
104 |
Correct |
158 ms |
32868 KB |
Output is correct |
105 |
Correct |
186 ms |
33764 KB |
Output is correct |
106 |
Correct |
233 ms |
34176 KB |
Output is correct |
107 |
Correct |
234 ms |
34508 KB |
Output is correct |
108 |
Correct |
189 ms |
34032 KB |
Output is correct |
109 |
Correct |
194 ms |
33028 KB |
Output is correct |
110 |
Correct |
163 ms |
32704 KB |
Output is correct |
111 |
Correct |
171 ms |
32168 KB |
Output is correct |