#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=1e5;
int n, q, lg[2*mxN+1], st[2*mxN][18], lift[mxN][17];
ar<int, 2> events[mxN];
vector<int> d;
int cmb(int a, int b) {
if (a==-1||b==-1)
return a==-1?b:a;
return events[a][0]<events[b][0]?a:b;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
for (int i=0; i<n; ++i) {
cin >> events[i][0] >> events[i][1];
d.push_back(events[i][0]);
d.push_back(events[i][1]);
}
sort(d.begin(), d.end());
d.resize(unique(d.begin(), d.end())-d.begin());
memset(st, -1, sizeof(st));
for (int i=0; i<n; ++i) {
events[i][0]=lower_bound(d.begin(), d.end(), events[i][0])-d.begin();
events[i][1]=lower_bound(d.begin(), d.end(), events[i][1])-d.begin();
st[events[i][1]][0]=cmb(st[events[i][1]][0], i);
}
for (int i=2; i<=d.size(); ++i)
lg[i]=lg[i/2]+1;
for (int j=1; (1<<j)<=d.size(); ++j)
for (int i=0; i+(1<<j)-1<d.size(); ++i)
st[i][j]=cmb(st[i][j-1], st[i+(1<<j-1)][j-1]);
for (int i=0; i<n; ++i) {
int k=lg[events[i][1]-events[i][0]+1];
lift[i][0]=cmb(st[events[i][0]][k], st[events[i][1]-(1<<k)+1][k]);
assert(lift[i][0]!=-1);
}
for (int j=1; j<17; ++j)
for (int i=0; i<n; ++i)
lift[i][j]=lift[lift[i][j-1]][j-1];
while(q--) {
int a, b;
cin >> a >> b, --a, --b;
if (events[a][1]>events[b][1]) {
cout << "impossible\n";
continue;
}
if (events[b][0]<=events[a][1]&&events[a][1]<=events[b][1]) {
cout << (a!=b) << "\n";
continue;
}
int ans=0;
for (int i=16; ~i; --i)
if (events[lift[b][i]][0]>events[a][1])
b=lift[b][i], ans+=1<<i;
if (events[lift[b][0]][0]>events[a][1]) {
cout << "impossible\n";
continue;
}
cout << ans+2 << "\n";
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:35:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for (int i=2; i<=d.size(); ++i)
| ~^~~~~~~~~~
events.cpp:37:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for (int j=1; (1<<j)<=d.size(); ++j)
| ~~~~~~^~~~~~~~~~
events.cpp:38:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for (int i=0; i+(1<<j)-1<d.size(); ++i)
| ~~~~~~~~~~^~~~~~~~~
events.cpp:39:39: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
39 | st[i][j]=cmb(st[i][j-1], st[i+(1<<j-1)][j-1]);
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
133 ms |
23680 KB |
Output is correct |
3 |
Correct |
113 ms |
23708 KB |
Output is correct |
4 |
Correct |
157 ms |
23828 KB |
Output is correct |
5 |
Correct |
141 ms |
24144 KB |
Output is correct |
6 |
Correct |
141 ms |
24532 KB |
Output is correct |
7 |
Correct |
136 ms |
24212 KB |
Output is correct |
8 |
Correct |
152 ms |
25260 KB |
Output is correct |
9 |
Correct |
129 ms |
25276 KB |
Output is correct |
10 |
Correct |
156 ms |
24720 KB |
Output is correct |
11 |
Correct |
134 ms |
24836 KB |
Output is correct |
12 |
Correct |
78 ms |
24460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
14364 KB |
Output is correct |
2 |
Correct |
6 ms |
14356 KB |
Output is correct |
3 |
Correct |
6 ms |
14420 KB |
Output is correct |
4 |
Correct |
6 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
9 ms |
14408 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
9 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
14364 KB |
Output is correct |
2 |
Correct |
6 ms |
14356 KB |
Output is correct |
3 |
Correct |
6 ms |
14420 KB |
Output is correct |
4 |
Correct |
6 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
9 ms |
14408 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
9 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14332 KB |
Output is correct |
12 |
Correct |
6 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14536 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14492 KB |
Output is correct |
17 |
Correct |
6 ms |
14420 KB |
Output is correct |
18 |
Correct |
6 ms |
14484 KB |
Output is correct |
19 |
Correct |
39 ms |
15376 KB |
Output is correct |
20 |
Correct |
36 ms |
15316 KB |
Output is correct |
21 |
Correct |
46 ms |
15696 KB |
Output is correct |
22 |
Correct |
30 ms |
15692 KB |
Output is correct |
23 |
Correct |
32 ms |
15580 KB |
Output is correct |
24 |
Correct |
29 ms |
15504 KB |
Output is correct |
25 |
Correct |
31 ms |
15168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
14364 KB |
Output is correct |
2 |
Correct |
6 ms |
14356 KB |
Output is correct |
3 |
Correct |
6 ms |
14420 KB |
Output is correct |
4 |
Correct |
6 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
9 ms |
14408 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
9 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
6 ms |
14368 KB |
Output is correct |
11 |
Correct |
6 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
6 ms |
14532 KB |
Output is correct |
15 |
Correct |
8 ms |
14408 KB |
Output is correct |
16 |
Correct |
8 ms |
14464 KB |
Output is correct |
17 |
Correct |
7 ms |
14500 KB |
Output is correct |
18 |
Correct |
6 ms |
14420 KB |
Output is correct |
19 |
Correct |
83 ms |
23084 KB |
Output is correct |
20 |
Correct |
81 ms |
23036 KB |
Output is correct |
21 |
Correct |
93 ms |
23164 KB |
Output is correct |
22 |
Correct |
104 ms |
23292 KB |
Output is correct |
23 |
Correct |
101 ms |
23432 KB |
Output is correct |
24 |
Correct |
117 ms |
23420 KB |
Output is correct |
25 |
Correct |
38 ms |
22688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
129 ms |
23832 KB |
Output is correct |
2 |
Correct |
120 ms |
23668 KB |
Output is correct |
3 |
Correct |
156 ms |
23676 KB |
Output is correct |
4 |
Correct |
131 ms |
24572 KB |
Output is correct |
5 |
Correct |
123 ms |
24024 KB |
Output is correct |
6 |
Correct |
181 ms |
24196 KB |
Output is correct |
7 |
Correct |
156 ms |
24264 KB |
Output is correct |
8 |
Correct |
137 ms |
24324 KB |
Output is correct |
9 |
Correct |
114 ms |
23496 KB |
Output is correct |
10 |
Correct |
146 ms |
23864 KB |
Output is correct |
11 |
Correct |
149 ms |
23740 KB |
Output is correct |
12 |
Correct |
137 ms |
23876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
133 ms |
23680 KB |
Output is correct |
3 |
Correct |
113 ms |
23708 KB |
Output is correct |
4 |
Correct |
157 ms |
23828 KB |
Output is correct |
5 |
Correct |
141 ms |
24144 KB |
Output is correct |
6 |
Correct |
141 ms |
24532 KB |
Output is correct |
7 |
Correct |
136 ms |
24212 KB |
Output is correct |
8 |
Correct |
152 ms |
25260 KB |
Output is correct |
9 |
Correct |
129 ms |
25276 KB |
Output is correct |
10 |
Correct |
156 ms |
24720 KB |
Output is correct |
11 |
Correct |
134 ms |
24836 KB |
Output is correct |
12 |
Correct |
78 ms |
24460 KB |
Output is correct |
13 |
Correct |
6 ms |
14364 KB |
Output is correct |
14 |
Correct |
6 ms |
14356 KB |
Output is correct |
15 |
Correct |
6 ms |
14420 KB |
Output is correct |
16 |
Correct |
6 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
9 ms |
14408 KB |
Output is correct |
19 |
Correct |
7 ms |
14420 KB |
Output is correct |
20 |
Correct |
9 ms |
14420 KB |
Output is correct |
21 |
Correct |
8 ms |
14420 KB |
Output is correct |
22 |
Correct |
8 ms |
14420 KB |
Output is correct |
23 |
Correct |
7 ms |
14332 KB |
Output is correct |
24 |
Correct |
6 ms |
14420 KB |
Output is correct |
25 |
Correct |
6 ms |
14536 KB |
Output is correct |
26 |
Correct |
8 ms |
14420 KB |
Output is correct |
27 |
Correct |
7 ms |
14420 KB |
Output is correct |
28 |
Correct |
8 ms |
14492 KB |
Output is correct |
29 |
Correct |
6 ms |
14420 KB |
Output is correct |
30 |
Correct |
6 ms |
14484 KB |
Output is correct |
31 |
Correct |
39 ms |
15376 KB |
Output is correct |
32 |
Correct |
36 ms |
15316 KB |
Output is correct |
33 |
Correct |
46 ms |
15696 KB |
Output is correct |
34 |
Correct |
30 ms |
15692 KB |
Output is correct |
35 |
Correct |
32 ms |
15580 KB |
Output is correct |
36 |
Correct |
29 ms |
15504 KB |
Output is correct |
37 |
Correct |
31 ms |
15168 KB |
Output is correct |
38 |
Correct |
6 ms |
14368 KB |
Output is correct |
39 |
Correct |
6 ms |
14420 KB |
Output is correct |
40 |
Correct |
7 ms |
14420 KB |
Output is correct |
41 |
Correct |
8 ms |
14420 KB |
Output is correct |
42 |
Correct |
6 ms |
14532 KB |
Output is correct |
43 |
Correct |
8 ms |
14408 KB |
Output is correct |
44 |
Correct |
8 ms |
14464 KB |
Output is correct |
45 |
Correct |
7 ms |
14500 KB |
Output is correct |
46 |
Correct |
6 ms |
14420 KB |
Output is correct |
47 |
Correct |
83 ms |
23084 KB |
Output is correct |
48 |
Correct |
81 ms |
23036 KB |
Output is correct |
49 |
Correct |
93 ms |
23164 KB |
Output is correct |
50 |
Correct |
104 ms |
23292 KB |
Output is correct |
51 |
Correct |
101 ms |
23432 KB |
Output is correct |
52 |
Correct |
117 ms |
23420 KB |
Output is correct |
53 |
Correct |
38 ms |
22688 KB |
Output is correct |
54 |
Correct |
129 ms |
23832 KB |
Output is correct |
55 |
Correct |
120 ms |
23668 KB |
Output is correct |
56 |
Correct |
156 ms |
23676 KB |
Output is correct |
57 |
Correct |
131 ms |
24572 KB |
Output is correct |
58 |
Correct |
123 ms |
24024 KB |
Output is correct |
59 |
Correct |
181 ms |
24196 KB |
Output is correct |
60 |
Correct |
156 ms |
24264 KB |
Output is correct |
61 |
Correct |
137 ms |
24324 KB |
Output is correct |
62 |
Correct |
114 ms |
23496 KB |
Output is correct |
63 |
Correct |
146 ms |
23864 KB |
Output is correct |
64 |
Correct |
149 ms |
23740 KB |
Output is correct |
65 |
Correct |
137 ms |
23876 KB |
Output is correct |
66 |
Correct |
7 ms |
14420 KB |
Output is correct |
67 |
Correct |
122 ms |
23668 KB |
Output is correct |
68 |
Correct |
135 ms |
23752 KB |
Output is correct |
69 |
Correct |
153 ms |
23640 KB |
Output is correct |
70 |
Correct |
120 ms |
23748 KB |
Output is correct |
71 |
Correct |
154 ms |
23800 KB |
Output is correct |
72 |
Correct |
131 ms |
23864 KB |
Output is correct |
73 |
Correct |
119 ms |
24644 KB |
Output is correct |
74 |
Correct |
155 ms |
24560 KB |
Output is correct |
75 |
Correct |
122 ms |
24108 KB |
Output is correct |
76 |
Correct |
125 ms |
24320 KB |
Output is correct |
77 |
Correct |
88 ms |
24180 KB |
Output is correct |
78 |
Correct |
6 ms |
14420 KB |
Output is correct |
79 |
Correct |
8 ms |
14548 KB |
Output is correct |
80 |
Correct |
8 ms |
14420 KB |
Output is correct |
81 |
Correct |
7 ms |
14420 KB |
Output is correct |
82 |
Correct |
8 ms |
14420 KB |
Output is correct |
83 |
Correct |
6 ms |
14420 KB |
Output is correct |
84 |
Correct |
6 ms |
14420 KB |
Output is correct |
85 |
Correct |
6 ms |
14420 KB |
Output is correct |
86 |
Correct |
39 ms |
15364 KB |
Output is correct |
87 |
Correct |
34 ms |
15316 KB |
Output is correct |
88 |
Correct |
52 ms |
15724 KB |
Output is correct |
89 |
Correct |
39 ms |
15720 KB |
Output is correct |
90 |
Correct |
33 ms |
15596 KB |
Output is correct |
91 |
Correct |
30 ms |
15528 KB |
Output is correct |
92 |
Correct |
33 ms |
15204 KB |
Output is correct |
93 |
Correct |
95 ms |
23108 KB |
Output is correct |
94 |
Correct |
107 ms |
23168 KB |
Output is correct |
95 |
Correct |
93 ms |
23108 KB |
Output is correct |
96 |
Correct |
87 ms |
23288 KB |
Output is correct |
97 |
Correct |
104 ms |
23464 KB |
Output is correct |
98 |
Correct |
101 ms |
23428 KB |
Output is correct |
99 |
Correct |
36 ms |
22736 KB |
Output is correct |
100 |
Correct |
174 ms |
24176 KB |
Output is correct |
101 |
Correct |
191 ms |
24300 KB |
Output is correct |
102 |
Correct |
162 ms |
24324 KB |
Output is correct |
103 |
Correct |
137 ms |
23924 KB |
Output is correct |
104 |
Correct |
137 ms |
23664 KB |
Output is correct |
105 |
Correct |
173 ms |
23948 KB |
Output is correct |
106 |
Correct |
118 ms |
23776 KB |
Output is correct |
107 |
Correct |
132 ms |
24004 KB |
Output is correct |
108 |
Correct |
124 ms |
23932 KB |
Output is correct |
109 |
Correct |
120 ms |
23948 KB |
Output is correct |
110 |
Correct |
121 ms |
24172 KB |
Output is correct |
111 |
Correct |
128 ms |
24180 KB |
Output is correct |