#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 = 20;
const int INF = 0x3f3f3f3f;
int n, q, L[N], R[N], T[LOG][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();
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];
}
else{
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:64:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | int s, t; scanf("%d%d", &s, &t);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
41364 KB |
Output is correct |
2 |
Correct |
1239 ms |
58660 KB |
Output is correct |
3 |
Correct |
1325 ms |
58740 KB |
Output is correct |
4 |
Correct |
1185 ms |
58648 KB |
Output is correct |
5 |
Correct |
1069 ms |
58652 KB |
Output is correct |
6 |
Correct |
1018 ms |
58844 KB |
Output is correct |
7 |
Correct |
934 ms |
58800 KB |
Output is correct |
8 |
Correct |
150 ms |
52400 KB |
Output is correct |
9 |
Correct |
147 ms |
52344 KB |
Output is correct |
10 |
Correct |
768 ms |
58620 KB |
Output is correct |
11 |
Correct |
608 ms |
58620 KB |
Output is correct |
12 |
Correct |
111 ms |
52432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
41428 KB |
Output is correct |
2 |
Correct |
26 ms |
41424 KB |
Output is correct |
3 |
Correct |
25 ms |
41572 KB |
Output is correct |
4 |
Correct |
26 ms |
41548 KB |
Output is correct |
5 |
Correct |
29 ms |
41556 KB |
Output is correct |
6 |
Correct |
22 ms |
41484 KB |
Output is correct |
7 |
Correct |
23 ms |
41568 KB |
Output is correct |
8 |
Correct |
28 ms |
41484 KB |
Output is correct |
9 |
Correct |
25 ms |
41556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
41428 KB |
Output is correct |
2 |
Correct |
26 ms |
41424 KB |
Output is correct |
3 |
Correct |
25 ms |
41572 KB |
Output is correct |
4 |
Correct |
26 ms |
41548 KB |
Output is correct |
5 |
Correct |
29 ms |
41556 KB |
Output is correct |
6 |
Correct |
22 ms |
41484 KB |
Output is correct |
7 |
Correct |
23 ms |
41568 KB |
Output is correct |
8 |
Correct |
28 ms |
41484 KB |
Output is correct |
9 |
Correct |
25 ms |
41556 KB |
Output is correct |
10 |
Correct |
21 ms |
41480 KB |
Output is correct |
11 |
Correct |
22 ms |
41396 KB |
Output is correct |
12 |
Correct |
25 ms |
41608 KB |
Output is correct |
13 |
Correct |
27 ms |
41588 KB |
Output is correct |
14 |
Correct |
27 ms |
41708 KB |
Output is correct |
15 |
Correct |
23 ms |
41592 KB |
Output is correct |
16 |
Correct |
23 ms |
41576 KB |
Output is correct |
17 |
Correct |
24 ms |
41464 KB |
Output is correct |
18 |
Correct |
24 ms |
41560 KB |
Output is correct |
19 |
Correct |
695 ms |
42896 KB |
Output is correct |
20 |
Correct |
511 ms |
42696 KB |
Output is correct |
21 |
Correct |
316 ms |
43084 KB |
Output is correct |
22 |
Correct |
109 ms |
42788 KB |
Output is correct |
23 |
Correct |
148 ms |
42700 KB |
Output is correct |
24 |
Correct |
51 ms |
42572 KB |
Output is correct |
25 |
Correct |
467 ms |
42356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
41428 KB |
Output is correct |
2 |
Correct |
26 ms |
41424 KB |
Output is correct |
3 |
Correct |
25 ms |
41572 KB |
Output is correct |
4 |
Correct |
26 ms |
41548 KB |
Output is correct |
5 |
Correct |
29 ms |
41556 KB |
Output is correct |
6 |
Correct |
22 ms |
41484 KB |
Output is correct |
7 |
Correct |
23 ms |
41568 KB |
Output is correct |
8 |
Correct |
28 ms |
41484 KB |
Output is correct |
9 |
Correct |
25 ms |
41556 KB |
Output is correct |
10 |
Correct |
20 ms |
41452 KB |
Output is correct |
11 |
Correct |
21 ms |
41424 KB |
Output is correct |
12 |
Correct |
27 ms |
41588 KB |
Output is correct |
13 |
Correct |
27 ms |
41612 KB |
Output is correct |
14 |
Correct |
25 ms |
41564 KB |
Output is correct |
15 |
Correct |
27 ms |
41556 KB |
Output is correct |
16 |
Correct |
23 ms |
41556 KB |
Output is correct |
17 |
Correct |
22 ms |
41556 KB |
Output is correct |
18 |
Correct |
21 ms |
41556 KB |
Output is correct |
19 |
Correct |
576 ms |
57996 KB |
Output is correct |
20 |
Correct |
525 ms |
57940 KB |
Output is correct |
21 |
Correct |
139 ms |
52160 KB |
Output is correct |
22 |
Correct |
372 ms |
57560 KB |
Output is correct |
23 |
Correct |
170 ms |
51636 KB |
Output is correct |
24 |
Correct |
400 ms |
54840 KB |
Output is correct |
25 |
Correct |
67 ms |
51276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1084 ms |
58652 KB |
Output is correct |
2 |
Correct |
1077 ms |
58604 KB |
Output is correct |
3 |
Correct |
1075 ms |
58780 KB |
Output is correct |
4 |
Correct |
143 ms |
52436 KB |
Output is correct |
5 |
Correct |
734 ms |
58620 KB |
Output is correct |
6 |
Correct |
852 ms |
56420 KB |
Output is correct |
7 |
Correct |
804 ms |
56064 KB |
Output is correct |
8 |
Correct |
700 ms |
56248 KB |
Output is correct |
9 |
Correct |
150 ms |
51732 KB |
Output is correct |
10 |
Correct |
843 ms |
55348 KB |
Output is correct |
11 |
Correct |
755 ms |
52736 KB |
Output is correct |
12 |
Correct |
854 ms |
55308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
41364 KB |
Output is correct |
2 |
Correct |
1239 ms |
58660 KB |
Output is correct |
3 |
Correct |
1325 ms |
58740 KB |
Output is correct |
4 |
Correct |
1185 ms |
58648 KB |
Output is correct |
5 |
Correct |
1069 ms |
58652 KB |
Output is correct |
6 |
Correct |
1018 ms |
58844 KB |
Output is correct |
7 |
Correct |
934 ms |
58800 KB |
Output is correct |
8 |
Correct |
150 ms |
52400 KB |
Output is correct |
9 |
Correct |
147 ms |
52344 KB |
Output is correct |
10 |
Correct |
768 ms |
58620 KB |
Output is correct |
11 |
Correct |
608 ms |
58620 KB |
Output is correct |
12 |
Correct |
111 ms |
52432 KB |
Output is correct |
13 |
Correct |
21 ms |
41428 KB |
Output is correct |
14 |
Correct |
26 ms |
41424 KB |
Output is correct |
15 |
Correct |
25 ms |
41572 KB |
Output is correct |
16 |
Correct |
26 ms |
41548 KB |
Output is correct |
17 |
Correct |
29 ms |
41556 KB |
Output is correct |
18 |
Correct |
22 ms |
41484 KB |
Output is correct |
19 |
Correct |
23 ms |
41568 KB |
Output is correct |
20 |
Correct |
28 ms |
41484 KB |
Output is correct |
21 |
Correct |
25 ms |
41556 KB |
Output is correct |
22 |
Correct |
21 ms |
41480 KB |
Output is correct |
23 |
Correct |
22 ms |
41396 KB |
Output is correct |
24 |
Correct |
25 ms |
41608 KB |
Output is correct |
25 |
Correct |
27 ms |
41588 KB |
Output is correct |
26 |
Correct |
27 ms |
41708 KB |
Output is correct |
27 |
Correct |
23 ms |
41592 KB |
Output is correct |
28 |
Correct |
23 ms |
41576 KB |
Output is correct |
29 |
Correct |
24 ms |
41464 KB |
Output is correct |
30 |
Correct |
24 ms |
41560 KB |
Output is correct |
31 |
Correct |
695 ms |
42896 KB |
Output is correct |
32 |
Correct |
511 ms |
42696 KB |
Output is correct |
33 |
Correct |
316 ms |
43084 KB |
Output is correct |
34 |
Correct |
109 ms |
42788 KB |
Output is correct |
35 |
Correct |
148 ms |
42700 KB |
Output is correct |
36 |
Correct |
51 ms |
42572 KB |
Output is correct |
37 |
Correct |
467 ms |
42356 KB |
Output is correct |
38 |
Correct |
20 ms |
41452 KB |
Output is correct |
39 |
Correct |
21 ms |
41424 KB |
Output is correct |
40 |
Correct |
27 ms |
41588 KB |
Output is correct |
41 |
Correct |
27 ms |
41612 KB |
Output is correct |
42 |
Correct |
25 ms |
41564 KB |
Output is correct |
43 |
Correct |
27 ms |
41556 KB |
Output is correct |
44 |
Correct |
23 ms |
41556 KB |
Output is correct |
45 |
Correct |
22 ms |
41556 KB |
Output is correct |
46 |
Correct |
21 ms |
41556 KB |
Output is correct |
47 |
Correct |
576 ms |
57996 KB |
Output is correct |
48 |
Correct |
525 ms |
57940 KB |
Output is correct |
49 |
Correct |
139 ms |
52160 KB |
Output is correct |
50 |
Correct |
372 ms |
57560 KB |
Output is correct |
51 |
Correct |
170 ms |
51636 KB |
Output is correct |
52 |
Correct |
400 ms |
54840 KB |
Output is correct |
53 |
Correct |
67 ms |
51276 KB |
Output is correct |
54 |
Correct |
1084 ms |
58652 KB |
Output is correct |
55 |
Correct |
1077 ms |
58604 KB |
Output is correct |
56 |
Correct |
1075 ms |
58780 KB |
Output is correct |
57 |
Correct |
143 ms |
52436 KB |
Output is correct |
58 |
Correct |
734 ms |
58620 KB |
Output is correct |
59 |
Correct |
852 ms |
56420 KB |
Output is correct |
60 |
Correct |
804 ms |
56064 KB |
Output is correct |
61 |
Correct |
700 ms |
56248 KB |
Output is correct |
62 |
Correct |
150 ms |
51732 KB |
Output is correct |
63 |
Correct |
843 ms |
55348 KB |
Output is correct |
64 |
Correct |
755 ms |
52736 KB |
Output is correct |
65 |
Correct |
854 ms |
55308 KB |
Output is correct |
66 |
Correct |
22 ms |
41484 KB |
Output is correct |
67 |
Correct |
1086 ms |
58896 KB |
Output is correct |
68 |
Correct |
1047 ms |
58716 KB |
Output is correct |
69 |
Correct |
1063 ms |
58668 KB |
Output is correct |
70 |
Correct |
964 ms |
58752 KB |
Output is correct |
71 |
Correct |
921 ms |
58712 KB |
Output is correct |
72 |
Correct |
873 ms |
58828 KB |
Output is correct |
73 |
Correct |
149 ms |
52432 KB |
Output is correct |
74 |
Correct |
140 ms |
52436 KB |
Output is correct |
75 |
Correct |
717 ms |
58532 KB |
Output is correct |
76 |
Correct |
535 ms |
58780 KB |
Output is correct |
77 |
Correct |
111 ms |
52436 KB |
Output is correct |
78 |
Correct |
22 ms |
41460 KB |
Output is correct |
79 |
Correct |
26 ms |
41612 KB |
Output is correct |
80 |
Correct |
25 ms |
41620 KB |
Output is correct |
81 |
Correct |
27 ms |
41556 KB |
Output is correct |
82 |
Correct |
25 ms |
41528 KB |
Output is correct |
83 |
Correct |
22 ms |
41512 KB |
Output is correct |
84 |
Correct |
23 ms |
41504 KB |
Output is correct |
85 |
Correct |
24 ms |
41556 KB |
Output is correct |
86 |
Correct |
576 ms |
42756 KB |
Output is correct |
87 |
Correct |
491 ms |
42780 KB |
Output is correct |
88 |
Correct |
293 ms |
43060 KB |
Output is correct |
89 |
Correct |
114 ms |
42848 KB |
Output is correct |
90 |
Correct |
136 ms |
42608 KB |
Output is correct |
91 |
Correct |
49 ms |
42640 KB |
Output is correct |
92 |
Correct |
453 ms |
42396 KB |
Output is correct |
93 |
Correct |
541 ms |
58076 KB |
Output is correct |
94 |
Correct |
519 ms |
58196 KB |
Output is correct |
95 |
Correct |
136 ms |
52228 KB |
Output is correct |
96 |
Correct |
370 ms |
57676 KB |
Output is correct |
97 |
Correct |
154 ms |
51628 KB |
Output is correct |
98 |
Correct |
401 ms |
54908 KB |
Output is correct |
99 |
Correct |
68 ms |
51384 KB |
Output is correct |
100 |
Correct |
837 ms |
56512 KB |
Output is correct |
101 |
Correct |
817 ms |
56076 KB |
Output is correct |
102 |
Correct |
695 ms |
56076 KB |
Output is correct |
103 |
Correct |
822 ms |
55312 KB |
Output is correct |
104 |
Correct |
739 ms |
52764 KB |
Output is correct |
105 |
Correct |
836 ms |
55356 KB |
Output is correct |
106 |
Correct |
687 ms |
58792 KB |
Output is correct |
107 |
Correct |
925 ms |
58904 KB |
Output is correct |
108 |
Correct |
240 ms |
52960 KB |
Output is correct |
109 |
Correct |
260 ms |
52900 KB |
Output is correct |
110 |
Correct |
361 ms |
52764 KB |
Output is correct |
111 |
Correct |
350 ms |
52788 KB |
Output is correct |