#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 rmq[logo][logo][maxn];
int pot[maxn];
int query(int ind, int a, int b) {
int len = b - a + 1;
int ac = pot[len];
return min(rmq[ind][ac][a], rmq[ind][ac][b - (1 << ac) + 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);
}
pot[1] = 0;
for (int i = 2; i < maxn; i++) {
pot[i] = pot[i - 1];
if ((i & (i - 1)) == 0) pot[i]++;
}
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]);
}
memset(rmq, inf, sizeof rmq);
for (int i = 1; i < logo; i++) {
for (int j = 0; j < v.size(); j++) rmq[i - 1][0][j] = dp[i - 1][j];
for (int j = 1; j < logo; j++)
for (int k = 0; k < v.size(); k++)
rmq[i - 1][j][k] = min(rmq[i - 1][j - 1][k], rmq[i - 1][j - 1][k + (1 << (j - 1))]);
for (int j = 0; j < v.size(); j++) {
dp[i][j] = query(i - 1, dp[i - 1][j], j);
}
}
for (int j = 0; j < v.size(); j++) rmq[logo - 1][0][j] = dp[logo - 1][j];
for (int j = 1; j < logo; j++)
for (int k = 0; k < v.size(); k++)
rmq[logo - 1][j][k] = min(rmq[logo - 1][j - 1][k], rmq[logo - 1][j - 1][k + (1 << (j - 1))]);
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);
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) <= 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:46:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for (int i = 0; i < v.size(); i++) mini[i] = i;
| ~~^~~~~~~~~~
events.cpp:53:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | 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++) rmq[i - 1][0][j] = dp[i - 1][j];
| ~~^~~~~~~~~~
events.cpp:62:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int k = 0; k < v.size(); k++)
| ~~^~~~~~~~~~
events.cpp:65:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (int j = 0; j < v.size(); j++) {
| ~~^~~~~~~~~~
events.cpp:69:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int j = 0; j < v.size(); j++) rmq[logo - 1][0][j] = dp[logo - 1][j];
| ~~^~~~~~~~~~
events.cpp:71:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for (int k = 0; k < v.size(); k++)
| ~~^~~~~~~~~~
events.cpp:29:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
events.cpp:32:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
events.cpp:76:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | scanf("%d%d", &a, &b); a--, b--;
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
132 ms |
314304 KB |
Output is correct |
2 |
Correct |
265 ms |
324812 KB |
Output is correct |
3 |
Correct |
261 ms |
324880 KB |
Output is correct |
4 |
Correct |
320 ms |
324700 KB |
Output is correct |
5 |
Correct |
290 ms |
325588 KB |
Output is correct |
6 |
Correct |
299 ms |
326368 KB |
Output is correct |
7 |
Correct |
287 ms |
326720 KB |
Output is correct |
8 |
Correct |
340 ms |
333352 KB |
Output is correct |
9 |
Correct |
363 ms |
333364 KB |
Output is correct |
10 |
Correct |
335 ms |
325084 KB |
Output is correct |
11 |
Correct |
410 ms |
328388 KB |
Output is correct |
12 |
Correct |
293 ms |
325316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
314316 KB |
Output is correct |
2 |
Correct |
118 ms |
314208 KB |
Output is correct |
3 |
Correct |
133 ms |
314328 KB |
Output is correct |
4 |
Correct |
127 ms |
314392 KB |
Output is correct |
5 |
Correct |
125 ms |
314444 KB |
Output is correct |
6 |
Correct |
123 ms |
314380 KB |
Output is correct |
7 |
Correct |
138 ms |
314464 KB |
Output is correct |
8 |
Correct |
128 ms |
314392 KB |
Output is correct |
9 |
Correct |
126 ms |
314268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
314316 KB |
Output is correct |
2 |
Correct |
118 ms |
314208 KB |
Output is correct |
3 |
Correct |
133 ms |
314328 KB |
Output is correct |
4 |
Correct |
127 ms |
314392 KB |
Output is correct |
5 |
Correct |
125 ms |
314444 KB |
Output is correct |
6 |
Correct |
123 ms |
314380 KB |
Output is correct |
7 |
Correct |
138 ms |
314464 KB |
Output is correct |
8 |
Correct |
128 ms |
314392 KB |
Output is correct |
9 |
Correct |
126 ms |
314268 KB |
Output is correct |
10 |
Correct |
126 ms |
314336 KB |
Output is correct |
11 |
Correct |
130 ms |
314308 KB |
Output is correct |
12 |
Correct |
138 ms |
314332 KB |
Output is correct |
13 |
Correct |
119 ms |
314352 KB |
Output is correct |
14 |
Correct |
130 ms |
314396 KB |
Output is correct |
15 |
Correct |
124 ms |
314320 KB |
Output is correct |
16 |
Correct |
119 ms |
314376 KB |
Output is correct |
17 |
Correct |
131 ms |
314472 KB |
Output is correct |
18 |
Correct |
129 ms |
314224 KB |
Output is correct |
19 |
Correct |
169 ms |
315392 KB |
Output is correct |
20 |
Correct |
158 ms |
315340 KB |
Output is correct |
21 |
Correct |
158 ms |
315708 KB |
Output is correct |
22 |
Correct |
176 ms |
315660 KB |
Output is correct |
23 |
Correct |
154 ms |
315912 KB |
Output is correct |
24 |
Correct |
169 ms |
315852 KB |
Output is correct |
25 |
Correct |
183 ms |
315552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
314316 KB |
Output is correct |
2 |
Correct |
118 ms |
314208 KB |
Output is correct |
3 |
Correct |
133 ms |
314328 KB |
Output is correct |
4 |
Correct |
127 ms |
314392 KB |
Output is correct |
5 |
Correct |
125 ms |
314444 KB |
Output is correct |
6 |
Correct |
123 ms |
314380 KB |
Output is correct |
7 |
Correct |
138 ms |
314464 KB |
Output is correct |
8 |
Correct |
128 ms |
314392 KB |
Output is correct |
9 |
Correct |
126 ms |
314268 KB |
Output is correct |
10 |
Correct |
117 ms |
314188 KB |
Output is correct |
11 |
Correct |
120 ms |
314240 KB |
Output is correct |
12 |
Correct |
131 ms |
314304 KB |
Output is correct |
13 |
Correct |
128 ms |
314352 KB |
Output is correct |
14 |
Correct |
131 ms |
314396 KB |
Output is correct |
15 |
Correct |
114 ms |
314372 KB |
Output is correct |
16 |
Correct |
119 ms |
314444 KB |
Output is correct |
17 |
Correct |
130 ms |
314380 KB |
Output is correct |
18 |
Correct |
129 ms |
314260 KB |
Output is correct |
19 |
Correct |
220 ms |
324156 KB |
Output is correct |
20 |
Correct |
211 ms |
324200 KB |
Output is correct |
21 |
Correct |
224 ms |
324212 KB |
Output is correct |
22 |
Correct |
259 ms |
327172 KB |
Output is correct |
23 |
Correct |
262 ms |
332228 KB |
Output is correct |
24 |
Correct |
264 ms |
332312 KB |
Output is correct |
25 |
Correct |
140 ms |
315964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
267 ms |
324696 KB |
Output is correct |
2 |
Correct |
291 ms |
324708 KB |
Output is correct |
3 |
Correct |
265 ms |
324796 KB |
Output is correct |
4 |
Correct |
308 ms |
333360 KB |
Output is correct |
5 |
Correct |
345 ms |
325076 KB |
Output is correct |
6 |
Correct |
361 ms |
333028 KB |
Output is correct |
7 |
Correct |
383 ms |
332988 KB |
Output is correct |
8 |
Correct |
395 ms |
333176 KB |
Output is correct |
9 |
Correct |
281 ms |
332276 KB |
Output is correct |
10 |
Correct |
308 ms |
332628 KB |
Output is correct |
11 |
Correct |
305 ms |
332624 KB |
Output is correct |
12 |
Correct |
303 ms |
332720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
132 ms |
314304 KB |
Output is correct |
2 |
Correct |
265 ms |
324812 KB |
Output is correct |
3 |
Correct |
261 ms |
324880 KB |
Output is correct |
4 |
Correct |
320 ms |
324700 KB |
Output is correct |
5 |
Correct |
290 ms |
325588 KB |
Output is correct |
6 |
Correct |
299 ms |
326368 KB |
Output is correct |
7 |
Correct |
287 ms |
326720 KB |
Output is correct |
8 |
Correct |
340 ms |
333352 KB |
Output is correct |
9 |
Correct |
363 ms |
333364 KB |
Output is correct |
10 |
Correct |
335 ms |
325084 KB |
Output is correct |
11 |
Correct |
410 ms |
328388 KB |
Output is correct |
12 |
Correct |
293 ms |
325316 KB |
Output is correct |
13 |
Correct |
121 ms |
314316 KB |
Output is correct |
14 |
Correct |
118 ms |
314208 KB |
Output is correct |
15 |
Correct |
133 ms |
314328 KB |
Output is correct |
16 |
Correct |
127 ms |
314392 KB |
Output is correct |
17 |
Correct |
125 ms |
314444 KB |
Output is correct |
18 |
Correct |
123 ms |
314380 KB |
Output is correct |
19 |
Correct |
138 ms |
314464 KB |
Output is correct |
20 |
Correct |
128 ms |
314392 KB |
Output is correct |
21 |
Correct |
126 ms |
314268 KB |
Output is correct |
22 |
Correct |
126 ms |
314336 KB |
Output is correct |
23 |
Correct |
130 ms |
314308 KB |
Output is correct |
24 |
Correct |
138 ms |
314332 KB |
Output is correct |
25 |
Correct |
119 ms |
314352 KB |
Output is correct |
26 |
Correct |
130 ms |
314396 KB |
Output is correct |
27 |
Correct |
124 ms |
314320 KB |
Output is correct |
28 |
Correct |
119 ms |
314376 KB |
Output is correct |
29 |
Correct |
131 ms |
314472 KB |
Output is correct |
30 |
Correct |
129 ms |
314224 KB |
Output is correct |
31 |
Correct |
169 ms |
315392 KB |
Output is correct |
32 |
Correct |
158 ms |
315340 KB |
Output is correct |
33 |
Correct |
158 ms |
315708 KB |
Output is correct |
34 |
Correct |
176 ms |
315660 KB |
Output is correct |
35 |
Correct |
154 ms |
315912 KB |
Output is correct |
36 |
Correct |
169 ms |
315852 KB |
Output is correct |
37 |
Correct |
183 ms |
315552 KB |
Output is correct |
38 |
Correct |
117 ms |
314188 KB |
Output is correct |
39 |
Correct |
120 ms |
314240 KB |
Output is correct |
40 |
Correct |
131 ms |
314304 KB |
Output is correct |
41 |
Correct |
128 ms |
314352 KB |
Output is correct |
42 |
Correct |
131 ms |
314396 KB |
Output is correct |
43 |
Correct |
114 ms |
314372 KB |
Output is correct |
44 |
Correct |
119 ms |
314444 KB |
Output is correct |
45 |
Correct |
130 ms |
314380 KB |
Output is correct |
46 |
Correct |
129 ms |
314260 KB |
Output is correct |
47 |
Correct |
220 ms |
324156 KB |
Output is correct |
48 |
Correct |
211 ms |
324200 KB |
Output is correct |
49 |
Correct |
224 ms |
324212 KB |
Output is correct |
50 |
Correct |
259 ms |
327172 KB |
Output is correct |
51 |
Correct |
262 ms |
332228 KB |
Output is correct |
52 |
Correct |
264 ms |
332312 KB |
Output is correct |
53 |
Correct |
140 ms |
315964 KB |
Output is correct |
54 |
Correct |
267 ms |
324696 KB |
Output is correct |
55 |
Correct |
291 ms |
324708 KB |
Output is correct |
56 |
Correct |
265 ms |
324796 KB |
Output is correct |
57 |
Correct |
308 ms |
333360 KB |
Output is correct |
58 |
Correct |
345 ms |
325076 KB |
Output is correct |
59 |
Correct |
361 ms |
333028 KB |
Output is correct |
60 |
Correct |
383 ms |
332988 KB |
Output is correct |
61 |
Correct |
395 ms |
333176 KB |
Output is correct |
62 |
Correct |
281 ms |
332276 KB |
Output is correct |
63 |
Correct |
308 ms |
332628 KB |
Output is correct |
64 |
Correct |
305 ms |
332624 KB |
Output is correct |
65 |
Correct |
303 ms |
332720 KB |
Output is correct |
66 |
Correct |
126 ms |
314312 KB |
Output is correct |
67 |
Correct |
250 ms |
324772 KB |
Output is correct |
68 |
Correct |
255 ms |
324924 KB |
Output is correct |
69 |
Correct |
321 ms |
324700 KB |
Output is correct |
70 |
Correct |
265 ms |
325692 KB |
Output is correct |
71 |
Correct |
310 ms |
326448 KB |
Output is correct |
72 |
Correct |
272 ms |
326700 KB |
Output is correct |
73 |
Correct |
324 ms |
333436 KB |
Output is correct |
74 |
Correct |
306 ms |
333432 KB |
Output is correct |
75 |
Correct |
323 ms |
325160 KB |
Output is correct |
76 |
Correct |
381 ms |
328360 KB |
Output is correct |
77 |
Correct |
228 ms |
325312 KB |
Output is correct |
78 |
Correct |
150 ms |
314288 KB |
Output is correct |
79 |
Correct |
115 ms |
314396 KB |
Output is correct |
80 |
Correct |
120 ms |
314396 KB |
Output is correct |
81 |
Correct |
121 ms |
314376 KB |
Output is correct |
82 |
Correct |
135 ms |
314340 KB |
Output is correct |
83 |
Correct |
121 ms |
314452 KB |
Output is correct |
84 |
Correct |
124 ms |
314516 KB |
Output is correct |
85 |
Correct |
133 ms |
314276 KB |
Output is correct |
86 |
Correct |
168 ms |
315432 KB |
Output is correct |
87 |
Correct |
154 ms |
315332 KB |
Output is correct |
88 |
Correct |
169 ms |
315632 KB |
Output is correct |
89 |
Correct |
167 ms |
315592 KB |
Output is correct |
90 |
Correct |
160 ms |
315872 KB |
Output is correct |
91 |
Correct |
153 ms |
315932 KB |
Output is correct |
92 |
Correct |
177 ms |
315596 KB |
Output is correct |
93 |
Correct |
230 ms |
324196 KB |
Output is correct |
94 |
Correct |
222 ms |
324160 KB |
Output is correct |
95 |
Correct |
231 ms |
324192 KB |
Output is correct |
96 |
Correct |
241 ms |
327228 KB |
Output is correct |
97 |
Correct |
286 ms |
332272 KB |
Output is correct |
98 |
Correct |
276 ms |
332304 KB |
Output is correct |
99 |
Correct |
134 ms |
315968 KB |
Output is correct |
100 |
Correct |
397 ms |
333028 KB |
Output is correct |
101 |
Correct |
428 ms |
332968 KB |
Output is correct |
102 |
Correct |
429 ms |
333136 KB |
Output is correct |
103 |
Correct |
323 ms |
332784 KB |
Output is correct |
104 |
Correct |
332 ms |
332512 KB |
Output is correct |
105 |
Correct |
303 ms |
332636 KB |
Output is correct |
106 |
Correct |
300 ms |
322932 KB |
Output is correct |
107 |
Correct |
330 ms |
324916 KB |
Output is correct |
108 |
Correct |
299 ms |
325228 KB |
Output is correct |
109 |
Correct |
296 ms |
325004 KB |
Output is correct |
110 |
Correct |
338 ms |
332940 KB |
Output is correct |
111 |
Correct |
326 ms |
332916 KB |
Output is correct |