#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define pii pair<int, int>
#define piii pair<pii, int>
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define f first
#define s second
#define oo 1000000001
#define eb emplace_back
#define pb push_back
#define mpr make_pair
#define size(v) (int)v.size()
#define ln '\n'
#define ull unsigned long long
#define ll long long
#define all(v) v.begin(), v.end()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int up = 2e5 + 5;
int LOG[up];
void preClc() {
LOG[1] = 0;
for (int i = 2; i < up; ++i) {
LOG[i] = LOG[i >> 1] + 1;
}
}
struct Stable {
int n, lg;
vector<int>v;
vector<vector<int>>stMin;
Stable(int N, int LG, vector<int> &arr) {
n = N;
lg = LG;
v = arr;
stMin.resize(lg, vector<int>(n));
for (int i = 0; i < n; ++i) {
stMin[0][i] = i;
}
}
void buildMin() {
for (int i = 1; i < lg; ++i) {
for (int j = 0; j + (1 << i) <= n; ++j) {
if (v[stMin[i - 1][j]] <= v[stMin[i - 1][j + (1 << (i - 1))]]) {
stMin[i][j] = stMin[i - 1][j];
} else {
stMin[i][j] = stMin[i - 1][j + (1 << (i - 1))];
}
}
}
}
int getMin(int l, int r) {
if (l > r) return -1;
int sz = r - l + 1;
int j = LOG[sz];
if (v[stMin[j][l]] <= v[stMin[j][r - (1 << j) + 1]]) {
return stMin[j][l];
} else {
return stMin[j][r - (1 << j) + 1];
}
}
}; // Call preClc() and assign up
void solve() {
int n, q;
cin >> n >> q;
vpii e(n + 1);
vi compress;
for (int i = 1; i <= n; ++i) {
cin >> e[i].f >> e[i].s;
compress.eb(e[i].f);
compress.eb(e[i].s);
}
sort(all(compress));
compress.erase(unique(all(compress)), compress.end());
for (int i = 1; i <= n; ++i) {
e[i].f = lower_bound(all(compress), e[i].f) - compress.begin();
e[i].s = lower_bound(all(compress), e[i].s) - compress.begin();
}
int sz = size(compress);
vi minStart(sz, sz);
vi who(sz, 0);
for (int i = 1; i <= n; ++i) {
if (minStart[e[i].s] > e[i].f) {
minStart[e[i].s] = e[i].f;
who[e[i].s] = i;
}
}
Stable rmq(sz, 19, minStart);
rmq.buildMin();
const int lg = 18;
vvi prev(lg, vi(n + 1));
for (int i = 1; i <= n; ++i) {
prev[0][i] = who[rmq.getMin(e[i].f, e[i].s)];
if (prev[0][i] == i) prev[0][i] = 0;
}
for (int i = 1; i < lg; ++i) {
for (int j = 1; j <= n; ++j) {
prev[i][j] = prev[i - 1][prev[i - 1][j]];
}
}
for (int i = 0; i < q; ++i) {
int st, ed;
cin >> st >> ed;
if (e[st].s > e[ed].s) {
cout << "impossible\n";
} else if (st == ed) {
cout << 0 << ln;
} else if (e[st].s >= e[ed].f && e[st].s <= e[ed].s) {
cout << 1 << ln;
} else {
int res = 0;
for (int i = lg - 1; i >= 0; --i) {
if (e[prev[i][ed]].f > e[st].s) {
res += (1 << i);
ed = prev[i][ed];
}
}
ed = prev[0][ed];
++res;
if (ed == 0) {
cout << "impossible\n";
} else {
cout << res + 1 << ln;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
preClc();
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
202 ms |
19320 KB |
Output is correct |
3 |
Correct |
224 ms |
19328 KB |
Output is correct |
4 |
Correct |
271 ms |
20008 KB |
Output is correct |
5 |
Correct |
230 ms |
21024 KB |
Output is correct |
6 |
Correct |
239 ms |
21824 KB |
Output is correct |
7 |
Correct |
223 ms |
22048 KB |
Output is correct |
8 |
Correct |
219 ms |
29180 KB |
Output is correct |
9 |
Correct |
225 ms |
29184 KB |
Output is correct |
10 |
Correct |
261 ms |
20376 KB |
Output is correct |
11 |
Correct |
242 ms |
23828 KB |
Output is correct |
12 |
Correct |
178 ms |
20664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1084 KB |
Output is correct |
3 |
Correct |
2 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
2 ms |
1236 KB |
Output is correct |
7 |
Correct |
2 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1084 KB |
Output is correct |
3 |
Correct |
2 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
2 ms |
1236 KB |
Output is correct |
7 |
Correct |
2 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1236 KB |
Output is correct |
13 |
Correct |
2 ms |
1236 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
16 |
Correct |
2 ms |
1364 KB |
Output is correct |
17 |
Correct |
3 ms |
1364 KB |
Output is correct |
18 |
Correct |
1 ms |
1108 KB |
Output is correct |
19 |
Correct |
146 ms |
2632 KB |
Output is correct |
20 |
Correct |
147 ms |
2620 KB |
Output is correct |
21 |
Correct |
152 ms |
2816 KB |
Output is correct |
22 |
Correct |
172 ms |
2912 KB |
Output is correct |
23 |
Correct |
138 ms |
3116 KB |
Output is correct |
24 |
Correct |
135 ms |
3148 KB |
Output is correct |
25 |
Correct |
144 ms |
2764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1084 KB |
Output is correct |
3 |
Correct |
2 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
2 ms |
1236 KB |
Output is correct |
7 |
Correct |
2 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1236 KB |
Output is correct |
13 |
Correct |
2 ms |
1236 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
16 |
Correct |
2 ms |
1364 KB |
Output is correct |
17 |
Correct |
2 ms |
1364 KB |
Output is correct |
18 |
Correct |
1 ms |
1108 KB |
Output is correct |
19 |
Correct |
68 ms |
18692 KB |
Output is correct |
20 |
Correct |
65 ms |
19096 KB |
Output is correct |
21 |
Correct |
78 ms |
18736 KB |
Output is correct |
22 |
Correct |
74 ms |
22576 KB |
Output is correct |
23 |
Correct |
77 ms |
28048 KB |
Output is correct |
24 |
Correct |
80 ms |
27996 KB |
Output is correct |
25 |
Correct |
29 ms |
10728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
206 ms |
19324 KB |
Output is correct |
2 |
Correct |
231 ms |
19272 KB |
Output is correct |
3 |
Correct |
264 ms |
20120 KB |
Output is correct |
4 |
Correct |
237 ms |
29316 KB |
Output is correct |
5 |
Correct |
266 ms |
20500 KB |
Output is correct |
6 |
Correct |
275 ms |
28896 KB |
Output is correct |
7 |
Correct |
262 ms |
28848 KB |
Output is correct |
8 |
Correct |
234 ms |
28980 KB |
Output is correct |
9 |
Correct |
80 ms |
28084 KB |
Output is correct |
10 |
Correct |
237 ms |
28664 KB |
Output is correct |
11 |
Correct |
221 ms |
28252 KB |
Output is correct |
12 |
Correct |
233 ms |
28480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
202 ms |
19320 KB |
Output is correct |
3 |
Correct |
224 ms |
19328 KB |
Output is correct |
4 |
Correct |
271 ms |
20008 KB |
Output is correct |
5 |
Correct |
230 ms |
21024 KB |
Output is correct |
6 |
Correct |
239 ms |
21824 KB |
Output is correct |
7 |
Correct |
223 ms |
22048 KB |
Output is correct |
8 |
Correct |
219 ms |
29180 KB |
Output is correct |
9 |
Correct |
225 ms |
29184 KB |
Output is correct |
10 |
Correct |
261 ms |
20376 KB |
Output is correct |
11 |
Correct |
242 ms |
23828 KB |
Output is correct |
12 |
Correct |
178 ms |
20664 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
1 ms |
1084 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1236 KB |
Output is correct |
17 |
Correct |
1 ms |
1236 KB |
Output is correct |
18 |
Correct |
2 ms |
1236 KB |
Output is correct |
19 |
Correct |
2 ms |
1364 KB |
Output is correct |
20 |
Correct |
1 ms |
1364 KB |
Output is correct |
21 |
Correct |
1 ms |
1108 KB |
Output is correct |
22 |
Correct |
1 ms |
1108 KB |
Output is correct |
23 |
Correct |
1 ms |
1108 KB |
Output is correct |
24 |
Correct |
2 ms |
1236 KB |
Output is correct |
25 |
Correct |
2 ms |
1236 KB |
Output is correct |
26 |
Correct |
2 ms |
1236 KB |
Output is correct |
27 |
Correct |
2 ms |
1236 KB |
Output is correct |
28 |
Correct |
2 ms |
1364 KB |
Output is correct |
29 |
Correct |
3 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
1108 KB |
Output is correct |
31 |
Correct |
146 ms |
2632 KB |
Output is correct |
32 |
Correct |
147 ms |
2620 KB |
Output is correct |
33 |
Correct |
152 ms |
2816 KB |
Output is correct |
34 |
Correct |
172 ms |
2912 KB |
Output is correct |
35 |
Correct |
138 ms |
3116 KB |
Output is correct |
36 |
Correct |
135 ms |
3148 KB |
Output is correct |
37 |
Correct |
144 ms |
2764 KB |
Output is correct |
38 |
Correct |
1 ms |
1108 KB |
Output is correct |
39 |
Correct |
1 ms |
1108 KB |
Output is correct |
40 |
Correct |
2 ms |
1236 KB |
Output is correct |
41 |
Correct |
2 ms |
1236 KB |
Output is correct |
42 |
Correct |
2 ms |
1236 KB |
Output is correct |
43 |
Correct |
2 ms |
1236 KB |
Output is correct |
44 |
Correct |
2 ms |
1364 KB |
Output is correct |
45 |
Correct |
2 ms |
1364 KB |
Output is correct |
46 |
Correct |
1 ms |
1108 KB |
Output is correct |
47 |
Correct |
68 ms |
18692 KB |
Output is correct |
48 |
Correct |
65 ms |
19096 KB |
Output is correct |
49 |
Correct |
78 ms |
18736 KB |
Output is correct |
50 |
Correct |
74 ms |
22576 KB |
Output is correct |
51 |
Correct |
77 ms |
28048 KB |
Output is correct |
52 |
Correct |
80 ms |
27996 KB |
Output is correct |
53 |
Correct |
29 ms |
10728 KB |
Output is correct |
54 |
Correct |
206 ms |
19324 KB |
Output is correct |
55 |
Correct |
231 ms |
19272 KB |
Output is correct |
56 |
Correct |
264 ms |
20120 KB |
Output is correct |
57 |
Correct |
237 ms |
29316 KB |
Output is correct |
58 |
Correct |
266 ms |
20500 KB |
Output is correct |
59 |
Correct |
275 ms |
28896 KB |
Output is correct |
60 |
Correct |
262 ms |
28848 KB |
Output is correct |
61 |
Correct |
234 ms |
28980 KB |
Output is correct |
62 |
Correct |
80 ms |
28084 KB |
Output is correct |
63 |
Correct |
237 ms |
28664 KB |
Output is correct |
64 |
Correct |
221 ms |
28252 KB |
Output is correct |
65 |
Correct |
233 ms |
28480 KB |
Output is correct |
66 |
Correct |
1 ms |
1108 KB |
Output is correct |
67 |
Correct |
212 ms |
19996 KB |
Output is correct |
68 |
Correct |
225 ms |
19980 KB |
Output is correct |
69 |
Correct |
272 ms |
19924 KB |
Output is correct |
70 |
Correct |
227 ms |
20920 KB |
Output is correct |
71 |
Correct |
233 ms |
21856 KB |
Output is correct |
72 |
Correct |
222 ms |
22080 KB |
Output is correct |
73 |
Correct |
212 ms |
29128 KB |
Output is correct |
74 |
Correct |
215 ms |
29136 KB |
Output is correct |
75 |
Correct |
248 ms |
20436 KB |
Output is correct |
76 |
Correct |
232 ms |
23936 KB |
Output is correct |
77 |
Correct |
178 ms |
20508 KB |
Output is correct |
78 |
Correct |
1 ms |
1108 KB |
Output is correct |
79 |
Correct |
2 ms |
1256 KB |
Output is correct |
80 |
Correct |
2 ms |
1236 KB |
Output is correct |
81 |
Correct |
2 ms |
1236 KB |
Output is correct |
82 |
Correct |
2 ms |
1248 KB |
Output is correct |
83 |
Correct |
2 ms |
1384 KB |
Output is correct |
84 |
Correct |
2 ms |
1380 KB |
Output is correct |
85 |
Correct |
1 ms |
1124 KB |
Output is correct |
86 |
Correct |
150 ms |
3276 KB |
Output is correct |
87 |
Correct |
143 ms |
3080 KB |
Output is correct |
88 |
Correct |
152 ms |
3400 KB |
Output is correct |
89 |
Correct |
143 ms |
3452 KB |
Output is correct |
90 |
Correct |
138 ms |
3692 KB |
Output is correct |
91 |
Correct |
135 ms |
3700 KB |
Output is correct |
92 |
Correct |
143 ms |
3404 KB |
Output is correct |
93 |
Correct |
67 ms |
19392 KB |
Output is correct |
94 |
Correct |
69 ms |
19772 KB |
Output is correct |
95 |
Correct |
72 ms |
19400 KB |
Output is correct |
96 |
Correct |
73 ms |
22600 KB |
Output is correct |
97 |
Correct |
75 ms |
28096 KB |
Output is correct |
98 |
Correct |
78 ms |
28012 KB |
Output is correct |
99 |
Correct |
29 ms |
10824 KB |
Output is correct |
100 |
Correct |
243 ms |
28868 KB |
Output is correct |
101 |
Correct |
238 ms |
28968 KB |
Output is correct |
102 |
Correct |
242 ms |
28972 KB |
Output is correct |
103 |
Correct |
222 ms |
28464 KB |
Output is correct |
104 |
Correct |
221 ms |
28224 KB |
Output is correct |
105 |
Correct |
223 ms |
28504 KB |
Output is correct |
106 |
Correct |
227 ms |
18268 KB |
Output is correct |
107 |
Correct |
253 ms |
20544 KB |
Output is correct |
108 |
Correct |
221 ms |
20276 KB |
Output is correct |
109 |
Correct |
211 ms |
20296 KB |
Output is correct |
110 |
Correct |
219 ms |
28772 KB |
Output is correct |
111 |
Correct |
213 ms |
28740 KB |
Output is correct |