#include <stdio.h>
#define N 100000
#define L 17 /* L = ceil(log2(N)) */
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int ll[N], rr[N];
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (rr[ii[j]] == rr[i_])
j++;
else if (rr[ii[j]] < rr[i_]) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
int main() {
static int ii[N], qu[N], pp[L + 1][N];
int n, q, h, i, i_, j, j_, k, l, cnt, lower, upper;
scanf("%d%d", &n, &q);
for (i = 0; i < n; i++) {
scanf("%d%d", &ll[i], &rr[i]);
ii[i] = i;
}
sort(ii, 0, n);
cnt = 0;
for (i = 0, j = 0; j < n; j++) {
j_ = ii[j];
while (i < n && rr[i_ = ii[i]] <= rr[j_]) {
while (cnt && ll[qu[cnt - 1]] >= ll[i_])
cnt--;
qu[cnt++] = i_;
i++;
}
lower = -1, upper = cnt;
while (upper - lower > 1) {
h = (lower + upper) / 2;
if (ll[j_] <= rr[qu[h]])
upper = h;
else
lower = h;
}
pp[0][j_] = qu[upper];
}
for (l = 1; l <= L; l++)
for (i = 0; i < n; i++)
pp[l][i] = pp[l - 1][pp[l - 1][i]];
while (q--) {
scanf("%d%d", &i, &j), i--, j--;
if (i == j) {
printf("0\n");
continue;
}
if (rr[i] > rr[j] || ll[pp[L][j]] > rr[i]) {
printf("impossible\n");
continue;
}
if (ll[j] <= rr[i]) {
printf("1\n");
continue;
}
k = 2;
for (l = L; l >= 0; l--)
if (ll[pp[l][j]] > rr[i])
j = pp[l][j], k += 1 << l;
printf("%d\n", k);
}
return 0;
}
Compilation message
events.c: In function 'main':
events.c:37:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | scanf("%d%d", &n, &q);
| ^~~~~~~~~~~~~~~~~~~~~
events.c:39:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | scanf("%d%d", &ll[i], &rr[i]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
events.c:66:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
94 ms |
12480 KB |
Output is correct |
3 |
Correct |
118 ms |
12536 KB |
Output is correct |
4 |
Correct |
146 ms |
12352 KB |
Output is correct |
5 |
Correct |
122 ms |
12456 KB |
Output is correct |
6 |
Correct |
129 ms |
12432 KB |
Output is correct |
7 |
Correct |
118 ms |
12532 KB |
Output is correct |
8 |
Correct |
94 ms |
12936 KB |
Output is correct |
9 |
Correct |
83 ms |
12888 KB |
Output is correct |
10 |
Correct |
108 ms |
12880 KB |
Output is correct |
11 |
Correct |
91 ms |
12716 KB |
Output is correct |
12 |
Correct |
74 ms |
11948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
516 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
444 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
516 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
444 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
448 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
39 ms |
2192 KB |
Output is correct |
20 |
Correct |
40 ms |
2264 KB |
Output is correct |
21 |
Correct |
48 ms |
2472 KB |
Output is correct |
22 |
Correct |
28 ms |
2532 KB |
Output is correct |
23 |
Correct |
27 ms |
2424 KB |
Output is correct |
24 |
Correct |
26 ms |
2480 KB |
Output is correct |
25 |
Correct |
36 ms |
2108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
516 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
444 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
476 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
60 ms |
10792 KB |
Output is correct |
20 |
Correct |
63 ms |
10008 KB |
Output is correct |
21 |
Correct |
43 ms |
10128 KB |
Output is correct |
22 |
Correct |
69 ms |
10464 KB |
Output is correct |
23 |
Correct |
64 ms |
10700 KB |
Output is correct |
24 |
Correct |
56 ms |
10776 KB |
Output is correct |
25 |
Correct |
31 ms |
9644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
12500 KB |
Output is correct |
2 |
Correct |
108 ms |
12456 KB |
Output is correct |
3 |
Correct |
150 ms |
12308 KB |
Output is correct |
4 |
Correct |
85 ms |
12744 KB |
Output is correct |
5 |
Correct |
118 ms |
12852 KB |
Output is correct |
6 |
Correct |
114 ms |
12624 KB |
Output is correct |
7 |
Correct |
128 ms |
12560 KB |
Output is correct |
8 |
Correct |
117 ms |
12832 KB |
Output is correct |
9 |
Correct |
67 ms |
10700 KB |
Output is correct |
10 |
Correct |
107 ms |
12308 KB |
Output is correct |
11 |
Correct |
97 ms |
12000 KB |
Output is correct |
12 |
Correct |
99 ms |
12196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
94 ms |
12480 KB |
Output is correct |
3 |
Correct |
118 ms |
12536 KB |
Output is correct |
4 |
Correct |
146 ms |
12352 KB |
Output is correct |
5 |
Correct |
122 ms |
12456 KB |
Output is correct |
6 |
Correct |
129 ms |
12432 KB |
Output is correct |
7 |
Correct |
118 ms |
12532 KB |
Output is correct |
8 |
Correct |
94 ms |
12936 KB |
Output is correct |
9 |
Correct |
83 ms |
12888 KB |
Output is correct |
10 |
Correct |
108 ms |
12880 KB |
Output is correct |
11 |
Correct |
91 ms |
12716 KB |
Output is correct |
12 |
Correct |
74 ms |
11948 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
516 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
444 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
448 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
39 ms |
2192 KB |
Output is correct |
32 |
Correct |
40 ms |
2264 KB |
Output is correct |
33 |
Correct |
48 ms |
2472 KB |
Output is correct |
34 |
Correct |
28 ms |
2532 KB |
Output is correct |
35 |
Correct |
27 ms |
2424 KB |
Output is correct |
36 |
Correct |
26 ms |
2480 KB |
Output is correct |
37 |
Correct |
36 ms |
2108 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
476 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
60 ms |
10792 KB |
Output is correct |
48 |
Correct |
63 ms |
10008 KB |
Output is correct |
49 |
Correct |
43 ms |
10128 KB |
Output is correct |
50 |
Correct |
69 ms |
10464 KB |
Output is correct |
51 |
Correct |
64 ms |
10700 KB |
Output is correct |
52 |
Correct |
56 ms |
10776 KB |
Output is correct |
53 |
Correct |
31 ms |
9644 KB |
Output is correct |
54 |
Correct |
95 ms |
12500 KB |
Output is correct |
55 |
Correct |
108 ms |
12456 KB |
Output is correct |
56 |
Correct |
150 ms |
12308 KB |
Output is correct |
57 |
Correct |
85 ms |
12744 KB |
Output is correct |
58 |
Correct |
118 ms |
12852 KB |
Output is correct |
59 |
Correct |
114 ms |
12624 KB |
Output is correct |
60 |
Correct |
128 ms |
12560 KB |
Output is correct |
61 |
Correct |
117 ms |
12832 KB |
Output is correct |
62 |
Correct |
67 ms |
10700 KB |
Output is correct |
63 |
Correct |
107 ms |
12308 KB |
Output is correct |
64 |
Correct |
97 ms |
12000 KB |
Output is correct |
65 |
Correct |
99 ms |
12196 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
91 ms |
12368 KB |
Output is correct |
68 |
Correct |
115 ms |
12400 KB |
Output is correct |
69 |
Correct |
138 ms |
12416 KB |
Output is correct |
70 |
Correct |
115 ms |
12496 KB |
Output is correct |
71 |
Correct |
118 ms |
12512 KB |
Output is correct |
72 |
Correct |
104 ms |
12452 KB |
Output is correct |
73 |
Correct |
81 ms |
12892 KB |
Output is correct |
74 |
Correct |
80 ms |
13012 KB |
Output is correct |
75 |
Correct |
110 ms |
12964 KB |
Output is correct |
76 |
Correct |
98 ms |
12688 KB |
Output is correct |
77 |
Correct |
55 ms |
12084 KB |
Output is correct |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
1 ms |
468 KB |
Output is correct |
81 |
Correct |
1 ms |
468 KB |
Output is correct |
82 |
Correct |
1 ms |
468 KB |
Output is correct |
83 |
Correct |
1 ms |
468 KB |
Output is correct |
84 |
Correct |
1 ms |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
40 ms |
2144 KB |
Output is correct |
87 |
Correct |
41 ms |
2244 KB |
Output is correct |
88 |
Correct |
43 ms |
2560 KB |
Output is correct |
89 |
Correct |
40 ms |
2544 KB |
Output is correct |
90 |
Correct |
27 ms |
2420 KB |
Output is correct |
91 |
Correct |
26 ms |
2452 KB |
Output is correct |
92 |
Correct |
33 ms |
2108 KB |
Output is correct |
93 |
Correct |
67 ms |
10776 KB |
Output is correct |
94 |
Correct |
61 ms |
9944 KB |
Output is correct |
95 |
Correct |
48 ms |
10188 KB |
Output is correct |
96 |
Correct |
59 ms |
10632 KB |
Output is correct |
97 |
Correct |
52 ms |
10720 KB |
Output is correct |
98 |
Correct |
57 ms |
10796 KB |
Output is correct |
99 |
Correct |
30 ms |
9736 KB |
Output is correct |
100 |
Correct |
108 ms |
12520 KB |
Output is correct |
101 |
Correct |
118 ms |
12620 KB |
Output is correct |
102 |
Correct |
105 ms |
12884 KB |
Output is correct |
103 |
Correct |
103 ms |
12196 KB |
Output is correct |
104 |
Correct |
98 ms |
11880 KB |
Output is correct |
105 |
Correct |
143 ms |
12296 KB |
Output is correct |
106 |
Correct |
99 ms |
11848 KB |
Output is correct |
107 |
Correct |
125 ms |
11960 KB |
Output is correct |
108 |
Correct |
73 ms |
12192 KB |
Output is correct |
109 |
Correct |
84 ms |
12128 KB |
Output is correct |
110 |
Correct |
91 ms |
12220 KB |
Output is correct |
111 |
Correct |
81 ms |
12152 KB |
Output is correct |