#include <bits/stdc++.h>
using namespace std;
char *p1, *p2, buf[1<<20];
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
inline int read() {int x=0;char c=gc();while(!isdigit(c))c=gc();while(isdigit(c))x=x*10+c-'0',c=gc();return x;}
#define pc(c) putchar(c)
void write(int x) {if(x>9)write(x/10);pc('0'+x%10);}
int n, q;
int mp[100010];
struct Int {
int l, r, id;
} a[100010];
int ST[100010][20], fa[100010][20], lg2[100010];
inline int get(int x, int y) {
return a[x].l <= a[y].l? x : y;
}
inline int query(int l, int r) {
int d = r - l + 1;
return get(ST[l][lg2[d]], ST[r - (1 << lg2[d]) + 1][lg2[d]]);
}
int main() {
n = read(), q = read();
for (int i = 1; i <= n; i++) {
a[i].l = read(), a[i].r = read(), a[i].id = i;
}
sort(a + 1, a + 1 + n, [](Int &a, Int &b){return a.r < b.r || (a.r == b.r && a.l < b.l);});
lg2[0] = -1;
for (int i = 1; i <= n; i++) {
mp[a[i].id] = i;
ST[i][0] = i;
lg2[i] = lg2[i - 1] + (i == (i & -i));
}
for (int i = 1; i < 20; i++) {
for (int j = 1; j + (1 << i - 1) <= n; j++) {
ST[j][i] = get(ST[j][i - 1], ST[j + (1 << i - 1)][i - 1]);
}
}
for (int i = 1; i <= n; i++) {
int L = 1, R = i;
while (L < R) {
int M = L + R >> 1;
if (a[i].l <= a[M].r) R = M;
else L = M + 1;
}
if (L != i) fa[i][0] = query(L, i - 1);
else fa[i][0] = 0;
// cerr << a[i].l << ' ' << a[i].r << ' ' << fa[i][0] << endl;
}
for (int i = 1; i < 20; i++) {
for (int j = 1; j <= n; j++) {
fa[j][i] = fa[fa[j][i - 1]][i - 1];
}
}
a[0].l = -1;
while (q--) {
int s = read(), t = read();
s = mp[s], t = mp[t];
if (s == t) {
puts("0");
continue;
}
if (a[s].r > a[t].r) {
puts("impossible");
continue;
}
if (a[t].l <= a[s].r) {
puts("1");
continue;
}
int ans = 0;
for (int i = 19; i >= 0; i--) {
if (a[fa[t][i]].l > a[s].r) t = fa[t][i], ans += 1 << i;
}
t = fa[t][0];
if (!t) {
puts("impossible");
continue;
}
write(ans + 2); pc('\n');
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:39:37: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
39 | for (int j = 1; j + (1 << i - 1) <= n; j++) {
| ~~^~~
events.cpp:40:57: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
40 | ST[j][i] = get(ST[j][i - 1], ST[j + (1 << i - 1)][i - 1]);
| ~~^~~
events.cpp:46:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
46 | int M = L + R >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
27 ms |
22712 KB |
Output is correct |
3 |
Correct |
28 ms |
22608 KB |
Output is correct |
4 |
Correct |
34 ms |
22616 KB |
Output is correct |
5 |
Correct |
30 ms |
22608 KB |
Output is correct |
6 |
Correct |
31 ms |
22612 KB |
Output is correct |
7 |
Correct |
30 ms |
22864 KB |
Output is correct |
8 |
Correct |
25 ms |
23120 KB |
Output is correct |
9 |
Correct |
26 ms |
23136 KB |
Output is correct |
10 |
Correct |
43 ms |
23120 KB |
Output is correct |
11 |
Correct |
35 ms |
23124 KB |
Output is correct |
12 |
Correct |
20 ms |
22352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
9 ms |
11100 KB |
Output is correct |
20 |
Correct |
8 ms |
11096 KB |
Output is correct |
21 |
Correct |
12 ms |
11412 KB |
Output is correct |
22 |
Correct |
8 ms |
11356 KB |
Output is correct |
23 |
Correct |
7 ms |
11356 KB |
Output is correct |
24 |
Correct |
7 ms |
11356 KB |
Output is correct |
25 |
Correct |
7 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6644 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
20 ms |
20828 KB |
Output is correct |
20 |
Correct |
19 ms |
20224 KB |
Output is correct |
21 |
Correct |
24 ms |
20852 KB |
Output is correct |
22 |
Correct |
20 ms |
21028 KB |
Output is correct |
23 |
Correct |
23 ms |
20744 KB |
Output is correct |
24 |
Correct |
21 ms |
20824 KB |
Output is correct |
25 |
Correct |
14 ms |
20316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
22644 KB |
Output is correct |
2 |
Correct |
33 ms |
22652 KB |
Output is correct |
3 |
Correct |
34 ms |
22612 KB |
Output is correct |
4 |
Correct |
28 ms |
23092 KB |
Output is correct |
5 |
Correct |
41 ms |
23124 KB |
Output is correct |
6 |
Correct |
38 ms |
22864 KB |
Output is correct |
7 |
Correct |
43 ms |
22728 KB |
Output is correct |
8 |
Correct |
35 ms |
22872 KB |
Output is correct |
9 |
Correct |
20 ms |
20816 KB |
Output is correct |
10 |
Correct |
26 ms |
22356 KB |
Output is correct |
11 |
Correct |
28 ms |
22096 KB |
Output is correct |
12 |
Correct |
28 ms |
22356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
27 ms |
22712 KB |
Output is correct |
3 |
Correct |
28 ms |
22608 KB |
Output is correct |
4 |
Correct |
34 ms |
22616 KB |
Output is correct |
5 |
Correct |
30 ms |
22608 KB |
Output is correct |
6 |
Correct |
31 ms |
22612 KB |
Output is correct |
7 |
Correct |
30 ms |
22864 KB |
Output is correct |
8 |
Correct |
25 ms |
23120 KB |
Output is correct |
9 |
Correct |
26 ms |
23136 KB |
Output is correct |
10 |
Correct |
43 ms |
23120 KB |
Output is correct |
11 |
Correct |
35 ms |
23124 KB |
Output is correct |
12 |
Correct |
20 ms |
22352 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6488 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
9 ms |
11100 KB |
Output is correct |
32 |
Correct |
8 ms |
11096 KB |
Output is correct |
33 |
Correct |
12 ms |
11412 KB |
Output is correct |
34 |
Correct |
8 ms |
11356 KB |
Output is correct |
35 |
Correct |
7 ms |
11356 KB |
Output is correct |
36 |
Correct |
7 ms |
11356 KB |
Output is correct |
37 |
Correct |
7 ms |
10844 KB |
Output is correct |
38 |
Correct |
1 ms |
6492 KB |
Output is correct |
39 |
Correct |
1 ms |
6492 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
1 ms |
6492 KB |
Output is correct |
42 |
Correct |
1 ms |
6492 KB |
Output is correct |
43 |
Correct |
1 ms |
6492 KB |
Output is correct |
44 |
Correct |
1 ms |
6644 KB |
Output is correct |
45 |
Correct |
1 ms |
6492 KB |
Output is correct |
46 |
Correct |
1 ms |
6492 KB |
Output is correct |
47 |
Correct |
20 ms |
20828 KB |
Output is correct |
48 |
Correct |
19 ms |
20224 KB |
Output is correct |
49 |
Correct |
24 ms |
20852 KB |
Output is correct |
50 |
Correct |
20 ms |
21028 KB |
Output is correct |
51 |
Correct |
23 ms |
20744 KB |
Output is correct |
52 |
Correct |
21 ms |
20824 KB |
Output is correct |
53 |
Correct |
14 ms |
20316 KB |
Output is correct |
54 |
Correct |
27 ms |
22644 KB |
Output is correct |
55 |
Correct |
33 ms |
22652 KB |
Output is correct |
56 |
Correct |
34 ms |
22612 KB |
Output is correct |
57 |
Correct |
28 ms |
23092 KB |
Output is correct |
58 |
Correct |
41 ms |
23124 KB |
Output is correct |
59 |
Correct |
38 ms |
22864 KB |
Output is correct |
60 |
Correct |
43 ms |
22728 KB |
Output is correct |
61 |
Correct |
35 ms |
22872 KB |
Output is correct |
62 |
Correct |
20 ms |
20816 KB |
Output is correct |
63 |
Correct |
26 ms |
22356 KB |
Output is correct |
64 |
Correct |
28 ms |
22096 KB |
Output is correct |
65 |
Correct |
28 ms |
22356 KB |
Output is correct |
66 |
Correct |
1 ms |
6588 KB |
Output is correct |
67 |
Correct |
27 ms |
22620 KB |
Output is correct |
68 |
Correct |
29 ms |
22548 KB |
Output is correct |
69 |
Correct |
33 ms |
22580 KB |
Output is correct |
70 |
Correct |
31 ms |
22608 KB |
Output is correct |
71 |
Correct |
31 ms |
22612 KB |
Output is correct |
72 |
Correct |
31 ms |
22868 KB |
Output is correct |
73 |
Correct |
27 ms |
23120 KB |
Output is correct |
74 |
Correct |
27 ms |
23120 KB |
Output is correct |
75 |
Correct |
42 ms |
23120 KB |
Output is correct |
76 |
Correct |
39 ms |
23088 KB |
Output is correct |
77 |
Correct |
19 ms |
22360 KB |
Output is correct |
78 |
Correct |
1 ms |
6492 KB |
Output is correct |
79 |
Correct |
1 ms |
6492 KB |
Output is correct |
80 |
Correct |
1 ms |
6492 KB |
Output is correct |
81 |
Correct |
1 ms |
6492 KB |
Output is correct |
82 |
Correct |
1 ms |
6492 KB |
Output is correct |
83 |
Correct |
1 ms |
6492 KB |
Output is correct |
84 |
Correct |
1 ms |
6556 KB |
Output is correct |
85 |
Correct |
1 ms |
6492 KB |
Output is correct |
86 |
Correct |
9 ms |
11096 KB |
Output is correct |
87 |
Correct |
8 ms |
11100 KB |
Output is correct |
88 |
Correct |
14 ms |
11356 KB |
Output is correct |
89 |
Correct |
8 ms |
11356 KB |
Output is correct |
90 |
Correct |
8 ms |
11356 KB |
Output is correct |
91 |
Correct |
7 ms |
11356 KB |
Output is correct |
92 |
Correct |
6 ms |
10844 KB |
Output is correct |
93 |
Correct |
20 ms |
20824 KB |
Output is correct |
94 |
Correct |
20 ms |
20060 KB |
Output is correct |
95 |
Correct |
24 ms |
20924 KB |
Output is correct |
96 |
Correct |
23 ms |
20820 KB |
Output is correct |
97 |
Correct |
20 ms |
20828 KB |
Output is correct |
98 |
Correct |
21 ms |
20828 KB |
Output is correct |
99 |
Correct |
14 ms |
20316 KB |
Output is correct |
100 |
Correct |
44 ms |
22900 KB |
Output is correct |
101 |
Correct |
37 ms |
22864 KB |
Output is correct |
102 |
Correct |
34 ms |
22868 KB |
Output is correct |
103 |
Correct |
27 ms |
22356 KB |
Output is correct |
104 |
Correct |
26 ms |
22100 KB |
Output is correct |
105 |
Correct |
26 ms |
22356 KB |
Output is correct |
106 |
Correct |
37 ms |
22100 KB |
Output is correct |
107 |
Correct |
45 ms |
22100 KB |
Output is correct |
108 |
Correct |
34 ms |
22716 KB |
Output is correct |
109 |
Correct |
33 ms |
22868 KB |
Output is correct |
110 |
Correct |
36 ms |
22608 KB |
Output is correct |
111 |
Correct |
38 ms |
22608 KB |
Output is correct |