#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int maxn = 1e5 + 5;
struct SegTree {
int n;
vector<pii> tree;
SegTree(int _n) : n(_n), tree(4*_n+5, { 1e9, 0 }) {}
void update(int u, int tl, int tr, int p, int L, int id) {
if(tl == tr) {
tree[u] = min(tree[u], pii{ L, id });
} else {
int tm = (tl + tr) / 2;
if(p <= tm) update(2*u, tl, tm, p, L, id);
else update(2*u+1, tm+1, tr, p, L, id);
tree[u] = min(tree[2*u], tree[2*u+1]);
}
}
pii query(int u, int tl, int tr, int l, int r) {
if(tl > tr || l > tr || tl > r) return { 1e9, 0 };
if(l <= tl && tr <= r) return tree[u];
int tm = (tl + tr) / 2;
return min(query(2*u, tl, tm, l, r), query(2*u+1, tm+1, tr, l, r));
}
void update(int p, int L, int id) { update(1, 0, n-1, p, L, id); }
pii query(int l, int r) { return query(1, 0, n-1, l, r); }
};
vector<int> graph[maxn];
int up[maxn][20];
signed main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
int n, q;
cin >> n >> q;
vector<array<int, 3> > v(n+1); set<int> s;
vector<int> L(n+1), R(n+1);
for(int i=1; i<=n; i++) {
cin >> v[i][1] >> v[i][0]; v[i][2] = i;
s.insert(v[i][1]); s.insert(v[i][0]);
L[i] = v[i][1]; R[i] = v[i][0];
}
vector<int> comp(s.begin(), s.end());
for(auto &[r, l, id] : v) {
if(l == 0 && r == 0) continue;
l = lower_bound(comp.begin(), comp.end(), l) - comp.begin();
r = lower_bound(comp.begin(), comp.end(), r) - comp.begin();
}
SegTree tree(2*n);
sort(v.begin(), v.end());
for(auto &[r, l, id] : v) {
auto [x, y] = tree.query(l, r);
if(y) up[id][0] = y;
tree.update(r, l, id);
}
for(int j=1; j<20; j++)
for(int i=1; i<=n; i++) up[i][j] = up[up[i][j-1]][j-1];
while(q--) {
int a, b;
cin >> a >> b;
if(a == b) {
cout << 0 << '\n';
continue;
}
if(R[a] > R[b]) {
cout << "impossible\n";
continue;
}
int ans = 0;
for(int j=19; j>=0; j--)
if(up[b][j] != 0 && L[up[b][j]] > R[a]) b = up[b][j], ans |= (1 << j);
if(a == b) cout << ans << '\n';
else if(L[b] <= R[a]) cout << ans + 1 << '\n';
else if(up[b][0] && L[up[b][0]] <= R[a]) cout << ans + 2 << '\n';
else cout << "impossible\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4184 KB |
Output is correct |
2 |
Correct |
105 ms |
27596 KB |
Output is correct |
3 |
Correct |
103 ms |
27600 KB |
Output is correct |
4 |
Correct |
116 ms |
27472 KB |
Output is correct |
5 |
Correct |
111 ms |
28064 KB |
Output is correct |
6 |
Correct |
110 ms |
28620 KB |
Output is correct |
7 |
Correct |
108 ms |
28772 KB |
Output is correct |
8 |
Correct |
113 ms |
33104 KB |
Output is correct |
9 |
Correct |
110 ms |
33136 KB |
Output is correct |
10 |
Correct |
110 ms |
27868 KB |
Output is correct |
11 |
Correct |
115 ms |
29868 KB |
Output is correct |
12 |
Correct |
70 ms |
27216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4188 KB |
Output is correct |
2 |
Correct |
1 ms |
4188 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4404 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4188 KB |
Output is correct |
2 |
Correct |
1 ms |
4188 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4404 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4188 KB |
Output is correct |
11 |
Correct |
1 ms |
4188 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4344 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
15 |
Correct |
2 ms |
4484 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
21 ms |
8772 KB |
Output is correct |
20 |
Correct |
20 ms |
8536 KB |
Output is correct |
21 |
Correct |
23 ms |
8796 KB |
Output is correct |
22 |
Correct |
17 ms |
9052 KB |
Output is correct |
23 |
Correct |
19 ms |
9052 KB |
Output is correct |
24 |
Correct |
20 ms |
9040 KB |
Output is correct |
25 |
Correct |
19 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4188 KB |
Output is correct |
2 |
Correct |
1 ms |
4188 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4404 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4188 KB |
Output is correct |
11 |
Correct |
1 ms |
4188 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
92 ms |
25892 KB |
Output is correct |
20 |
Correct |
80 ms |
25028 KB |
Output is correct |
21 |
Correct |
88 ms |
25680 KB |
Output is correct |
22 |
Correct |
95 ms |
27728 KB |
Output is correct |
23 |
Correct |
121 ms |
30920 KB |
Output is correct |
24 |
Correct |
120 ms |
30912 KB |
Output is correct |
25 |
Correct |
33 ms |
20088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
27632 KB |
Output is correct |
2 |
Correct |
100 ms |
27472 KB |
Output is correct |
3 |
Correct |
108 ms |
27472 KB |
Output is correct |
4 |
Correct |
110 ms |
33064 KB |
Output is correct |
5 |
Correct |
106 ms |
27980 KB |
Output is correct |
6 |
Correct |
132 ms |
32656 KB |
Output is correct |
7 |
Correct |
137 ms |
32728 KB |
Output is correct |
8 |
Correct |
141 ms |
32808 KB |
Output is correct |
9 |
Correct |
106 ms |
30800 KB |
Output is correct |
10 |
Correct |
136 ms |
32336 KB |
Output is correct |
11 |
Correct |
149 ms |
32216 KB |
Output is correct |
12 |
Correct |
143 ms |
32428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4184 KB |
Output is correct |
2 |
Correct |
105 ms |
27596 KB |
Output is correct |
3 |
Correct |
103 ms |
27600 KB |
Output is correct |
4 |
Correct |
116 ms |
27472 KB |
Output is correct |
5 |
Correct |
111 ms |
28064 KB |
Output is correct |
6 |
Correct |
110 ms |
28620 KB |
Output is correct |
7 |
Correct |
108 ms |
28772 KB |
Output is correct |
8 |
Correct |
113 ms |
33104 KB |
Output is correct |
9 |
Correct |
110 ms |
33136 KB |
Output is correct |
10 |
Correct |
110 ms |
27868 KB |
Output is correct |
11 |
Correct |
115 ms |
29868 KB |
Output is correct |
12 |
Correct |
70 ms |
27216 KB |
Output is correct |
13 |
Correct |
1 ms |
4188 KB |
Output is correct |
14 |
Correct |
1 ms |
4188 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
2 ms |
4444 KB |
Output is correct |
19 |
Correct |
2 ms |
4444 KB |
Output is correct |
20 |
Correct |
2 ms |
4404 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4188 KB |
Output is correct |
23 |
Correct |
1 ms |
4188 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
2 ms |
4344 KB |
Output is correct |
26 |
Correct |
2 ms |
4444 KB |
Output is correct |
27 |
Correct |
2 ms |
4484 KB |
Output is correct |
28 |
Correct |
2 ms |
4444 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
21 ms |
8772 KB |
Output is correct |
32 |
Correct |
20 ms |
8536 KB |
Output is correct |
33 |
Correct |
23 ms |
8796 KB |
Output is correct |
34 |
Correct |
17 ms |
9052 KB |
Output is correct |
35 |
Correct |
19 ms |
9052 KB |
Output is correct |
36 |
Correct |
20 ms |
9040 KB |
Output is correct |
37 |
Correct |
19 ms |
8540 KB |
Output is correct |
38 |
Correct |
1 ms |
4188 KB |
Output is correct |
39 |
Correct |
1 ms |
4188 KB |
Output is correct |
40 |
Correct |
1 ms |
4444 KB |
Output is correct |
41 |
Correct |
2 ms |
4444 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
2 ms |
4444 KB |
Output is correct |
44 |
Correct |
2 ms |
4444 KB |
Output is correct |
45 |
Correct |
2 ms |
4444 KB |
Output is correct |
46 |
Correct |
1 ms |
4444 KB |
Output is correct |
47 |
Correct |
92 ms |
25892 KB |
Output is correct |
48 |
Correct |
80 ms |
25028 KB |
Output is correct |
49 |
Correct |
88 ms |
25680 KB |
Output is correct |
50 |
Correct |
95 ms |
27728 KB |
Output is correct |
51 |
Correct |
121 ms |
30920 KB |
Output is correct |
52 |
Correct |
120 ms |
30912 KB |
Output is correct |
53 |
Correct |
33 ms |
20088 KB |
Output is correct |
54 |
Correct |
97 ms |
27632 KB |
Output is correct |
55 |
Correct |
100 ms |
27472 KB |
Output is correct |
56 |
Correct |
108 ms |
27472 KB |
Output is correct |
57 |
Correct |
110 ms |
33064 KB |
Output is correct |
58 |
Correct |
106 ms |
27980 KB |
Output is correct |
59 |
Correct |
132 ms |
32656 KB |
Output is correct |
60 |
Correct |
137 ms |
32728 KB |
Output is correct |
61 |
Correct |
141 ms |
32808 KB |
Output is correct |
62 |
Correct |
106 ms |
30800 KB |
Output is correct |
63 |
Correct |
136 ms |
32336 KB |
Output is correct |
64 |
Correct |
149 ms |
32216 KB |
Output is correct |
65 |
Correct |
143 ms |
32428 KB |
Output is correct |
66 |
Correct |
1 ms |
4188 KB |
Output is correct |
67 |
Correct |
103 ms |
27608 KB |
Output is correct |
68 |
Correct |
98 ms |
27600 KB |
Output is correct |
69 |
Correct |
108 ms |
27480 KB |
Output is correct |
70 |
Correct |
103 ms |
27984 KB |
Output is correct |
71 |
Correct |
105 ms |
28500 KB |
Output is correct |
72 |
Correct |
110 ms |
28756 KB |
Output is correct |
73 |
Correct |
114 ms |
32988 KB |
Output is correct |
74 |
Correct |
108 ms |
33056 KB |
Output is correct |
75 |
Correct |
106 ms |
27984 KB |
Output is correct |
76 |
Correct |
116 ms |
29832 KB |
Output is correct |
77 |
Correct |
70 ms |
27216 KB |
Output is correct |
78 |
Correct |
1 ms |
4188 KB |
Output is correct |
79 |
Correct |
2 ms |
4444 KB |
Output is correct |
80 |
Correct |
2 ms |
4444 KB |
Output is correct |
81 |
Correct |
2 ms |
4444 KB |
Output is correct |
82 |
Correct |
2 ms |
4440 KB |
Output is correct |
83 |
Correct |
2 ms |
4440 KB |
Output is correct |
84 |
Correct |
2 ms |
4444 KB |
Output is correct |
85 |
Correct |
1 ms |
4444 KB |
Output is correct |
86 |
Correct |
21 ms |
8664 KB |
Output is correct |
87 |
Correct |
19 ms |
8540 KB |
Output is correct |
88 |
Correct |
21 ms |
8796 KB |
Output is correct |
89 |
Correct |
17 ms |
9052 KB |
Output is correct |
90 |
Correct |
18 ms |
9048 KB |
Output is correct |
91 |
Correct |
17 ms |
9052 KB |
Output is correct |
92 |
Correct |
19 ms |
8792 KB |
Output is correct |
93 |
Correct |
81 ms |
25684 KB |
Output is correct |
94 |
Correct |
79 ms |
24924 KB |
Output is correct |
95 |
Correct |
90 ms |
25828 KB |
Output is correct |
96 |
Correct |
86 ms |
27532 KB |
Output is correct |
97 |
Correct |
107 ms |
30800 KB |
Output is correct |
98 |
Correct |
113 ms |
30808 KB |
Output is correct |
99 |
Correct |
32 ms |
19944 KB |
Output is correct |
100 |
Correct |
136 ms |
32756 KB |
Output is correct |
101 |
Correct |
134 ms |
32672 KB |
Output is correct |
102 |
Correct |
141 ms |
32788 KB |
Output is correct |
103 |
Correct |
139 ms |
32336 KB |
Output is correct |
104 |
Correct |
133 ms |
32232 KB |
Output is correct |
105 |
Correct |
130 ms |
32476 KB |
Output is correct |
106 |
Correct |
96 ms |
25680 KB |
Output is correct |
107 |
Correct |
112 ms |
26848 KB |
Output is correct |
108 |
Correct |
102 ms |
27704 KB |
Output is correct |
109 |
Correct |
102 ms |
27728 KB |
Output is correct |
110 |
Correct |
147 ms |
32572 KB |
Output is correct |
111 |
Correct |
147 ms |
32972 KB |
Output is correct |