#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#define PB push_back
using namespace std;
const int N = 2e5 + 500;
const int OFF = (1 << 18);
const int LOG = 19;
const int INF = 0x3f3f3f3f;
int n, q, L[N], R[N], T[20][2 * OFF];
int dp[LOG][N], dobar[LOG][N];
vector < int > saz;
void build(int j){
for(int i = OFF - 1; i ; i--)
T[j][i] = min(T[j][2 * i], T[j][2 * i + 1]);
}
int query(int j, int i, int a, int b, int lo, int hi){
if(lo <= a && b <= hi) return T[j][i];
if(a > hi || b < lo) return INF;
return min(query(j, 2 * i, a, (a + b) / 2, lo, hi), query(j, 2 * i + 1, (a + b) / 2 + 1, b, lo, hi));
}
int main(){
memset(T, INF, sizeof(T));
scanf("%d%d", &n, &q);
for(int i = 0;i < n;i++){
scanf("%d%d", L + i, R + i);
saz.PB(L[i]); saz.PB(R[i]);
dp[0][i] = L[i];
}
sort(saz.begin(), saz.end());
saz.erase(unique(saz.begin(), saz.end()), saz.end());
for(int i = 0;i < n;i++){
L[i] = lower_bound(saz.begin(), saz.end(), L[i]) - saz.begin();
R[i] = lower_bound(saz.begin(), saz.end(), R[i]) - saz.begin();
// printf("i = (%d %d)\n", L[i], R[i]);
dp[0][i] = L[i];
dobar[0][i] = 1;
T[0][OFF + R[i]] = min(T[0][OFF + R[i]], L[i]);
}
build(0);
for(int j = 1;j < LOG;j++){
for(int i = 0;i < n;i++){
if(!dobar[j - 1][i]){
dp[j][i] = dp[j - 1][i];
continue;
}
dp[j][i] = query(j - 1, 1, 0, OFF - 1, dp[j - 1][i], R[i]);
if(dp[j][i] < dp[j - 1][i]){
dobar[j][i] = 1;
T[j][OFF + R[i]] = min(T[j][OFF + R[i]], dp[j][i]);
}
// printf("dp[ %d ][ %d ] = %d\n", j, i, dp[j][i]);
}
build(j);
}
for(int i = 0;i < q;i++){
int s, t; scanf("%d%d", &s, &t);
s--; t--;
if(R[s] > R[t] || dp[LOG - 1][t] > R[s]){
printf("impossible\n");
continue;
}
if(s == t){
printf("0\n");
continue;
}
if(R[s] >= L[t]){
printf("1\n");
continue;
}
int odg = 0, curL = L[t];
for(int j = LOG - 1;j >= 0;j--){
int nw = query(j, 1, 0, OFF - 1, curL, R[t]);
if(nw > R[s] && nw < curL){
// printf("uzeo j = %d\n", j);
// printf("prije %d sad %d\n", curL, nw);
odg += (1 << j);
curL = nw;
}
}
printf("%d\n", odg + 2);
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:32:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
events.cpp:34:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | scanf("%d%d", L + i, R + i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
events.cpp:65:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | int s, t; scanf("%d%d", &s, &t);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
41428 KB |
Output is correct |
2 |
Correct |
1253 ms |
58248 KB |
Output is correct |
3 |
Correct |
1128 ms |
58228 KB |
Output is correct |
4 |
Correct |
1113 ms |
58204 KB |
Output is correct |
5 |
Correct |
1052 ms |
58224 KB |
Output is correct |
6 |
Correct |
961 ms |
58444 KB |
Output is correct |
7 |
Correct |
893 ms |
58340 KB |
Output is correct |
8 |
Correct |
149 ms |
52040 KB |
Output is correct |
9 |
Correct |
127 ms |
51976 KB |
Output is correct |
10 |
Correct |
791 ms |
58192 KB |
Output is correct |
11 |
Correct |
593 ms |
58300 KB |
Output is correct |
12 |
Correct |
97 ms |
52040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
41428 KB |
Output is correct |
2 |
Correct |
24 ms |
41428 KB |
Output is correct |
3 |
Correct |
26 ms |
41560 KB |
Output is correct |
4 |
Correct |
26 ms |
41584 KB |
Output is correct |
5 |
Correct |
25 ms |
41556 KB |
Output is correct |
6 |
Correct |
23 ms |
41580 KB |
Output is correct |
7 |
Correct |
23 ms |
41556 KB |
Output is correct |
8 |
Correct |
22 ms |
41520 KB |
Output is correct |
9 |
Correct |
27 ms |
41568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
41428 KB |
Output is correct |
2 |
Correct |
24 ms |
41428 KB |
Output is correct |
3 |
Correct |
26 ms |
41560 KB |
Output is correct |
4 |
Correct |
26 ms |
41584 KB |
Output is correct |
5 |
Correct |
25 ms |
41556 KB |
Output is correct |
6 |
Correct |
23 ms |
41580 KB |
Output is correct |
7 |
Correct |
23 ms |
41556 KB |
Output is correct |
8 |
Correct |
22 ms |
41520 KB |
Output is correct |
9 |
Correct |
27 ms |
41568 KB |
Output is correct |
10 |
Correct |
26 ms |
41404 KB |
Output is correct |
11 |
Correct |
29 ms |
41412 KB |
Output is correct |
12 |
Correct |
27 ms |
41652 KB |
Output is correct |
13 |
Correct |
26 ms |
41632 KB |
Output is correct |
14 |
Correct |
25 ms |
41604 KB |
Output is correct |
15 |
Correct |
23 ms |
41532 KB |
Output is correct |
16 |
Correct |
23 ms |
41544 KB |
Output is correct |
17 |
Correct |
22 ms |
41556 KB |
Output is correct |
18 |
Correct |
22 ms |
41516 KB |
Output is correct |
19 |
Correct |
609 ms |
42724 KB |
Output is correct |
20 |
Correct |
529 ms |
42776 KB |
Output is correct |
21 |
Correct |
336 ms |
43068 KB |
Output is correct |
22 |
Correct |
110 ms |
42820 KB |
Output is correct |
23 |
Correct |
144 ms |
42636 KB |
Output is correct |
24 |
Correct |
51 ms |
42552 KB |
Output is correct |
25 |
Correct |
508 ms |
42396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
41428 KB |
Output is correct |
2 |
Correct |
24 ms |
41428 KB |
Output is correct |
3 |
Correct |
26 ms |
41560 KB |
Output is correct |
4 |
Correct |
26 ms |
41584 KB |
Output is correct |
5 |
Correct |
25 ms |
41556 KB |
Output is correct |
6 |
Correct |
23 ms |
41580 KB |
Output is correct |
7 |
Correct |
23 ms |
41556 KB |
Output is correct |
8 |
Correct |
22 ms |
41520 KB |
Output is correct |
9 |
Correct |
27 ms |
41568 KB |
Output is correct |
10 |
Correct |
23 ms |
41344 KB |
Output is correct |
11 |
Correct |
25 ms |
41536 KB |
Output is correct |
12 |
Correct |
28 ms |
41564 KB |
Output is correct |
13 |
Correct |
27 ms |
41500 KB |
Output is correct |
14 |
Correct |
25 ms |
41644 KB |
Output is correct |
15 |
Correct |
23 ms |
41548 KB |
Output is correct |
16 |
Correct |
23 ms |
41528 KB |
Output is correct |
17 |
Correct |
23 ms |
41556 KB |
Output is correct |
18 |
Correct |
23 ms |
41504 KB |
Output is correct |
19 |
Correct |
568 ms |
57552 KB |
Output is correct |
20 |
Correct |
558 ms |
57636 KB |
Output is correct |
21 |
Correct |
163 ms |
51684 KB |
Output is correct |
22 |
Correct |
380 ms |
57296 KB |
Output is correct |
23 |
Correct |
153 ms |
51256 KB |
Output is correct |
24 |
Correct |
348 ms |
54468 KB |
Output is correct |
25 |
Correct |
64 ms |
50876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1205 ms |
58340 KB |
Output is correct |
2 |
Correct |
1133 ms |
58376 KB |
Output is correct |
3 |
Correct |
1137 ms |
58284 KB |
Output is correct |
4 |
Correct |
136 ms |
52060 KB |
Output is correct |
5 |
Correct |
801 ms |
58208 KB |
Output is correct |
6 |
Correct |
918 ms |
55952 KB |
Output is correct |
7 |
Correct |
845 ms |
55748 KB |
Output is correct |
8 |
Correct |
715 ms |
55688 KB |
Output is correct |
9 |
Correct |
147 ms |
51236 KB |
Output is correct |
10 |
Correct |
884 ms |
54984 KB |
Output is correct |
11 |
Correct |
753 ms |
52444 KB |
Output is correct |
12 |
Correct |
846 ms |
55072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
41428 KB |
Output is correct |
2 |
Correct |
1253 ms |
58248 KB |
Output is correct |
3 |
Correct |
1128 ms |
58228 KB |
Output is correct |
4 |
Correct |
1113 ms |
58204 KB |
Output is correct |
5 |
Correct |
1052 ms |
58224 KB |
Output is correct |
6 |
Correct |
961 ms |
58444 KB |
Output is correct |
7 |
Correct |
893 ms |
58340 KB |
Output is correct |
8 |
Correct |
149 ms |
52040 KB |
Output is correct |
9 |
Correct |
127 ms |
51976 KB |
Output is correct |
10 |
Correct |
791 ms |
58192 KB |
Output is correct |
11 |
Correct |
593 ms |
58300 KB |
Output is correct |
12 |
Correct |
97 ms |
52040 KB |
Output is correct |
13 |
Correct |
23 ms |
41428 KB |
Output is correct |
14 |
Correct |
24 ms |
41428 KB |
Output is correct |
15 |
Correct |
26 ms |
41560 KB |
Output is correct |
16 |
Correct |
26 ms |
41584 KB |
Output is correct |
17 |
Correct |
25 ms |
41556 KB |
Output is correct |
18 |
Correct |
23 ms |
41580 KB |
Output is correct |
19 |
Correct |
23 ms |
41556 KB |
Output is correct |
20 |
Correct |
22 ms |
41520 KB |
Output is correct |
21 |
Correct |
27 ms |
41568 KB |
Output is correct |
22 |
Correct |
26 ms |
41404 KB |
Output is correct |
23 |
Correct |
29 ms |
41412 KB |
Output is correct |
24 |
Correct |
27 ms |
41652 KB |
Output is correct |
25 |
Correct |
26 ms |
41632 KB |
Output is correct |
26 |
Correct |
25 ms |
41604 KB |
Output is correct |
27 |
Correct |
23 ms |
41532 KB |
Output is correct |
28 |
Correct |
23 ms |
41544 KB |
Output is correct |
29 |
Correct |
22 ms |
41556 KB |
Output is correct |
30 |
Correct |
22 ms |
41516 KB |
Output is correct |
31 |
Correct |
609 ms |
42724 KB |
Output is correct |
32 |
Correct |
529 ms |
42776 KB |
Output is correct |
33 |
Correct |
336 ms |
43068 KB |
Output is correct |
34 |
Correct |
110 ms |
42820 KB |
Output is correct |
35 |
Correct |
144 ms |
42636 KB |
Output is correct |
36 |
Correct |
51 ms |
42552 KB |
Output is correct |
37 |
Correct |
508 ms |
42396 KB |
Output is correct |
38 |
Correct |
23 ms |
41344 KB |
Output is correct |
39 |
Correct |
25 ms |
41536 KB |
Output is correct |
40 |
Correct |
28 ms |
41564 KB |
Output is correct |
41 |
Correct |
27 ms |
41500 KB |
Output is correct |
42 |
Correct |
25 ms |
41644 KB |
Output is correct |
43 |
Correct |
23 ms |
41548 KB |
Output is correct |
44 |
Correct |
23 ms |
41528 KB |
Output is correct |
45 |
Correct |
23 ms |
41556 KB |
Output is correct |
46 |
Correct |
23 ms |
41504 KB |
Output is correct |
47 |
Correct |
568 ms |
57552 KB |
Output is correct |
48 |
Correct |
558 ms |
57636 KB |
Output is correct |
49 |
Correct |
163 ms |
51684 KB |
Output is correct |
50 |
Correct |
380 ms |
57296 KB |
Output is correct |
51 |
Correct |
153 ms |
51256 KB |
Output is correct |
52 |
Correct |
348 ms |
54468 KB |
Output is correct |
53 |
Correct |
64 ms |
50876 KB |
Output is correct |
54 |
Correct |
1205 ms |
58340 KB |
Output is correct |
55 |
Correct |
1133 ms |
58376 KB |
Output is correct |
56 |
Correct |
1137 ms |
58284 KB |
Output is correct |
57 |
Correct |
136 ms |
52060 KB |
Output is correct |
58 |
Correct |
801 ms |
58208 KB |
Output is correct |
59 |
Correct |
918 ms |
55952 KB |
Output is correct |
60 |
Correct |
845 ms |
55748 KB |
Output is correct |
61 |
Correct |
715 ms |
55688 KB |
Output is correct |
62 |
Correct |
147 ms |
51236 KB |
Output is correct |
63 |
Correct |
884 ms |
54984 KB |
Output is correct |
64 |
Correct |
753 ms |
52444 KB |
Output is correct |
65 |
Correct |
846 ms |
55072 KB |
Output is correct |
66 |
Correct |
22 ms |
41428 KB |
Output is correct |
67 |
Correct |
1203 ms |
58384 KB |
Output is correct |
68 |
Correct |
1131 ms |
58144 KB |
Output is correct |
69 |
Correct |
1152 ms |
58212 KB |
Output is correct |
70 |
Correct |
1039 ms |
58364 KB |
Output is correct |
71 |
Correct |
1018 ms |
58212 KB |
Output is correct |
72 |
Correct |
1010 ms |
58364 KB |
Output is correct |
73 |
Correct |
135 ms |
52012 KB |
Output is correct |
74 |
Correct |
131 ms |
51964 KB |
Output is correct |
75 |
Correct |
775 ms |
58148 KB |
Output is correct |
76 |
Correct |
552 ms |
58192 KB |
Output is correct |
77 |
Correct |
95 ms |
51960 KB |
Output is correct |
78 |
Correct |
23 ms |
41400 KB |
Output is correct |
79 |
Correct |
25 ms |
41556 KB |
Output is correct |
80 |
Correct |
25 ms |
41624 KB |
Output is correct |
81 |
Correct |
25 ms |
41596 KB |
Output is correct |
82 |
Correct |
28 ms |
41544 KB |
Output is correct |
83 |
Correct |
25 ms |
41532 KB |
Output is correct |
84 |
Correct |
24 ms |
41556 KB |
Output is correct |
85 |
Correct |
23 ms |
41560 KB |
Output is correct |
86 |
Correct |
609 ms |
42780 KB |
Output is correct |
87 |
Correct |
502 ms |
42820 KB |
Output is correct |
88 |
Correct |
313 ms |
42956 KB |
Output is correct |
89 |
Correct |
108 ms |
42824 KB |
Output is correct |
90 |
Correct |
174 ms |
42592 KB |
Output is correct |
91 |
Correct |
52 ms |
42616 KB |
Output is correct |
92 |
Correct |
448 ms |
42436 KB |
Output is correct |
93 |
Correct |
601 ms |
57656 KB |
Output is correct |
94 |
Correct |
569 ms |
57576 KB |
Output is correct |
95 |
Correct |
128 ms |
51708 KB |
Output is correct |
96 |
Correct |
463 ms |
57240 KB |
Output is correct |
97 |
Correct |
140 ms |
51352 KB |
Output is correct |
98 |
Correct |
375 ms |
54472 KB |
Output is correct |
99 |
Correct |
66 ms |
50908 KB |
Output is correct |
100 |
Correct |
965 ms |
56020 KB |
Output is correct |
101 |
Correct |
860 ms |
55680 KB |
Output is correct |
102 |
Correct |
719 ms |
55920 KB |
Output is correct |
103 |
Correct |
922 ms |
54964 KB |
Output is correct |
104 |
Correct |
774 ms |
52420 KB |
Output is correct |
105 |
Correct |
939 ms |
54912 KB |
Output is correct |
106 |
Incorrect |
742 ms |
58308 KB |
Output isn't correct |
107 |
Halted |
0 ms |
0 KB |
- |