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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
int N, Q, S[100005], E[100005], to[100005], dep[100005], anc[100005][25];
bool is_root[100005];
vector<int> disc, adj[100005];
void dfs(int n) {
for (int k = 1; k < 20; k++)
if (anc[n][k - 1] != -1) anc[n][k] = anc[anc[n][k - 1]][k - 1];
for (auto u : adj[n]) {
dep[u] = dep[n] + 1;
anc[u][0] = n;
dfs(u);
}
}
struct node {
node *left, *right;
int S, E;
ii val;
node(int _s, int _e) : S(_s), E(_e), val(LLONG_MAX, -1ll) {
if (S == E) return;
int M = (S + E) >> 1;
left = new node(S, M);
right = new node(M + 1, E);
}
void upd(int p, int v) {
if (S == E) {
val = min(val, mp(::S[v], v));
return;
}
int M = (S + E) >> 1;
if (p <= M) left->upd(p, v);
else right->upd(p, v);
val = min(left->val, right->val);
}
ii qry(int l, int r) {
if (l > E || r < S) return mp(LLONG_MAX, -1ll);
if (l <= S && E <= r) return val;
return min(left->qry(l, r), right->qry(l, r));
}
} *root;
main() {
memset(anc, -1, sizeof anc);
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> Q;
for (int i = 1; i <= N; i++) {
cin >> S[i] >> E[i];
disc.pb(S[i]);
disc.pb(E[i]);
is_root[i] = 1;
}
sort(disc.begin(), disc.end());
disc.erase(unique(disc.begin(), disc.end()), disc.end());
root = new node(1, 2 * N);
for (int i = 1; i <= N; i++) {
S[i] = lower_bound(disc.begin(), disc.end(), S[i]) - disc.begin() + 1;
E[i] = lower_bound(disc.begin(), disc.end(), E[i]) - disc.begin() + 1;
root->upd(E[i], i);
}
for (int i = 1; i <= N; i++) {
to[i] = root->qry(S[i], E[i]).second;
if (S[to[i]] >= S[i]) to[i] = -1;
if (to[i] != -1) {
adj[to[i]].pb(i);
is_root[i] = 0;
}
}
for (int i = 1; i <= N; i++)
if (is_root[i]) dfs(i);
for (int i = 1, s, e; i <= Q; i++) {
cin >> s >> e;
if (E[e] < E[s]) {
cout << "impossible\n";
continue;
} else if (e == s) {
cout << "0\n";
continue;
} else if (S[e] <= E[s] && E[s] <= E[e]) {
cout << "1\n";
continue;
}
int jumps = 0;
for (int k = 19; k >= 0; k--)
if (anc[e][k] != -1 && S[anc[e][k]] > E[s]) {
jumps += dep[e] - dep[anc[e][k]];
e = anc[e][k];
}
e = anc[e][0];
if (e == -1) cout << "impossible\n";
else cout << jumps + 2 << '\n';
}
}
Compilation message (stderr)
events.cpp:70:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
70 | main() {
| ^~~~
# | 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... |