This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 2e5 + 10;
pair <int, int> seg[N * 4];
void upd(int id, int l, int r, int p, pair <int, int> st) {
if (l > p || r < p)
return;
if (l == r) {
seg[id] = min(seg[id], st);
return;
}
int m = (l + r) / 2;
upd(id * 2, l, m, p, st);
upd(id * 2 + 1, m + 1, r, p, st);
seg[id] = min(seg[id * 2], seg[id * 2 + 1]);
}
pair <int, int> get(int id, int l, int r, int u, int v) {
if (l > v || r < u)
return make_pair(1e8, 0);
if (l >= u && r <= v)
return seg[id];
int m = (l + r) / 2;
return min(get(id * 2, l, m, u, v), get(id * 2 + 1, m + 1, r, u, v));
}
int l[N], r[N];
pair <int, int> save[N];
int sp[N][20];
ll solve() {
for (int i = 1; i < N * 4; i++)
seg[i] = make_pair(1e9, 0);
int n, q;
cin >> n >> q;
vector <int> save;
for (int i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
save.pb(l[i]);
save.pb(r[i]);
}
uniquev(save);
for (int i = 1; i <= n; i++) {
l[i] = lower_bound(all(save), l[i]) - save.begin() + 1;
r[i] = lower_bound(all(save), r[i]) - save.begin() + 1;
upd(1, 1, N, r[i], make_pair(l[i], i));
}
for (int i = 1; i <= n; i++) {
sp[i][0] = get(1, 1, N, l[i], r[i]).s;
}
for (int j = 1; j < 20; j++) {
for (int i = 1; i <= n; i++) {
sp[i][j] = sp[sp[i][j - 1]][j - 1];
}
}
while (q--) {
int a, b;
cin >> a >> b;
if (a == b) {
cout << 0 << '\n';
continue;
}
if (r[b] < r[a]) {
cout << "impossible\n";
continue;
}
if (l[a] >= r[b]) {
cout << 1 << '\n';
continue;
}
int ans = 0;
for (int i = 19; i >= 0; i--) {
if (r[a] < l[sp[b][i]]) {
ans |= (1 << i);
b = sp[b][i];
}
}
if (ans > N) {
cout << "impossible\n";
continue;
}
cout << ans + 2 << '\n';
}
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) {
solve();
//cout << solve() << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |