#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (int) 1e5 + 7;
int n;
int q;
struct T {
int l;
int r;
int ind;
};
bool operator < (T a, T b) {
if (a.r != b.r) {
return a.r < b.r;
} else {
return a.l < b.l;
}
}
T a[N];
int l[N];
int r[N];
int where[N];
int bs(int x) {
int low = 1, high = n, sol = 0;
while (low <= high) {
int mid = (low + high) / 2;
if (a[mid].r <= x) {
sol = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return sol;
}
pair<int, int> unite(pair<int, int> a, pair<int, int> b) {
return {min(a.first, b.first), max(a.second, b.second)};
}
const int K = 20;
pair<int, int> tab[K][N];
pair<int, int> segt[K][4 * N];
void build(int v, int tl, int tr, int k) {
if (tl == tr) {
segt[k][v] = tab[k][tl];
} else {
int tm = (tl + tr) / 2;
build(2 * v, tl, tm, k);
build(2 * v + 1, tm + 1, tr, k);
segt[k][v] = unite(segt[k][2 * v], segt[k][2 * v + 1]);
}
}
pair<int, int> get(int v, int tl, int tr, int l, int r, int k) {
if (l <= tl && tr <= r) {
return segt[k][v];
}
int tm = (tl + tr) / 2;
if (l <= tm && r <= tm) {
return get(2 * v, tl, tm, l, r, k);
}
if (tm + 1 <= l && tm + 1 <= r) {
return get(2 * v + 1, tm + 1, tr, l, r, k);
}
return unite(get(2 * v, tl, tm, l, tm, k), get(2 * v + 1, tm + 1, tr, tm + 1, r, k));
}
pair<int, int> get(int l, int r, int k) {
return get(1, 1, n, l, r, k);
}
void build(int k) {
build(1, 1, n, k);
}
pair<int, int> go_up(pair<int, int> cur, int k) {
return get(cur.first, cur.second, k);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen ("input", "r", stdin);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i].l >> a[i].r;
a[i].ind = i;
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) {
where[a[i].ind] = i;
}
for (int i = 1; i <= n; i++) {
l[i] = bs(a[i].l - 1) + 1;
r[i] = bs(a[i].r);
tab[0][i] = {l[i], r[i]};
}
build(0);
for (int k = 1; k < K; k++) {
for (int i = 1; i <= n; i++) {
tab[k][i] = go_up(tab[k - 1][i], k - 1);
}
build(k);
}
for (int iq = 1; iq <= q; iq++) {
int ff, ss;
cin >> ff >> ss;
int sol = 1;
pair<int, int> now = {ss, ss};
pair<int, int> nxt = go_up(now, K - 1);
if (nxt.second < ff || nxt.first > ff) {
cout << "impossible\n";
continue;
}
if (ff == ss) {
cout << "0\n";
continue;
}
for (int k = K - 1; k >= 0; k--) {
pair<int, int> nxt = go_up(now, k);
if (nxt.second < ff || nxt.first > ff) {
sol += (1 << k);
now = nxt;
}
}
cout << sol << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
57688 KB |
Output is correct |
2 |
Incorrect |
509 ms |
84668 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
57688 KB |
Output is correct |
2 |
Correct |
6 ms |
57688 KB |
Output is correct |
3 |
Incorrect |
8 ms |
57948 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
57688 KB |
Output is correct |
2 |
Correct |
6 ms |
57688 KB |
Output is correct |
3 |
Incorrect |
8 ms |
57948 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
57688 KB |
Output is correct |
2 |
Correct |
6 ms |
57688 KB |
Output is correct |
3 |
Incorrect |
8 ms |
57948 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
495 ms |
84696 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
57688 KB |
Output is correct |
2 |
Incorrect |
509 ms |
84668 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |