#include <bits/stdc++.h>
#define X first
#define Y second
using namespace std;
const int maxn = 2e5+10;
const int inf = 0x3f3f3f3f;
const int logo = 20;
const int off = 1 << logo;
const int treesiz = off << 1;
int n, q;
pair<int, int> s[maxn];
vector< int > v;
int dp[30][maxn];
int mini[maxn];
int tour[logo][treesiz];
void update(int ind, int x, int val) {
x += off;
tour[ind][x] = val;
x /= 2;
while (x) tour[ind][x] = min(tour[ind][x * 2], tour[ind][x * 2 + 1]), x /= 2;
}
int query(int ind, int a, int b, int l, int r, int node) {
if (l > b || r < a) return inf;
if (l >= a && r <= b) return tour[ind][node];
int mid = (l + r) / 2;
return min(query(ind, a, b, l, mid, node * 2), query(ind, a, b, mid + 1, r, node * 2 + 1));
}
int main() {
scanf("%d%d", &n, &q);
for (int i = 0; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
s[i] = {a, b};
v.push_back(a);
v.push_back(b);
}
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
for (int i = 0; i < v.size(); i++) mini[i] = i;
for (int i = 0; i < n; i++) {
s[i].X = lower_bound(v.begin(), v.end(), s[i].X) - v.begin();
s[i].Y = lower_bound(v.begin(), v.end(), s[i].Y) - v.begin();
mini[s[i].Y] = min(mini[s[i].Y], s[i].X);
}
for (int i = 0; i < v.size(); i++) {
dp[0][i] = mini[i];
//printf("mini %d --> %d\n", i, mini[i]);
}
for (int i = 1; i < logo; i++) {
for (int j = 0; j < v.size(); j++) update(i - 1, j, dp[i - 1][j]);
for (int j = 0; j < v.size(); j++) {
dp[i][j] = query(i - 1, dp[i - 1][j], j, 0, off - 1, 1);
}
}
for (int i = 0; i < v.size(); i++) update(logo - 1, i, dp[logo - 1][i]);
while (q--) {
int a, b;
scanf("%d%d", &a, &b); a--, b--;
if (s[a].Y > s[b].Y) {
printf("impossible\n");
continue;
}
if (a == b) {
printf("0\n");
continue;
}
int ca = a, cb = b;
int sol = inf;
/*int dis = 0;
b = s[b].Y;
a = s[a].Y;
for (int i = logo - 1; i >= 0; i--) {
if (dp[i][b] > a) b = dp[i][b], dis += (1 << i);
//printf("debug: %d %d\n", a, b);
}
if (mini[b] <= a)
sol = min(sol, dis + 2);
else {
printf("impossible\n");
continue;
}*/
int dis = 0;
b = s[cb].Y;
int l = s[cb].X;
a = s[ca].Y;
for (int i = logo - 1; i >= 0; i--) {
int tren = query(i, l, b, 0, off - 1, 1);
if (tren > a) l = tren, dis += (1 << i);
//printf("debug: %d %d %d\n", a, l, b);
}
if (l <= a) sol = min(sol, dis + 1);
if (query(0, l, b, 0, off - 1, 1) <= a) sol = min(dis + 2, sol);
if (sol >= inf) printf("impossible\n");
else printf("%d\n", sol);
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:48:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 0; i < v.size(); i++) mini[i] = i;
| ~~^~~~~~~~~~
events.cpp:55:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
events.cpp:60:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int j = 0; j < v.size(); j++) update(i - 1, j, dp[i - 1][j]);
| ~~^~~~~~~~~~
events.cpp:61:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int j = 0; j < v.size(); j++) {
| ~~^~~~~~~~~~
events.cpp:65:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (int i = 0; i < v.size(); i++) update(logo - 1, i, dp[logo - 1][i]);
| ~~^~~~~~~~~~
events.cpp:37:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
events.cpp:40:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
events.cpp:69:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%d%d", &a, &b); a--, b--;
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1162 ms |
27764 KB |
Output is correct |
3 |
Correct |
1190 ms |
27696 KB |
Output is correct |
4 |
Correct |
1179 ms |
27760 KB |
Output is correct |
5 |
Correct |
1180 ms |
30092 KB |
Output is correct |
6 |
Correct |
1208 ms |
32184 KB |
Output is correct |
7 |
Correct |
1199 ms |
33084 KB |
Output is correct |
8 |
Correct |
1213 ms |
51860 KB |
Output is correct |
9 |
Correct |
1114 ms |
51944 KB |
Output is correct |
10 |
Correct |
1046 ms |
28180 KB |
Output is correct |
11 |
Correct |
1145 ms |
37484 KB |
Output is correct |
12 |
Correct |
700 ms |
28260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1568 KB |
Output is correct |
3 |
Correct |
9 ms |
1704 KB |
Output is correct |
4 |
Correct |
6 ms |
1688 KB |
Output is correct |
5 |
Correct |
7 ms |
1748 KB |
Output is correct |
6 |
Correct |
5 ms |
1748 KB |
Output is correct |
7 |
Correct |
14 ms |
1940 KB |
Output is correct |
8 |
Correct |
13 ms |
2024 KB |
Output is correct |
9 |
Correct |
1 ms |
1620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1568 KB |
Output is correct |
3 |
Correct |
9 ms |
1704 KB |
Output is correct |
4 |
Correct |
6 ms |
1688 KB |
Output is correct |
5 |
Correct |
7 ms |
1748 KB |
Output is correct |
6 |
Correct |
5 ms |
1748 KB |
Output is correct |
7 |
Correct |
14 ms |
1940 KB |
Output is correct |
8 |
Correct |
13 ms |
2024 KB |
Output is correct |
9 |
Correct |
1 ms |
1620 KB |
Output is correct |
10 |
Correct |
1 ms |
1620 KB |
Output is correct |
11 |
Correct |
1 ms |
1620 KB |
Output is correct |
12 |
Correct |
6 ms |
1748 KB |
Output is correct |
13 |
Correct |
6 ms |
1748 KB |
Output is correct |
14 |
Correct |
9 ms |
1748 KB |
Output is correct |
15 |
Correct |
6 ms |
1748 KB |
Output is correct |
16 |
Correct |
11 ms |
2040 KB |
Output is correct |
17 |
Correct |
10 ms |
2004 KB |
Output is correct |
18 |
Correct |
2 ms |
1620 KB |
Output is correct |
19 |
Correct |
668 ms |
3680 KB |
Output is correct |
20 |
Correct |
546 ms |
3212 KB |
Output is correct |
21 |
Correct |
289 ms |
3564 KB |
Output is correct |
22 |
Correct |
265 ms |
3608 KB |
Output is correct |
23 |
Correct |
387 ms |
4572 KB |
Output is correct |
24 |
Correct |
425 ms |
4672 KB |
Output is correct |
25 |
Correct |
520 ms |
4416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1568 KB |
Output is correct |
3 |
Correct |
9 ms |
1704 KB |
Output is correct |
4 |
Correct |
6 ms |
1688 KB |
Output is correct |
5 |
Correct |
7 ms |
1748 KB |
Output is correct |
6 |
Correct |
5 ms |
1748 KB |
Output is correct |
7 |
Correct |
14 ms |
1940 KB |
Output is correct |
8 |
Correct |
13 ms |
2024 KB |
Output is correct |
9 |
Correct |
1 ms |
1620 KB |
Output is correct |
10 |
Correct |
1 ms |
1620 KB |
Output is correct |
11 |
Correct |
1 ms |
1620 KB |
Output is correct |
12 |
Correct |
9 ms |
1748 KB |
Output is correct |
13 |
Correct |
6 ms |
1748 KB |
Output is correct |
14 |
Correct |
6 ms |
1684 KB |
Output is correct |
15 |
Correct |
7 ms |
1748 KB |
Output is correct |
16 |
Correct |
14 ms |
2044 KB |
Output is correct |
17 |
Correct |
11 ms |
2048 KB |
Output is correct |
18 |
Correct |
2 ms |
1620 KB |
Output is correct |
19 |
Correct |
524 ms |
27036 KB |
Output is correct |
20 |
Correct |
562 ms |
27108 KB |
Output is correct |
21 |
Correct |
509 ms |
27052 KB |
Output is correct |
22 |
Correct |
707 ms |
35756 KB |
Output is correct |
23 |
Correct |
862 ms |
50752 KB |
Output is correct |
24 |
Correct |
924 ms |
50748 KB |
Output is correct |
25 |
Correct |
22 ms |
3264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1191 ms |
27664 KB |
Output is correct |
2 |
Correct |
1251 ms |
27708 KB |
Output is correct |
3 |
Correct |
1248 ms |
27804 KB |
Output is correct |
4 |
Correct |
1114 ms |
51888 KB |
Output is correct |
5 |
Correct |
994 ms |
27980 KB |
Output is correct |
6 |
Correct |
1459 ms |
51528 KB |
Output is correct |
7 |
Correct |
1427 ms |
51608 KB |
Output is correct |
8 |
Execution timed out |
1532 ms |
51636 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1162 ms |
27764 KB |
Output is correct |
3 |
Correct |
1190 ms |
27696 KB |
Output is correct |
4 |
Correct |
1179 ms |
27760 KB |
Output is correct |
5 |
Correct |
1180 ms |
30092 KB |
Output is correct |
6 |
Correct |
1208 ms |
32184 KB |
Output is correct |
7 |
Correct |
1199 ms |
33084 KB |
Output is correct |
8 |
Correct |
1213 ms |
51860 KB |
Output is correct |
9 |
Correct |
1114 ms |
51944 KB |
Output is correct |
10 |
Correct |
1046 ms |
28180 KB |
Output is correct |
11 |
Correct |
1145 ms |
37484 KB |
Output is correct |
12 |
Correct |
700 ms |
28260 KB |
Output is correct |
13 |
Correct |
2 ms |
1620 KB |
Output is correct |
14 |
Correct |
1 ms |
1568 KB |
Output is correct |
15 |
Correct |
9 ms |
1704 KB |
Output is correct |
16 |
Correct |
6 ms |
1688 KB |
Output is correct |
17 |
Correct |
7 ms |
1748 KB |
Output is correct |
18 |
Correct |
5 ms |
1748 KB |
Output is correct |
19 |
Correct |
14 ms |
1940 KB |
Output is correct |
20 |
Correct |
13 ms |
2024 KB |
Output is correct |
21 |
Correct |
1 ms |
1620 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
1 ms |
1620 KB |
Output is correct |
24 |
Correct |
6 ms |
1748 KB |
Output is correct |
25 |
Correct |
6 ms |
1748 KB |
Output is correct |
26 |
Correct |
9 ms |
1748 KB |
Output is correct |
27 |
Correct |
6 ms |
1748 KB |
Output is correct |
28 |
Correct |
11 ms |
2040 KB |
Output is correct |
29 |
Correct |
10 ms |
2004 KB |
Output is correct |
30 |
Correct |
2 ms |
1620 KB |
Output is correct |
31 |
Correct |
668 ms |
3680 KB |
Output is correct |
32 |
Correct |
546 ms |
3212 KB |
Output is correct |
33 |
Correct |
289 ms |
3564 KB |
Output is correct |
34 |
Correct |
265 ms |
3608 KB |
Output is correct |
35 |
Correct |
387 ms |
4572 KB |
Output is correct |
36 |
Correct |
425 ms |
4672 KB |
Output is correct |
37 |
Correct |
520 ms |
4416 KB |
Output is correct |
38 |
Correct |
1 ms |
1620 KB |
Output is correct |
39 |
Correct |
1 ms |
1620 KB |
Output is correct |
40 |
Correct |
9 ms |
1748 KB |
Output is correct |
41 |
Correct |
6 ms |
1748 KB |
Output is correct |
42 |
Correct |
6 ms |
1684 KB |
Output is correct |
43 |
Correct |
7 ms |
1748 KB |
Output is correct |
44 |
Correct |
14 ms |
2044 KB |
Output is correct |
45 |
Correct |
11 ms |
2048 KB |
Output is correct |
46 |
Correct |
2 ms |
1620 KB |
Output is correct |
47 |
Correct |
524 ms |
27036 KB |
Output is correct |
48 |
Correct |
562 ms |
27108 KB |
Output is correct |
49 |
Correct |
509 ms |
27052 KB |
Output is correct |
50 |
Correct |
707 ms |
35756 KB |
Output is correct |
51 |
Correct |
862 ms |
50752 KB |
Output is correct |
52 |
Correct |
924 ms |
50748 KB |
Output is correct |
53 |
Correct |
22 ms |
3264 KB |
Output is correct |
54 |
Correct |
1191 ms |
27664 KB |
Output is correct |
55 |
Correct |
1251 ms |
27708 KB |
Output is correct |
56 |
Correct |
1248 ms |
27804 KB |
Output is correct |
57 |
Correct |
1114 ms |
51888 KB |
Output is correct |
58 |
Correct |
994 ms |
27980 KB |
Output is correct |
59 |
Correct |
1459 ms |
51528 KB |
Output is correct |
60 |
Correct |
1427 ms |
51608 KB |
Output is correct |
61 |
Execution timed out |
1532 ms |
51636 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |