#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[b] <= r[a]) {
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 |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
124 ms |
18784 KB |
Output is correct |
3 |
Correct |
130 ms |
18788 KB |
Output is correct |
4 |
Correct |
160 ms |
18640 KB |
Output is correct |
5 |
Correct |
128 ms |
21984 KB |
Output is correct |
6 |
Correct |
139 ms |
21972 KB |
Output is correct |
7 |
Correct |
157 ms |
21968 KB |
Output is correct |
8 |
Correct |
128 ms |
22220 KB |
Output is correct |
9 |
Correct |
140 ms |
22180 KB |
Output is correct |
10 |
Correct |
137 ms |
22220 KB |
Output is correct |
11 |
Correct |
124 ms |
22116 KB |
Output is correct |
12 |
Correct |
77 ms |
21660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
3 ms |
8796 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
5 |
Correct |
4 ms |
8796 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
3 ms |
8796 KB |
Output is correct |
8 |
Correct |
5 ms |
8796 KB |
Output is correct |
9 |
Correct |
3 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
3 ms |
8796 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
5 |
Correct |
4 ms |
8796 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
3 ms |
8796 KB |
Output is correct |
8 |
Correct |
5 ms |
8796 KB |
Output is correct |
9 |
Correct |
3 ms |
8796 KB |
Output is correct |
10 |
Correct |
3 ms |
9048 KB |
Output is correct |
11 |
Correct |
3 ms |
8796 KB |
Output is correct |
12 |
Correct |
4 ms |
8960 KB |
Output is correct |
13 |
Correct |
4 ms |
8796 KB |
Output is correct |
14 |
Correct |
4 ms |
8796 KB |
Output is correct |
15 |
Correct |
4 ms |
8924 KB |
Output is correct |
16 |
Correct |
3 ms |
8796 KB |
Output is correct |
17 |
Correct |
4 ms |
8796 KB |
Output is correct |
18 |
Correct |
3 ms |
8796 KB |
Output is correct |
19 |
Correct |
44 ms |
12884 KB |
Output is correct |
20 |
Correct |
31 ms |
12744 KB |
Output is correct |
21 |
Correct |
40 ms |
12876 KB |
Output is correct |
22 |
Correct |
29 ms |
12856 KB |
Output is correct |
23 |
Correct |
26 ms |
12884 KB |
Output is correct |
24 |
Correct |
24 ms |
12892 KB |
Output is correct |
25 |
Correct |
29 ms |
12380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
3 ms |
8796 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
5 |
Correct |
4 ms |
8796 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
3 ms |
8796 KB |
Output is correct |
8 |
Correct |
5 ms |
8796 KB |
Output is correct |
9 |
Correct |
3 ms |
8796 KB |
Output is correct |
10 |
Correct |
3 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8912 KB |
Output is correct |
12 |
Correct |
4 ms |
8796 KB |
Output is correct |
13 |
Correct |
4 ms |
8796 KB |
Output is correct |
14 |
Correct |
4 ms |
9052 KB |
Output is correct |
15 |
Correct |
3 ms |
8796 KB |
Output is correct |
16 |
Correct |
4 ms |
8796 KB |
Output is correct |
17 |
Correct |
4 ms |
8796 KB |
Output is correct |
18 |
Correct |
3 ms |
8796 KB |
Output is correct |
19 |
Correct |
110 ms |
19916 KB |
Output is correct |
20 |
Correct |
103 ms |
19176 KB |
Output is correct |
21 |
Correct |
140 ms |
19884 KB |
Output is correct |
22 |
Correct |
111 ms |
20004 KB |
Output is correct |
23 |
Correct |
115 ms |
20144 KB |
Output is correct |
24 |
Correct |
143 ms |
19912 KB |
Output is correct |
25 |
Correct |
47 ms |
19320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
136 ms |
18788 KB |
Output is correct |
2 |
Correct |
141 ms |
18788 KB |
Output is correct |
3 |
Correct |
128 ms |
18796 KB |
Output is correct |
4 |
Correct |
154 ms |
22452 KB |
Output is correct |
5 |
Correct |
131 ms |
22224 KB |
Output is correct |
6 |
Correct |
166 ms |
21968 KB |
Output is correct |
7 |
Correct |
160 ms |
22296 KB |
Output is correct |
8 |
Correct |
145 ms |
21980 KB |
Output is correct |
9 |
Correct |
134 ms |
20052 KB |
Output is correct |
10 |
Correct |
139 ms |
21452 KB |
Output is correct |
11 |
Correct |
153 ms |
21380 KB |
Output is correct |
12 |
Correct |
167 ms |
21548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
124 ms |
18784 KB |
Output is correct |
3 |
Correct |
130 ms |
18788 KB |
Output is correct |
4 |
Correct |
160 ms |
18640 KB |
Output is correct |
5 |
Correct |
128 ms |
21984 KB |
Output is correct |
6 |
Correct |
139 ms |
21972 KB |
Output is correct |
7 |
Correct |
157 ms |
21968 KB |
Output is correct |
8 |
Correct |
128 ms |
22220 KB |
Output is correct |
9 |
Correct |
140 ms |
22180 KB |
Output is correct |
10 |
Correct |
137 ms |
22220 KB |
Output is correct |
11 |
Correct |
124 ms |
22116 KB |
Output is correct |
12 |
Correct |
77 ms |
21660 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
3 ms |
8796 KB |
Output is correct |
15 |
Correct |
3 ms |
8796 KB |
Output is correct |
16 |
Correct |
3 ms |
8796 KB |
Output is correct |
17 |
Correct |
4 ms |
8796 KB |
Output is correct |
18 |
Correct |
3 ms |
8796 KB |
Output is correct |
19 |
Correct |
3 ms |
8796 KB |
Output is correct |
20 |
Correct |
5 ms |
8796 KB |
Output is correct |
21 |
Correct |
3 ms |
8796 KB |
Output is correct |
22 |
Correct |
3 ms |
9048 KB |
Output is correct |
23 |
Correct |
3 ms |
8796 KB |
Output is correct |
24 |
Correct |
4 ms |
8960 KB |
Output is correct |
25 |
Correct |
4 ms |
8796 KB |
Output is correct |
26 |
Correct |
4 ms |
8796 KB |
Output is correct |
27 |
Correct |
4 ms |
8924 KB |
Output is correct |
28 |
Correct |
3 ms |
8796 KB |
Output is correct |
29 |
Correct |
4 ms |
8796 KB |
Output is correct |
30 |
Correct |
3 ms |
8796 KB |
Output is correct |
31 |
Correct |
44 ms |
12884 KB |
Output is correct |
32 |
Correct |
31 ms |
12744 KB |
Output is correct |
33 |
Correct |
40 ms |
12876 KB |
Output is correct |
34 |
Correct |
29 ms |
12856 KB |
Output is correct |
35 |
Correct |
26 ms |
12884 KB |
Output is correct |
36 |
Correct |
24 ms |
12892 KB |
Output is correct |
37 |
Correct |
29 ms |
12380 KB |
Output is correct |
38 |
Correct |
3 ms |
8796 KB |
Output is correct |
39 |
Correct |
2 ms |
8912 KB |
Output is correct |
40 |
Correct |
4 ms |
8796 KB |
Output is correct |
41 |
Correct |
4 ms |
8796 KB |
Output is correct |
42 |
Correct |
4 ms |
9052 KB |
Output is correct |
43 |
Correct |
3 ms |
8796 KB |
Output is correct |
44 |
Correct |
4 ms |
8796 KB |
Output is correct |
45 |
Correct |
4 ms |
8796 KB |
Output is correct |
46 |
Correct |
3 ms |
8796 KB |
Output is correct |
47 |
Correct |
110 ms |
19916 KB |
Output is correct |
48 |
Correct |
103 ms |
19176 KB |
Output is correct |
49 |
Correct |
140 ms |
19884 KB |
Output is correct |
50 |
Correct |
111 ms |
20004 KB |
Output is correct |
51 |
Correct |
115 ms |
20144 KB |
Output is correct |
52 |
Correct |
143 ms |
19912 KB |
Output is correct |
53 |
Correct |
47 ms |
19320 KB |
Output is correct |
54 |
Correct |
136 ms |
18788 KB |
Output is correct |
55 |
Correct |
141 ms |
18788 KB |
Output is correct |
56 |
Correct |
128 ms |
18796 KB |
Output is correct |
57 |
Correct |
154 ms |
22452 KB |
Output is correct |
58 |
Correct |
131 ms |
22224 KB |
Output is correct |
59 |
Correct |
166 ms |
21968 KB |
Output is correct |
60 |
Correct |
160 ms |
22296 KB |
Output is correct |
61 |
Correct |
145 ms |
21980 KB |
Output is correct |
62 |
Correct |
134 ms |
20052 KB |
Output is correct |
63 |
Correct |
139 ms |
21452 KB |
Output is correct |
64 |
Correct |
153 ms |
21380 KB |
Output is correct |
65 |
Correct |
167 ms |
21548 KB |
Output is correct |
66 |
Correct |
3 ms |
8796 KB |
Output is correct |
67 |
Correct |
120 ms |
21720 KB |
Output is correct |
68 |
Correct |
137 ms |
22028 KB |
Output is correct |
69 |
Correct |
121 ms |
21728 KB |
Output is correct |
70 |
Correct |
126 ms |
21980 KB |
Output is correct |
71 |
Correct |
123 ms |
21968 KB |
Output is correct |
72 |
Correct |
139 ms |
22156 KB |
Output is correct |
73 |
Correct |
133 ms |
22384 KB |
Output is correct |
74 |
Correct |
126 ms |
22240 KB |
Output is correct |
75 |
Correct |
138 ms |
22168 KB |
Output is correct |
76 |
Correct |
130 ms |
22164 KB |
Output is correct |
77 |
Correct |
78 ms |
21460 KB |
Output is correct |
78 |
Correct |
3 ms |
8796 KB |
Output is correct |
79 |
Correct |
4 ms |
8796 KB |
Output is correct |
80 |
Correct |
3 ms |
8796 KB |
Output is correct |
81 |
Correct |
3 ms |
8796 KB |
Output is correct |
82 |
Correct |
3 ms |
8796 KB |
Output is correct |
83 |
Correct |
4 ms |
8796 KB |
Output is correct |
84 |
Correct |
4 ms |
8960 KB |
Output is correct |
85 |
Correct |
3 ms |
8796 KB |
Output is correct |
86 |
Correct |
33 ms |
12516 KB |
Output is correct |
87 |
Correct |
28 ms |
12636 KB |
Output is correct |
88 |
Correct |
35 ms |
12888 KB |
Output is correct |
89 |
Correct |
25 ms |
12908 KB |
Output is correct |
90 |
Correct |
24 ms |
12884 KB |
Output is correct |
91 |
Correct |
23 ms |
12832 KB |
Output is correct |
92 |
Correct |
27 ms |
12372 KB |
Output is correct |
93 |
Correct |
99 ms |
20152 KB |
Output is correct |
94 |
Correct |
93 ms |
19596 KB |
Output is correct |
95 |
Correct |
114 ms |
20080 KB |
Output is correct |
96 |
Correct |
141 ms |
20164 KB |
Output is correct |
97 |
Correct |
127 ms |
19964 KB |
Output is correct |
98 |
Correct |
121 ms |
19916 KB |
Output is correct |
99 |
Correct |
44 ms |
19408 KB |
Output is correct |
100 |
Correct |
164 ms |
22052 KB |
Output is correct |
101 |
Correct |
178 ms |
21868 KB |
Output is correct |
102 |
Correct |
162 ms |
21964 KB |
Output is correct |
103 |
Correct |
150 ms |
21468 KB |
Output is correct |
104 |
Correct |
148 ms |
21400 KB |
Output is correct |
105 |
Correct |
160 ms |
21464 KB |
Output is correct |
106 |
Correct |
125 ms |
21208 KB |
Output is correct |
107 |
Correct |
154 ms |
21196 KB |
Output is correct |
108 |
Correct |
145 ms |
21852 KB |
Output is correct |
109 |
Correct |
140 ms |
22220 KB |
Output is correct |
110 |
Correct |
158 ms |
21708 KB |
Output is correct |
111 |
Correct |
143 ms |
21708 KB |
Output is correct |