#include <bits/stdc++.h>
using namespace std;
// ordered set whith s.order_of_key(x) method, which returns rank of element x in set s
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
*/
// pair printing
template <class T, class U>
ostream& operator<<(ostream& out, const pair<T, U> &par) {out << "(" << par.first << "; " << par.second << ")"; return out;}
// set printing
template <class T>
ostream& operator<<(ostream& out, const set<T> &cont) { out << "{"; for(const auto &x:cont) out << x << ", "; out << "}"; return out; }
// map printing
template <class T, class U>
ostream& operator<<(ostream& out, const map<T, U> &cont) {out << "{"; for(const auto &x:cont) out << x << ", "; out << "}"; return out; }
// unordered_set printing
template <class T>
ostream& operator<<(ostream& out, const unordered_set<T> &cont) {out << "{";for(const auto &x:cont) out << x << ", ";out << "}";return out;}
// unordered_map printing
template <class T, class U>
ostream& operator<<(ostream& out, const unordered_map<T, U> &cont) {out << "{";for(const auto &x:cont) out << x << ", ";out << "}";return out;}
// vector printing
template<class T>
ostream& operator<<(ostream& out, const vector<T> &cont){ out << "["; for (const auto &x:cont) out << x << ", "; out << "]"; return out;}
#define print(x) cout << (x) << endl;
#define dmp(x) cerr << #x << " = " << x << endl
#define dmpn(x) cerr << #x << " = " << x << "; "
#define dmpl(x) cerr << "Line " << __LINE__ << ": " << #x << " = " << x << endl
#define int long long
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define pb push_back
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define contains(s,x) ((s).find(x) != (s).end())
const int MOD = 998244353;
struct vertex {
int idx, start, end, parent, depth;
vector<int> children;
vector<int> binjumps;
};
int N, Q, LG = 20;
vector<vertex> g;
int eval(int x) {
if (x == INT_MAX) return INT_MAX;
return g[x].start;
}
struct SegTree {
int N;
vector<int> tree;
SegTree(vector<int> &base) {
N = 1 << (32 - __builtin_clz(sz(base)));
tree.assign(2 * N, INT_MAX);
for (int i = 0; i < sz(base); i++) tree[N + i] = base[i];
for (int i = N - 1; i > 0; i--) {
if (eval(tree[i * 2]) < eval(tree[i * 2 + 1])) tree[i] = tree[i * 2];
else tree[i] = tree[i * 2 + 1];
}
// dmp(tree);
}
int query(int l, int r, int i, int j, int idx) {
if (l <= i && j <= r) {
return tree[idx];
} else if ((i <= l && l < j) || (i < r && r <= j)) {
int mid = (i + j) / 2;
int x1 = query(l, r, i, mid, idx * 2);
int x2 = query(l, r, mid, j, idx * 2 + 1);
if (eval(x1) < eval(x2)) return x1;
else return x2;
}
return INT_MAX;
}
int bin_search_l(int base_row_idx, int mn) {
int l = N, r = N + base_row_idx;
while (r - l > 0) {
int mid = (l + r) / 2;
int val = g[tree[mid]].end;
if (val < mn) {
l = mid + 1;
} else {
r = mid;
}
}
return r - N;
}
};
void dfs(int v, int p, int root, int depth) {
g[v].depth = depth;
g[v].binjumps.assign(LG, -1);
g[v].binjumps[0] = p;
for (int i = 1; i < LG; i++) {
int last = g[v].binjumps[i - 1];
if (last == -1) break;
g[v].binjumps[i] = g[last].binjumps[i - 1];
}
for (int i = 0; i < LG; i++) if (g[v].binjumps[i] == -1) g[v].binjumps[i] = root;
for (int u : g[v].children) if (u != p) {
dfs(u, v, root, depth + 1);
}
}
int relative_pos(int a, int b) {
if (g[a].start <= g[b].end && g[b].end <= g[a].end) return 0;
if (g[b].start <= g[a].end && g[a].end <= g[b].end) return 0;
if (g[a].end < g[b].end) return 1;
return -1;
}
void solve() {
cin >> N >> Q;
vector<pii> ends;
for (int i = 0; i < N; i++) {
int start, end;
cin >> start >> end;
g.pb({ i, start, end, -1 });
ends.pb({ end, i });
}
sort(all(ends));
vector<int> indices;
for (pii pr : ends) indices.pb(pr.ss);
SegTree st(indices);
for (pii pr : ends) {
int v = pr.ss;
pii pr2 = { pr.ff, INT_MAX };
int r = lower_bound(all(ends), pr2) - ends.begin() - 1;
int l = st.bin_search_l(r, g[v].start);
// dmpn(v); dmpn(l); dmp(r);
if (l == r) continue;
int parent = st.query(l, r + 1, 0, st.N, 1);
if (g[parent].start < g[v].start) {
g[v].parent = parent;
g[parent].children.pb(v);
}
}
for (int i = 0; i < N; i++) {
// if is root
if (g[i].parent == -1) dfs(i, -1, i, 0);
}
/*
for (int i = 0; i < N; i++) {
cout << i << ": ";
for (int j : g[i].children) cout << j << ", ";
cout << endl;
}
*/
while (Q--) {
int s, e;
cin >> s >> e;
s--;
e--;
if (g[s].end > g[e].end) {
cout << "impossible" << endl;
continue;
}
int v = e, ans = INT_MAX;
if (relative_pos(s, e) == 0) ans = 1;
if (s == e) ans = 0;
for (int k = LG - 1; k >= 0; k--) {
int anc = g[v].binjumps[k];
// dmpn(k); dmpn(v); dmp(anc);
int pos = relative_pos(s, anc);
// dmpn(k); dmp(anc);
if (pos == 1) {
v = anc;
} else if (pos == 0) {
int dist = g[e].depth - g[anc].depth;
if (s != anc) dist++;
// dmp(dist);
ans = min(ans, dist);
}
}
if (ans == INT_MAX) cout << "impossible" << endl;
else cout << ans << endl;
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
292 ms |
46288 KB |
Output is correct |
3 |
Correct |
302 ms |
46728 KB |
Output is correct |
4 |
Correct |
355 ms |
46852 KB |
Output is correct |
5 |
Correct |
296 ms |
45516 KB |
Output is correct |
6 |
Correct |
294 ms |
45196 KB |
Output is correct |
7 |
Correct |
269 ms |
43708 KB |
Output is correct |
8 |
Correct |
192 ms |
36144 KB |
Output is correct |
9 |
Correct |
196 ms |
35552 KB |
Output is correct |
10 |
Correct |
311 ms |
42712 KB |
Output is correct |
11 |
Correct |
269 ms |
41680 KB |
Output is correct |
12 |
Correct |
190 ms |
35360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
856 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
856 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
856 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
856 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
139 ms |
3628 KB |
Output is correct |
20 |
Correct |
147 ms |
4396 KB |
Output is correct |
21 |
Correct |
144 ms |
4140 KB |
Output is correct |
22 |
Correct |
141 ms |
3960 KB |
Output is correct |
23 |
Correct |
135 ms |
3628 KB |
Output is correct |
24 |
Correct |
133 ms |
3732 KB |
Output is correct |
25 |
Correct |
140 ms |
3500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
856 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
800 KB |
Output is correct |
12 |
Correct |
2 ms |
856 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
856 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
159 ms |
44496 KB |
Output is correct |
20 |
Correct |
140 ms |
44756 KB |
Output is correct |
21 |
Correct |
98 ms |
35024 KB |
Output is correct |
22 |
Correct |
107 ms |
39120 KB |
Output is correct |
23 |
Correct |
89 ms |
34764 KB |
Output is correct |
24 |
Correct |
116 ms |
37680 KB |
Output is correct |
25 |
Correct |
62 ms |
32668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
304 ms |
47568 KB |
Output is correct |
2 |
Correct |
288 ms |
45800 KB |
Output is correct |
3 |
Correct |
377 ms |
47032 KB |
Output is correct |
4 |
Correct |
190 ms |
35244 KB |
Output is correct |
5 |
Correct |
322 ms |
42716 KB |
Output is correct |
6 |
Correct |
316 ms |
40144 KB |
Output is correct |
7 |
Correct |
289 ms |
38544 KB |
Output is correct |
8 |
Correct |
292 ms |
37580 KB |
Output is correct |
9 |
Correct |
105 ms |
34828 KB |
Output is correct |
10 |
Correct |
269 ms |
38472 KB |
Output is correct |
11 |
Correct |
258 ms |
38740 KB |
Output is correct |
12 |
Correct |
265 ms |
39160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
292 ms |
46288 KB |
Output is correct |
3 |
Correct |
302 ms |
46728 KB |
Output is correct |
4 |
Correct |
355 ms |
46852 KB |
Output is correct |
5 |
Correct |
296 ms |
45516 KB |
Output is correct |
6 |
Correct |
294 ms |
45196 KB |
Output is correct |
7 |
Correct |
269 ms |
43708 KB |
Output is correct |
8 |
Correct |
192 ms |
36144 KB |
Output is correct |
9 |
Correct |
196 ms |
35552 KB |
Output is correct |
10 |
Correct |
311 ms |
42712 KB |
Output is correct |
11 |
Correct |
269 ms |
41680 KB |
Output is correct |
12 |
Correct |
190 ms |
35360 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
856 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
856 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
856 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
600 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
31 |
Correct |
139 ms |
3628 KB |
Output is correct |
32 |
Correct |
147 ms |
4396 KB |
Output is correct |
33 |
Correct |
144 ms |
4140 KB |
Output is correct |
34 |
Correct |
141 ms |
3960 KB |
Output is correct |
35 |
Correct |
135 ms |
3628 KB |
Output is correct |
36 |
Correct |
133 ms |
3732 KB |
Output is correct |
37 |
Correct |
140 ms |
3500 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
800 KB |
Output is correct |
40 |
Correct |
2 ms |
856 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
42 |
Correct |
1 ms |
856 KB |
Output is correct |
43 |
Correct |
1 ms |
600 KB |
Output is correct |
44 |
Correct |
1 ms |
600 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
600 KB |
Output is correct |
47 |
Correct |
159 ms |
44496 KB |
Output is correct |
48 |
Correct |
140 ms |
44756 KB |
Output is correct |
49 |
Correct |
98 ms |
35024 KB |
Output is correct |
50 |
Correct |
107 ms |
39120 KB |
Output is correct |
51 |
Correct |
89 ms |
34764 KB |
Output is correct |
52 |
Correct |
116 ms |
37680 KB |
Output is correct |
53 |
Correct |
62 ms |
32668 KB |
Output is correct |
54 |
Correct |
304 ms |
47568 KB |
Output is correct |
55 |
Correct |
288 ms |
45800 KB |
Output is correct |
56 |
Correct |
377 ms |
47032 KB |
Output is correct |
57 |
Correct |
190 ms |
35244 KB |
Output is correct |
58 |
Correct |
322 ms |
42716 KB |
Output is correct |
59 |
Correct |
316 ms |
40144 KB |
Output is correct |
60 |
Correct |
289 ms |
38544 KB |
Output is correct |
61 |
Correct |
292 ms |
37580 KB |
Output is correct |
62 |
Correct |
105 ms |
34828 KB |
Output is correct |
63 |
Correct |
269 ms |
38472 KB |
Output is correct |
64 |
Correct |
258 ms |
38740 KB |
Output is correct |
65 |
Correct |
265 ms |
39160 KB |
Output is correct |
66 |
Correct |
0 ms |
344 KB |
Output is correct |
67 |
Correct |
299 ms |
47312 KB |
Output is correct |
68 |
Correct |
297 ms |
46628 KB |
Output is correct |
69 |
Correct |
393 ms |
46756 KB |
Output is correct |
70 |
Correct |
291 ms |
45076 KB |
Output is correct |
71 |
Correct |
272 ms |
45072 KB |
Output is correct |
72 |
Correct |
265 ms |
43388 KB |
Output is correct |
73 |
Correct |
207 ms |
36792 KB |
Output is correct |
74 |
Correct |
193 ms |
36816 KB |
Output is correct |
75 |
Correct |
314 ms |
43616 KB |
Output is correct |
76 |
Correct |
255 ms |
41680 KB |
Output is correct |
77 |
Correct |
186 ms |
35280 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
2 ms |
1112 KB |
Output is correct |
80 |
Correct |
2 ms |
612 KB |
Output is correct |
81 |
Correct |
1 ms |
856 KB |
Output is correct |
82 |
Correct |
2 ms |
600 KB |
Output is correct |
83 |
Correct |
1 ms |
600 KB |
Output is correct |
84 |
Correct |
1 ms |
600 KB |
Output is correct |
85 |
Correct |
1 ms |
604 KB |
Output is correct |
86 |
Correct |
139 ms |
3628 KB |
Output is correct |
87 |
Correct |
142 ms |
4052 KB |
Output is correct |
88 |
Correct |
143 ms |
4288 KB |
Output is correct |
89 |
Correct |
135 ms |
3756 KB |
Output is correct |
90 |
Correct |
145 ms |
3752 KB |
Output is correct |
91 |
Correct |
138 ms |
3700 KB |
Output is correct |
92 |
Correct |
140 ms |
3252 KB |
Output is correct |
93 |
Correct |
152 ms |
44472 KB |
Output is correct |
94 |
Correct |
164 ms |
43688 KB |
Output is correct |
95 |
Correct |
96 ms |
34004 KB |
Output is correct |
96 |
Correct |
106 ms |
37660 KB |
Output is correct |
97 |
Correct |
95 ms |
35500 KB |
Output is correct |
98 |
Correct |
120 ms |
36304 KB |
Output is correct |
99 |
Correct |
48 ms |
33748 KB |
Output is correct |
100 |
Correct |
365 ms |
39884 KB |
Output is correct |
101 |
Correct |
331 ms |
38136 KB |
Output is correct |
102 |
Correct |
274 ms |
37584 KB |
Output is correct |
103 |
Correct |
275 ms |
38288 KB |
Output is correct |
104 |
Correct |
258 ms |
37164 KB |
Output is correct |
105 |
Correct |
247 ms |
37588 KB |
Output is correct |
106 |
Correct |
353 ms |
43700 KB |
Output is correct |
107 |
Correct |
349 ms |
46284 KB |
Output is correct |
108 |
Correct |
266 ms |
35792 KB |
Output is correct |
109 |
Correct |
264 ms |
36556 KB |
Output is correct |
110 |
Correct |
298 ms |
35536 KB |
Output is correct |
111 |
Correct |
269 ms |
37072 KB |
Output is correct |