#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> l(n), r(n);
vector<int> xs;
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
xs.push_back(l[i]);
xs.push_back(r[i]);
}
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
for (int i = 0; i < n; i++) {
l[i] = lower_bound(xs.begin(), xs.end(), l[i]) - xs.begin();
r[i] = lower_bound(xs.begin(), xs.end(), r[i]) - xs.begin();
}
/* if (n <= 1000 && q <= 100) {
while (q--) {
int x, y;
cin >> x >> y;
--x, --y;
if (x == y) {
cout << 0 << '\n';
continue;
}
int ans = 1;
int idx = x;
while (idx != y) {
if (l[y] <= r[idx] && r[idx] <= r[y]) {
idx = y;
break;
}
int nxt = idx;
for (int i = 0; i < n; i++) {
if (l[i] <= r[idx] && r[idx] <= r[i] && r[i] > r[nxt] && r[i] <= r[y]) {
nxt = i;
}
}
ans += 1;
if (nxt == idx) {
break;
}
idx = nxt;
}
if (idx != y) {
cout << "impossible" << '\n';
continue;
}
cout << ans << '\n';
}
return 0;
} */
vector<int> order(n);
iota(order.begin(), order.end(), 0);
sort(order.begin(), order.end(), [&](int i, int j) {
if (r[i] != r[j]) {
return r[i] < r[j];
} else {
return l[i] < l[j];
}
});
vector<int> id(n);
for (int i = 0; i < n; i++) {
id[order[i]] = i;
}
vector<int> xl(n), xr(n);
for (int i = 0; i < n; i++) {
xl[i] = l[order[i]];
xr[i] = r[order[i]];
}
swap(l, xl);
swap(r, xr);
const int LOG = 17;
vector<vector<pair<int, int>>> who(n, vector<pair<int, int>>(LOG));
for (int i = 0; i < n; i++) {
who[i][0] = make_pair(l[i], i);
}
for (int j = 1; j < LOG; j++) {
for (int i = 0; i + (1 << j) <= n; i++) {
who[i][j] = min(who[i][j - 1], who[i + (1 << (j - 1))][j - 1]);
}
}
vector<int> logs(n + 1);
for (int i = 2; i <= n; i++) {
logs[i] = logs[i / 2] + 1;
}
auto query = [&](int l, int r) {
int k = logs[r - l + 1];
return min(who[l][k], who[r - (1 << k) + 1][k]);
};
vector<int> prv(n);
/* for (int i = 0; i < n; i++) {
cout << l[i] << " " << r[i] << '\n';
}
cout << '\n'; cout << '\n'; */
for (int i = 0; i < n; i++) {
int low = 0, high = i;
prv[i] = i;
while (low <= high) {
int mid = low + high >> 1;
if (r[mid] >= l[i]) {
prv[i] = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
/* cout << "[" << prv[i] + 1 << " " << i + 1 << "]" << '\n'; */
}
vector<vector<int>> jump(n, vector<int>(LOG));
vector<vector<int>> st(n, vector<int>(LOG));
function<void(int)> buildSparse = [&](int j) {
for (int i = 0; i < n; i++) {
st[i][0] = jump[i][j];
}
for (int p = 1; p < LOG; p++) {
for (int i = 0; i + (1 << p) <= n; i++) {
st[i][p] = min(st[i][p - 1], st[i + (1 << (p - 1))][p - 1]);
}
}
};
auto get = [&](int l, int r) {
int k = logs[r - l + 1];
return min(st[l][k], st[r - (1 << k) + 1][k]);
};
for (int i = 0; i < n; i++) {
jump[i][0] = prv[i];
}
for (int j = 1; j < LOG; j++) {
for (int i = 0; i < n; i++) {
jump[i][j] = jump[query(jump[i][j - 1], i).second][j - 1]; // min jump[x][j - 1] for x in range [jump[i][j - 1], i]
}
}
while (q--) {
int x, y;
cin >> x >> y;
--x, --y;
x = id[x];
y = id[y];
if (x == y) {
cout << 0 << '\n';
continue;
}
if (l[y] <= r[x] && r[x] <= r[y]) {
cout << 1 << '\n';
continue;
}
if (x > y) {
cout << "impossible" << '\n';
continue;
}
int ans = 0;
for (int i = LOG - 1; i >= 0; i--) {
if (jump[y][i] > x) {
ans += (1 << i);
y = query(jump[y][i], y).second;
}
}
if (jump[y][0] > x) {
cout << "impossible" << '\n';
continue;
} else {
ans += 1;
}
cout << ans << '\n';
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:106:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
106 | int mid = low + high >> 1;
| ~~~~^~~~~~
events.cpp:128:8: warning: variable 'get' set but not used [-Wunused-but-set-variable]
128 | auto get = [&](int l, int r) {
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
180 ms |
41568 KB |
Output is correct |
3 |
Correct |
209 ms |
41580 KB |
Output is correct |
4 |
Correct |
266 ms |
41608 KB |
Output is correct |
5 |
Correct |
212 ms |
41664 KB |
Output is correct |
6 |
Correct |
226 ms |
41656 KB |
Output is correct |
7 |
Correct |
232 ms |
41708 KB |
Output is correct |
8 |
Correct |
183 ms |
42000 KB |
Output is correct |
9 |
Correct |
200 ms |
42080 KB |
Output is correct |
10 |
Correct |
286 ms |
41876 KB |
Output is correct |
11 |
Correct |
272 ms |
41964 KB |
Output is correct |
12 |
Correct |
151 ms |
42112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
40 ms |
2824 KB |
Output is correct |
20 |
Correct |
35 ms |
2744 KB |
Output is correct |
21 |
Correct |
54 ms |
3016 KB |
Output is correct |
22 |
Correct |
34 ms |
3072 KB |
Output is correct |
23 |
Correct |
29 ms |
2936 KB |
Output is correct |
24 |
Correct |
27 ms |
2944 KB |
Output is correct |
25 |
Correct |
40 ms |
2612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
2 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
149 ms |
41008 KB |
Output is correct |
20 |
Correct |
174 ms |
40928 KB |
Output is correct |
21 |
Correct |
181 ms |
41012 KB |
Output is correct |
22 |
Correct |
192 ms |
40932 KB |
Output is correct |
23 |
Correct |
157 ms |
41016 KB |
Output is correct |
24 |
Correct |
162 ms |
40928 KB |
Output is correct |
25 |
Correct |
101 ms |
41012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
41548 KB |
Output is correct |
2 |
Correct |
209 ms |
41672 KB |
Output is correct |
3 |
Correct |
225 ms |
41568 KB |
Output is correct |
4 |
Correct |
193 ms |
42080 KB |
Output is correct |
5 |
Correct |
273 ms |
41880 KB |
Output is correct |
6 |
Correct |
284 ms |
41780 KB |
Output is correct |
7 |
Correct |
292 ms |
41656 KB |
Output is correct |
8 |
Correct |
228 ms |
41804 KB |
Output is correct |
9 |
Correct |
166 ms |
40924 KB |
Output is correct |
10 |
Correct |
207 ms |
41340 KB |
Output is correct |
11 |
Correct |
191 ms |
41156 KB |
Output is correct |
12 |
Correct |
234 ms |
41320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
180 ms |
41568 KB |
Output is correct |
3 |
Correct |
209 ms |
41580 KB |
Output is correct |
4 |
Correct |
266 ms |
41608 KB |
Output is correct |
5 |
Correct |
212 ms |
41664 KB |
Output is correct |
6 |
Correct |
226 ms |
41656 KB |
Output is correct |
7 |
Correct |
232 ms |
41708 KB |
Output is correct |
8 |
Correct |
183 ms |
42000 KB |
Output is correct |
9 |
Correct |
200 ms |
42080 KB |
Output is correct |
10 |
Correct |
286 ms |
41876 KB |
Output is correct |
11 |
Correct |
272 ms |
41964 KB |
Output is correct |
12 |
Correct |
151 ms |
42112 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
2 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
40 ms |
2824 KB |
Output is correct |
32 |
Correct |
35 ms |
2744 KB |
Output is correct |
33 |
Correct |
54 ms |
3016 KB |
Output is correct |
34 |
Correct |
34 ms |
3072 KB |
Output is correct |
35 |
Correct |
29 ms |
2936 KB |
Output is correct |
36 |
Correct |
27 ms |
2944 KB |
Output is correct |
37 |
Correct |
40 ms |
2612 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
724 KB |
Output is correct |
41 |
Correct |
2 ms |
724 KB |
Output is correct |
42 |
Correct |
1 ms |
724 KB |
Output is correct |
43 |
Correct |
2 ms |
724 KB |
Output is correct |
44 |
Correct |
1 ms |
724 KB |
Output is correct |
45 |
Correct |
2 ms |
724 KB |
Output is correct |
46 |
Correct |
1 ms |
724 KB |
Output is correct |
47 |
Correct |
149 ms |
41008 KB |
Output is correct |
48 |
Correct |
174 ms |
40928 KB |
Output is correct |
49 |
Correct |
181 ms |
41012 KB |
Output is correct |
50 |
Correct |
192 ms |
40932 KB |
Output is correct |
51 |
Correct |
157 ms |
41016 KB |
Output is correct |
52 |
Correct |
162 ms |
40928 KB |
Output is correct |
53 |
Correct |
101 ms |
41012 KB |
Output is correct |
54 |
Correct |
179 ms |
41548 KB |
Output is correct |
55 |
Correct |
209 ms |
41672 KB |
Output is correct |
56 |
Correct |
225 ms |
41568 KB |
Output is correct |
57 |
Correct |
193 ms |
42080 KB |
Output is correct |
58 |
Correct |
273 ms |
41880 KB |
Output is correct |
59 |
Correct |
284 ms |
41780 KB |
Output is correct |
60 |
Correct |
292 ms |
41656 KB |
Output is correct |
61 |
Correct |
228 ms |
41804 KB |
Output is correct |
62 |
Correct |
166 ms |
40924 KB |
Output is correct |
63 |
Correct |
207 ms |
41340 KB |
Output is correct |
64 |
Correct |
191 ms |
41156 KB |
Output is correct |
65 |
Correct |
234 ms |
41320 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
178 ms |
41604 KB |
Output is correct |
68 |
Correct |
187 ms |
41544 KB |
Output is correct |
69 |
Correct |
216 ms |
41544 KB |
Output is correct |
70 |
Correct |
221 ms |
41604 KB |
Output is correct |
71 |
Correct |
237 ms |
41744 KB |
Output is correct |
72 |
Correct |
242 ms |
41660 KB |
Output is correct |
73 |
Correct |
192 ms |
42092 KB |
Output is correct |
74 |
Correct |
218 ms |
42084 KB |
Output is correct |
75 |
Correct |
258 ms |
41960 KB |
Output is correct |
76 |
Correct |
260 ms |
41960 KB |
Output is correct |
77 |
Correct |
176 ms |
42092 KB |
Output is correct |
78 |
Correct |
1 ms |
260 KB |
Output is correct |
79 |
Correct |
2 ms |
768 KB |
Output is correct |
80 |
Correct |
2 ms |
668 KB |
Output is correct |
81 |
Correct |
2 ms |
724 KB |
Output is correct |
82 |
Correct |
2 ms |
724 KB |
Output is correct |
83 |
Correct |
2 ms |
724 KB |
Output is correct |
84 |
Correct |
1 ms |
724 KB |
Output is correct |
85 |
Correct |
1 ms |
724 KB |
Output is correct |
86 |
Correct |
36 ms |
2760 KB |
Output is correct |
87 |
Correct |
39 ms |
2772 KB |
Output is correct |
88 |
Correct |
49 ms |
3052 KB |
Output is correct |
89 |
Correct |
32 ms |
3148 KB |
Output is correct |
90 |
Correct |
28 ms |
2900 KB |
Output is correct |
91 |
Correct |
33 ms |
3004 KB |
Output is correct |
92 |
Correct |
31 ms |
2600 KB |
Output is correct |
93 |
Correct |
174 ms |
40968 KB |
Output is correct |
94 |
Correct |
145 ms |
41024 KB |
Output is correct |
95 |
Correct |
169 ms |
41076 KB |
Output is correct |
96 |
Correct |
172 ms |
41100 KB |
Output is correct |
97 |
Correct |
168 ms |
40904 KB |
Output is correct |
98 |
Correct |
150 ms |
40936 KB |
Output is correct |
99 |
Correct |
111 ms |
41024 KB |
Output is correct |
100 |
Correct |
327 ms |
41668 KB |
Output is correct |
101 |
Correct |
292 ms |
41696 KB |
Output is correct |
102 |
Correct |
258 ms |
41884 KB |
Output is correct |
103 |
Correct |
198 ms |
41404 KB |
Output is correct |
104 |
Correct |
201 ms |
41160 KB |
Output is correct |
105 |
Correct |
240 ms |
41308 KB |
Output is correct |
106 |
Correct |
245 ms |
41664 KB |
Output is correct |
107 |
Correct |
332 ms |
41800 KB |
Output is correct |
108 |
Correct |
233 ms |
41832 KB |
Output is correct |
109 |
Correct |
227 ms |
41812 KB |
Output is correct |
110 |
Correct |
202 ms |
41644 KB |
Output is correct |
111 |
Correct |
240 ms |
41684 KB |
Output is correct |