//InTheNameOfGOD
//PRS;)
#pragma GCC optimize("unroll-loops", "Ofast", "O3")
#include<iostream>
#include<algorithm>
#define rep(i, l, r) for(ll i = l; i < r; i++)
#define min(x, y) (x < y ? x : y)
#define max(x, y) (x > y ? x : y)
#define X first
#define Y second
typedef int ll;
using namespace std;
typedef pair<ll, ll> pl;
typedef pair<ll, pl> pi;
constexpr ll inf = 2e9, xn = 1e5 + 5, xs = 500, s = 200;
ll d[xn], p[xn], sx[xn], x[xn], m[xs];
pi e[xn];
int main()
{
ll n, q;
scanf("%d %d", &n, &q);
rep(i, 0, n) scanf("%d %d", &e[i].Y.X, &e[i].X), e[i] = {-e[i].X, {-e[i].Y.X, i}};
sort(e, e + n);
rep(i, 0, xs) m[i] = -inf;
rep(i, 0, n)
{
sx[i] = x[i] = -1;
rep(j, 0, i / s + 1) if(e[i].X <= m[j])
{
rep(k, j * s, min((j + 1) * s, i)) if(e[i].X <= e[k].Y.X)
{
if(k / s < i / s) sx[i] = k, d[i] = 1;
else if(sx[k] >= 0) sx[i] = sx[k], d[i] = d[k] + 1;
x[i] = k;
break;
}
break;
}
m[i / s] = max(e[i].Y.X, m[i / s]), p[e[i].Y.Y] = i;
}
while(q--)
{
ll l, r, ans = 0;
scanf("%d %d", &l, &r);
if(l == r)
{
printf("0\n");
continue;
}
l = p[l - 1], r = p[r - 1];
if(e[l].X < e[r].X)
{
printf("impossible\n");
continue;
}
if(e[l].X == e[r].X)
{
printf("1\n");
continue;
}
while(sx[l] >= r) ans += d[l], l = sx[l];
while(x[l] >= r) l = x[l], ans++;
if(r == l)
{
printf("%d\n", ans);
continue;
}
if(x[l] < 0)
{
printf("impossible\n");
continue;
}
ll z = e[r].Y.X, mx = e[r].Y.X;
bool f = 1;
rep(i, r + 1, l + 1)
{
if(z < e[i].X && mx < e[i].X)
{
f = 0;
break;
}
if(z < e[i].X) z = mx, ans++;
mx = max(mx, e[i].Y.X);
}
if(f) printf("%d\n", ans + 1);
else printf("impossible\n");
}
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:21:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | scanf("%d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~
events.cpp:22:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | rep(i, 0, n) scanf("%d %d", &e[i].Y.X, &e[i].X), e[i] = {-e[i].X, {-e[i].Y.X, i}};
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
events.cpp:44:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%d %d", &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
282 ms |
3636 KB |
Output is correct |
3 |
Correct |
239 ms |
3540 KB |
Output is correct |
4 |
Correct |
177 ms |
3572 KB |
Output is correct |
5 |
Correct |
276 ms |
3632 KB |
Output is correct |
6 |
Correct |
302 ms |
3680 KB |
Output is correct |
7 |
Correct |
303 ms |
3724 KB |
Output is correct |
8 |
Correct |
51 ms |
4044 KB |
Output is correct |
9 |
Correct |
61 ms |
3660 KB |
Output is correct |
10 |
Correct |
96 ms |
3916 KB |
Output is correct |
11 |
Correct |
109 ms |
4020 KB |
Output is correct |
12 |
Correct |
52 ms |
3724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
377 ms |
860 KB |
Output is correct |
20 |
Correct |
61 ms |
888 KB |
Output is correct |
21 |
Correct |
113 ms |
1096 KB |
Output is correct |
22 |
Correct |
88 ms |
1144 KB |
Output is correct |
23 |
Correct |
111 ms |
1036 KB |
Output is correct |
24 |
Correct |
119 ms |
1004 KB |
Output is correct |
25 |
Correct |
37 ms |
628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
42 ms |
3008 KB |
Output is correct |
20 |
Correct |
27 ms |
3012 KB |
Output is correct |
21 |
Correct |
36 ms |
2992 KB |
Output is correct |
22 |
Correct |
42 ms |
3012 KB |
Output is correct |
23 |
Correct |
31 ms |
3000 KB |
Output is correct |
24 |
Correct |
46 ms |
2928 KB |
Output is correct |
25 |
Correct |
21 ms |
3028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
273 ms |
3536 KB |
Output is correct |
2 |
Correct |
240 ms |
3608 KB |
Output is correct |
3 |
Correct |
166 ms |
3504 KB |
Output is correct |
4 |
Correct |
62 ms |
3744 KB |
Output is correct |
5 |
Correct |
99 ms |
3956 KB |
Output is correct |
6 |
Correct |
101 ms |
3660 KB |
Output is correct |
7 |
Correct |
102 ms |
3748 KB |
Output is correct |
8 |
Correct |
114 ms |
3900 KB |
Output is correct |
9 |
Correct |
38 ms |
2932 KB |
Output is correct |
10 |
Correct |
172 ms |
3340 KB |
Output is correct |
11 |
Execution timed out |
1580 ms |
3360 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
282 ms |
3636 KB |
Output is correct |
3 |
Correct |
239 ms |
3540 KB |
Output is correct |
4 |
Correct |
177 ms |
3572 KB |
Output is correct |
5 |
Correct |
276 ms |
3632 KB |
Output is correct |
6 |
Correct |
302 ms |
3680 KB |
Output is correct |
7 |
Correct |
303 ms |
3724 KB |
Output is correct |
8 |
Correct |
51 ms |
4044 KB |
Output is correct |
9 |
Correct |
61 ms |
3660 KB |
Output is correct |
10 |
Correct |
96 ms |
3916 KB |
Output is correct |
11 |
Correct |
109 ms |
4020 KB |
Output is correct |
12 |
Correct |
52 ms |
3724 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
377 ms |
860 KB |
Output is correct |
32 |
Correct |
61 ms |
888 KB |
Output is correct |
33 |
Correct |
113 ms |
1096 KB |
Output is correct |
34 |
Correct |
88 ms |
1144 KB |
Output is correct |
35 |
Correct |
111 ms |
1036 KB |
Output is correct |
36 |
Correct |
119 ms |
1004 KB |
Output is correct |
37 |
Correct |
37 ms |
628 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
42 ms |
3008 KB |
Output is correct |
48 |
Correct |
27 ms |
3012 KB |
Output is correct |
49 |
Correct |
36 ms |
2992 KB |
Output is correct |
50 |
Correct |
42 ms |
3012 KB |
Output is correct |
51 |
Correct |
31 ms |
3000 KB |
Output is correct |
52 |
Correct |
46 ms |
2928 KB |
Output is correct |
53 |
Correct |
21 ms |
3028 KB |
Output is correct |
54 |
Correct |
273 ms |
3536 KB |
Output is correct |
55 |
Correct |
240 ms |
3608 KB |
Output is correct |
56 |
Correct |
166 ms |
3504 KB |
Output is correct |
57 |
Correct |
62 ms |
3744 KB |
Output is correct |
58 |
Correct |
99 ms |
3956 KB |
Output is correct |
59 |
Correct |
101 ms |
3660 KB |
Output is correct |
60 |
Correct |
102 ms |
3748 KB |
Output is correct |
61 |
Correct |
114 ms |
3900 KB |
Output is correct |
62 |
Correct |
38 ms |
2932 KB |
Output is correct |
63 |
Correct |
172 ms |
3340 KB |
Output is correct |
64 |
Execution timed out |
1580 ms |
3360 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |