#include <bits/stdc++.h>
using namespace std;
int s[100005], e[100005];
vector<int> comp;
pair<int, int> tree[4 * 100005];
void update(int x, int l, int r, int loc, pair<int, int> stuff) {
if (l == r) {
tree[x] = min(tree[x], stuff);
return;
}
int mid = (l + r) / 2;
if (loc <= mid) update(2 * x, l, mid, loc, stuff);
else update(2 * x + 1, mid + 1, r, loc, stuff);
tree[x] = min(tree[2 * x], tree[2 * x + 1]);
}
int convert(int x) {
return lower_bound(comp.begin(), comp.end(), x) - comp.begin() + 1;
}
int prevjmp[100005][20];
pair<int, int> get(int x, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) return tree[x];
if (l > qr || r < ql) return {1e9, 1e9};
int mid = (l + r) / 2;
return min(get(2 * x, l, mid, ql, qr), get(2 * x + 1, mid + 1, r, ql, qr));
}
int main() {
int n, q; cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> s[i] >> e[i];
comp.push_back(s[i]);
comp.push_back(e[i]);
}
fill(&tree[0], &tree[0] + sizeof(tree) / sizeof(tree[0]), make_pair(1e9, 1e9));
sort(comp.begin(), comp.end());
comp.resize(unique(comp.begin(), comp.end()) - comp.begin());
int sz = comp.size();
for (int i = 1; i <= n; i++) {
// find furtherst cano back
s[i] = convert(s[i]);
e[i] = convert(e[i]);
update(1, 1, sz, e[i], {s[i], i});
}
for (int i = 1; i <= n; i++) {
prevjmp[i][0] = get(1, 1, sz, s[i], e[i]).second;
}
for (int log = 1; log < 20; log++) {
for (int i = 1; i <= n; i++) {
prevjmp[i][log] = prevjmp[prevjmp[i][log-1]][log-1];
}
}
for (int i = 0; i < q; i++) {
long long ans = 0;
int a, b; cin >> a >> b;
if (a == b) {
cout << "0\n";
continue;
}
if (e[a] > e[b]) {
cout << "impossible\n";
continue;
}
if (s[b] <= e[a] && e[a] <= e[b]) {
cout << "1\n";
continue;
}
for (int log = 19; log >= 0; log--) {
if (s[prevjmp[b][log]] > e[a]) {
b = prevjmp[b][log];
ans += (long long) (1 << log);
}
}
b = prevjmp[b][0];
if (s[b] > e[a]) {
cout << "impossible\n";
continue;
} else {
cout << ans + 2 << "\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
331 ms |
13744 KB |
Output is correct |
3 |
Correct |
377 ms |
13544 KB |
Output is correct |
4 |
Correct |
395 ms |
13448 KB |
Output is correct |
5 |
Correct |
355 ms |
13572 KB |
Output is correct |
6 |
Correct |
423 ms |
13588 KB |
Output is correct |
7 |
Correct |
397 ms |
13648 KB |
Output is correct |
8 |
Runtime error |
93 ms |
10152 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
4 ms |
3540 KB |
Output is correct |
4 |
Correct |
3 ms |
3540 KB |
Output is correct |
5 |
Correct |
3 ms |
3540 KB |
Output is correct |
6 |
Correct |
3 ms |
3540 KB |
Output is correct |
7 |
Correct |
4 ms |
3540 KB |
Output is correct |
8 |
Correct |
3 ms |
3524 KB |
Output is correct |
9 |
Correct |
3 ms |
3540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
4 ms |
3540 KB |
Output is correct |
4 |
Correct |
3 ms |
3540 KB |
Output is correct |
5 |
Correct |
3 ms |
3540 KB |
Output is correct |
6 |
Correct |
3 ms |
3540 KB |
Output is correct |
7 |
Correct |
4 ms |
3540 KB |
Output is correct |
8 |
Correct |
3 ms |
3524 KB |
Output is correct |
9 |
Correct |
3 ms |
3540 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
2 ms |
3412 KB |
Output is correct |
12 |
Correct |
3 ms |
3540 KB |
Output is correct |
13 |
Correct |
3 ms |
3540 KB |
Output is correct |
14 |
Correct |
3 ms |
3540 KB |
Output is correct |
15 |
Correct |
5 ms |
3524 KB |
Output is correct |
16 |
Correct |
4 ms |
3540 KB |
Output is correct |
17 |
Correct |
4 ms |
3540 KB |
Output is correct |
18 |
Correct |
3 ms |
3524 KB |
Output is correct |
19 |
Correct |
184 ms |
5436 KB |
Output is correct |
20 |
Correct |
181 ms |
5504 KB |
Output is correct |
21 |
Correct |
184 ms |
5692 KB |
Output is correct |
22 |
Correct |
187 ms |
5812 KB |
Output is correct |
23 |
Correct |
180 ms |
5612 KB |
Output is correct |
24 |
Correct |
202 ms |
5548 KB |
Output is correct |
25 |
Correct |
180 ms |
5276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
4 ms |
3540 KB |
Output is correct |
4 |
Correct |
3 ms |
3540 KB |
Output is correct |
5 |
Correct |
3 ms |
3540 KB |
Output is correct |
6 |
Correct |
3 ms |
3540 KB |
Output is correct |
7 |
Correct |
4 ms |
3540 KB |
Output is correct |
8 |
Correct |
3 ms |
3524 KB |
Output is correct |
9 |
Correct |
3 ms |
3540 KB |
Output is correct |
10 |
Correct |
3 ms |
3412 KB |
Output is correct |
11 |
Correct |
2 ms |
3412 KB |
Output is correct |
12 |
Correct |
3 ms |
3540 KB |
Output is correct |
13 |
Correct |
4 ms |
3540 KB |
Output is correct |
14 |
Correct |
3 ms |
3540 KB |
Output is correct |
15 |
Correct |
4 ms |
3524 KB |
Output is correct |
16 |
Correct |
3 ms |
3524 KB |
Output is correct |
17 |
Correct |
3 ms |
3540 KB |
Output is correct |
18 |
Correct |
2 ms |
3540 KB |
Output is correct |
19 |
Correct |
173 ms |
14896 KB |
Output is correct |
20 |
Correct |
139 ms |
14048 KB |
Output is correct |
21 |
Correct |
167 ms |
14804 KB |
Output is correct |
22 |
Runtime error |
117 ms |
12144 KB |
Execution killed with signal 11 |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
368 ms |
13444 KB |
Output is correct |
2 |
Correct |
366 ms |
13480 KB |
Output is correct |
3 |
Correct |
410 ms |
13504 KB |
Output is correct |
4 |
Runtime error |
98 ms |
10092 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
331 ms |
13744 KB |
Output is correct |
3 |
Correct |
377 ms |
13544 KB |
Output is correct |
4 |
Correct |
395 ms |
13448 KB |
Output is correct |
5 |
Correct |
355 ms |
13572 KB |
Output is correct |
6 |
Correct |
423 ms |
13588 KB |
Output is correct |
7 |
Correct |
397 ms |
13648 KB |
Output is correct |
8 |
Runtime error |
93 ms |
10152 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |