#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);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
41428 KB |
Output is correct |
2 |
Correct |
1140 ms |
58240 KB |
Output is correct |
3 |
Correct |
1231 ms |
58200 KB |
Output is correct |
4 |
Correct |
1175 ms |
58184 KB |
Output is correct |
5 |
Correct |
1240 ms |
58272 KB |
Output is correct |
6 |
Correct |
1004 ms |
58304 KB |
Output is correct |
7 |
Correct |
964 ms |
58300 KB |
Output is correct |
8 |
Correct |
137 ms |
51972 KB |
Output is correct |
9 |
Correct |
148 ms |
52044 KB |
Output is correct |
10 |
Correct |
764 ms |
58120 KB |
Output is correct |
11 |
Correct |
552 ms |
58280 KB |
Output is correct |
12 |
Correct |
93 ms |
52000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
41436 KB |
Output is correct |
2 |
Correct |
23 ms |
41420 KB |
Output is correct |
3 |
Correct |
26 ms |
41548 KB |
Output is correct |
4 |
Correct |
26 ms |
41580 KB |
Output is correct |
5 |
Correct |
27 ms |
41548 KB |
Output is correct |
6 |
Correct |
25 ms |
41584 KB |
Output is correct |
7 |
Correct |
25 ms |
41548 KB |
Output is correct |
8 |
Correct |
24 ms |
41548 KB |
Output is correct |
9 |
Correct |
24 ms |
41488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
41436 KB |
Output is correct |
2 |
Correct |
23 ms |
41420 KB |
Output is correct |
3 |
Correct |
26 ms |
41548 KB |
Output is correct |
4 |
Correct |
26 ms |
41580 KB |
Output is correct |
5 |
Correct |
27 ms |
41548 KB |
Output is correct |
6 |
Correct |
25 ms |
41584 KB |
Output is correct |
7 |
Correct |
25 ms |
41548 KB |
Output is correct |
8 |
Correct |
24 ms |
41548 KB |
Output is correct |
9 |
Correct |
24 ms |
41488 KB |
Output is correct |
10 |
Correct |
23 ms |
41396 KB |
Output is correct |
11 |
Correct |
23 ms |
41412 KB |
Output is correct |
12 |
Correct |
29 ms |
41716 KB |
Output is correct |
13 |
Correct |
28 ms |
41556 KB |
Output is correct |
14 |
Correct |
32 ms |
41604 KB |
Output is correct |
15 |
Correct |
24 ms |
41556 KB |
Output is correct |
16 |
Correct |
25 ms |
41540 KB |
Output is correct |
17 |
Correct |
25 ms |
41556 KB |
Output is correct |
18 |
Correct |
22 ms |
41516 KB |
Output is correct |
19 |
Correct |
633 ms |
42664 KB |
Output is correct |
20 |
Correct |
556 ms |
42764 KB |
Output is correct |
21 |
Correct |
305 ms |
42968 KB |
Output is correct |
22 |
Correct |
117 ms |
42752 KB |
Output is correct |
23 |
Correct |
161 ms |
42668 KB |
Output is correct |
24 |
Correct |
49 ms |
42556 KB |
Output is correct |
25 |
Correct |
460 ms |
42408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
41436 KB |
Output is correct |
2 |
Correct |
23 ms |
41420 KB |
Output is correct |
3 |
Correct |
26 ms |
41548 KB |
Output is correct |
4 |
Correct |
26 ms |
41580 KB |
Output is correct |
5 |
Correct |
27 ms |
41548 KB |
Output is correct |
6 |
Correct |
25 ms |
41584 KB |
Output is correct |
7 |
Correct |
25 ms |
41548 KB |
Output is correct |
8 |
Correct |
24 ms |
41548 KB |
Output is correct |
9 |
Correct |
24 ms |
41488 KB |
Output is correct |
10 |
Correct |
23 ms |
41404 KB |
Output is correct |
11 |
Correct |
24 ms |
41472 KB |
Output is correct |
12 |
Correct |
29 ms |
41528 KB |
Output is correct |
13 |
Correct |
28 ms |
41596 KB |
Output is correct |
14 |
Correct |
29 ms |
41652 KB |
Output is correct |
15 |
Correct |
22 ms |
41556 KB |
Output is correct |
16 |
Correct |
24 ms |
41504 KB |
Output is correct |
17 |
Correct |
24 ms |
41524 KB |
Output is correct |
18 |
Correct |
23 ms |
41536 KB |
Output is correct |
19 |
Correct |
572 ms |
57652 KB |
Output is correct |
20 |
Correct |
557 ms |
57632 KB |
Output is correct |
21 |
Correct |
126 ms |
51768 KB |
Output is correct |
22 |
Correct |
388 ms |
57272 KB |
Output is correct |
23 |
Correct |
148 ms |
51240 KB |
Output is correct |
24 |
Correct |
342 ms |
54480 KB |
Output is correct |
25 |
Correct |
64 ms |
50968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1162 ms |
58264 KB |
Output is correct |
2 |
Correct |
1134 ms |
58268 KB |
Output is correct |
3 |
Correct |
1107 ms |
58160 KB |
Output is correct |
4 |
Correct |
127 ms |
52028 KB |
Output is correct |
5 |
Correct |
766 ms |
58188 KB |
Output is correct |
6 |
Correct |
842 ms |
56104 KB |
Output is correct |
7 |
Correct |
866 ms |
55828 KB |
Output is correct |
8 |
Correct |
743 ms |
55780 KB |
Output is correct |
9 |
Correct |
144 ms |
51240 KB |
Output is correct |
10 |
Correct |
835 ms |
54916 KB |
Output is correct |
11 |
Correct |
743 ms |
52300 KB |
Output is correct |
12 |
Correct |
867 ms |
54828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
41428 KB |
Output is correct |
2 |
Correct |
1140 ms |
58240 KB |
Output is correct |
3 |
Correct |
1231 ms |
58200 KB |
Output is correct |
4 |
Correct |
1175 ms |
58184 KB |
Output is correct |
5 |
Correct |
1240 ms |
58272 KB |
Output is correct |
6 |
Correct |
1004 ms |
58304 KB |
Output is correct |
7 |
Correct |
964 ms |
58300 KB |
Output is correct |
8 |
Correct |
137 ms |
51972 KB |
Output is correct |
9 |
Correct |
148 ms |
52044 KB |
Output is correct |
10 |
Correct |
764 ms |
58120 KB |
Output is correct |
11 |
Correct |
552 ms |
58280 KB |
Output is correct |
12 |
Correct |
93 ms |
52000 KB |
Output is correct |
13 |
Correct |
23 ms |
41436 KB |
Output is correct |
14 |
Correct |
23 ms |
41420 KB |
Output is correct |
15 |
Correct |
26 ms |
41548 KB |
Output is correct |
16 |
Correct |
26 ms |
41580 KB |
Output is correct |
17 |
Correct |
27 ms |
41548 KB |
Output is correct |
18 |
Correct |
25 ms |
41584 KB |
Output is correct |
19 |
Correct |
25 ms |
41548 KB |
Output is correct |
20 |
Correct |
24 ms |
41548 KB |
Output is correct |
21 |
Correct |
24 ms |
41488 KB |
Output is correct |
22 |
Correct |
23 ms |
41396 KB |
Output is correct |
23 |
Correct |
23 ms |
41412 KB |
Output is correct |
24 |
Correct |
29 ms |
41716 KB |
Output is correct |
25 |
Correct |
28 ms |
41556 KB |
Output is correct |
26 |
Correct |
32 ms |
41604 KB |
Output is correct |
27 |
Correct |
24 ms |
41556 KB |
Output is correct |
28 |
Correct |
25 ms |
41540 KB |
Output is correct |
29 |
Correct |
25 ms |
41556 KB |
Output is correct |
30 |
Correct |
22 ms |
41516 KB |
Output is correct |
31 |
Correct |
633 ms |
42664 KB |
Output is correct |
32 |
Correct |
556 ms |
42764 KB |
Output is correct |
33 |
Correct |
305 ms |
42968 KB |
Output is correct |
34 |
Correct |
117 ms |
42752 KB |
Output is correct |
35 |
Correct |
161 ms |
42668 KB |
Output is correct |
36 |
Correct |
49 ms |
42556 KB |
Output is correct |
37 |
Correct |
460 ms |
42408 KB |
Output is correct |
38 |
Correct |
23 ms |
41404 KB |
Output is correct |
39 |
Correct |
24 ms |
41472 KB |
Output is correct |
40 |
Correct |
29 ms |
41528 KB |
Output is correct |
41 |
Correct |
28 ms |
41596 KB |
Output is correct |
42 |
Correct |
29 ms |
41652 KB |
Output is correct |
43 |
Correct |
22 ms |
41556 KB |
Output is correct |
44 |
Correct |
24 ms |
41504 KB |
Output is correct |
45 |
Correct |
24 ms |
41524 KB |
Output is correct |
46 |
Correct |
23 ms |
41536 KB |
Output is correct |
47 |
Correct |
572 ms |
57652 KB |
Output is correct |
48 |
Correct |
557 ms |
57632 KB |
Output is correct |
49 |
Correct |
126 ms |
51768 KB |
Output is correct |
50 |
Correct |
388 ms |
57272 KB |
Output is correct |
51 |
Correct |
148 ms |
51240 KB |
Output is correct |
52 |
Correct |
342 ms |
54480 KB |
Output is correct |
53 |
Correct |
64 ms |
50968 KB |
Output is correct |
54 |
Correct |
1162 ms |
58264 KB |
Output is correct |
55 |
Correct |
1134 ms |
58268 KB |
Output is correct |
56 |
Correct |
1107 ms |
58160 KB |
Output is correct |
57 |
Correct |
127 ms |
52028 KB |
Output is correct |
58 |
Correct |
766 ms |
58188 KB |
Output is correct |
59 |
Correct |
842 ms |
56104 KB |
Output is correct |
60 |
Correct |
866 ms |
55828 KB |
Output is correct |
61 |
Correct |
743 ms |
55780 KB |
Output is correct |
62 |
Correct |
144 ms |
51240 KB |
Output is correct |
63 |
Correct |
835 ms |
54916 KB |
Output is correct |
64 |
Correct |
743 ms |
52300 KB |
Output is correct |
65 |
Correct |
867 ms |
54828 KB |
Output is correct |
66 |
Correct |
23 ms |
41488 KB |
Output is correct |
67 |
Correct |
1133 ms |
58232 KB |
Output is correct |
68 |
Correct |
1131 ms |
58176 KB |
Output is correct |
69 |
Correct |
1099 ms |
58148 KB |
Output is correct |
70 |
Correct |
1028 ms |
58280 KB |
Output is correct |
71 |
Correct |
960 ms |
58436 KB |
Output is correct |
72 |
Correct |
929 ms |
58412 KB |
Output is correct |
73 |
Correct |
131 ms |
52048 KB |
Output is correct |
74 |
Correct |
128 ms |
51956 KB |
Output is correct |
75 |
Correct |
781 ms |
58312 KB |
Output is correct |
76 |
Correct |
565 ms |
58240 KB |
Output is correct |
77 |
Correct |
98 ms |
52004 KB |
Output is correct |
78 |
Correct |
23 ms |
41400 KB |
Output is correct |
79 |
Correct |
26 ms |
41624 KB |
Output is correct |
80 |
Correct |
28 ms |
41536 KB |
Output is correct |
81 |
Correct |
28 ms |
41600 KB |
Output is correct |
82 |
Correct |
23 ms |
41556 KB |
Output is correct |
83 |
Correct |
25 ms |
41488 KB |
Output is correct |
84 |
Correct |
24 ms |
41496 KB |
Output is correct |
85 |
Correct |
24 ms |
41436 KB |
Output is correct |
86 |
Correct |
612 ms |
42616 KB |
Output is correct |
87 |
Correct |
507 ms |
42700 KB |
Output is correct |
88 |
Correct |
303 ms |
43012 KB |
Output is correct |
89 |
Correct |
115 ms |
42728 KB |
Output is correct |
90 |
Correct |
150 ms |
42664 KB |
Output is correct |
91 |
Correct |
53 ms |
42572 KB |
Output is correct |
92 |
Correct |
456 ms |
42404 KB |
Output is correct |
93 |
Correct |
558 ms |
57656 KB |
Output is correct |
94 |
Correct |
538 ms |
57604 KB |
Output is correct |
95 |
Correct |
132 ms |
51736 KB |
Output is correct |
96 |
Correct |
382 ms |
57228 KB |
Output is correct |
97 |
Correct |
142 ms |
51336 KB |
Output is correct |
98 |
Correct |
372 ms |
54468 KB |
Output is correct |
99 |
Correct |
60 ms |
50832 KB |
Output is correct |
100 |
Correct |
872 ms |
56092 KB |
Output is correct |
101 |
Correct |
896 ms |
55544 KB |
Output is correct |
102 |
Correct |
708 ms |
55708 KB |
Output is correct |
103 |
Correct |
860 ms |
54908 KB |
Output is correct |
104 |
Correct |
766 ms |
52324 KB |
Output is correct |
105 |
Correct |
898 ms |
54932 KB |
Output is correct |
106 |
Incorrect |
695 ms |
58308 KB |
Output isn't correct |
107 |
Halted |
0 ms |
0 KB |
- |