#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
int n,q;
pii s[1<<20];
pii pos[200001],com_pos[200001];
int jump[200001][20];
map<int,int> com;
void update(int l, int r, int idx, int x, pii node) {
if (x>r || x<l) return;
if (l==r) s[idx]=min(s[idx],node);
else {
int mid=(l+r)/2;
update(l,mid,2*idx,x,node);
update(mid+1,r,2*idx+1,x,node);
s[idx]=min(s[2*idx],s[2*idx+1]);
}
}
pii query(int l, int r, int idx, int x, int y) {
if (x>r || y<l) return pii(INT_MAX,INT_MAX);
if (x<=l && r<=y) return s[idx];
int mid=(l+r)/2;
return min(query(l,mid,2*idx,x,y),query(mid+1,r,2*idx+1,x,y));
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>q;
for (int i=0; i<1<<20; ++i) s[i]=pii(INT_MAX,INT_MAX);
for (int i=1; i<=n; ++i) {
int st,ed; cin>>st>>ed;
pos[i].first=st;
pos[i].second=ed;
com[st]=1;
com[ed]=1;
}
int cnt=1;
for (auto it=com.begin(); it!=com.end(); ++it) (*it).second=cnt++;
for (int i=1; i<=n; ++i) {
com_pos[i]=pii(com[pos[i].first],com[pos[i].second]);
update(1,2*n,1,com_pos[i].second,pii(com_pos[i].first,i));
}
for (int i=1; i<=n; ++i) jump[i][0]=query(1,2*n,1,com_pos[i].first,com_pos[i].second).second;
for (int i=1; i<18; ++i) {
for (int j=1; j<=n; ++j) {
jump[j][i]=jump[jump[j][i-1]][i-1];
}
}
while (q--) {
int st,ed,cnt=1;
cin>>st>>ed;
if (com_pos[st].second>com_pos[ed].second) cout<<"impossible\n";
else if (st==ed) cout<<"0\n";
else {
if (com_pos[ed].first>com_pos[st].second) {
for (int i=17; i>=0; --i) {
if (com_pos[jump[ed][i]].first>com_pos[st].second) {
ed=jump[ed][i];
cnt+=(1<<i);
}
}
if (com_pos[jump[ed][0]].first<=com_pos[st].second) cout<<cnt+1<<"\n";
else cout<<"impossible\n";
} else cout<<"1\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
221 ms |
23212 KB |
Output is correct |
3 |
Correct |
197 ms |
26228 KB |
Output is correct |
4 |
Correct |
254 ms |
26244 KB |
Output is correct |
5 |
Correct |
256 ms |
26840 KB |
Output is correct |
6 |
Correct |
195 ms |
27212 KB |
Output is correct |
7 |
Correct |
225 ms |
27484 KB |
Output is correct |
8 |
Correct |
231 ms |
31512 KB |
Output is correct |
9 |
Correct |
218 ms |
31392 KB |
Output is correct |
10 |
Correct |
212 ms |
26672 KB |
Output is correct |
11 |
Correct |
201 ms |
28492 KB |
Output is correct |
12 |
Correct |
126 ms |
26064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8532 KB |
Output is correct |
3 |
Correct |
5 ms |
8660 KB |
Output is correct |
4 |
Correct |
5 ms |
8660 KB |
Output is correct |
5 |
Correct |
6 ms |
8660 KB |
Output is correct |
6 |
Correct |
6 ms |
8660 KB |
Output is correct |
7 |
Correct |
7 ms |
8660 KB |
Output is correct |
8 |
Correct |
7 ms |
8660 KB |
Output is correct |
9 |
Correct |
4 ms |
8592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8532 KB |
Output is correct |
3 |
Correct |
5 ms |
8660 KB |
Output is correct |
4 |
Correct |
5 ms |
8660 KB |
Output is correct |
5 |
Correct |
6 ms |
8660 KB |
Output is correct |
6 |
Correct |
6 ms |
8660 KB |
Output is correct |
7 |
Correct |
7 ms |
8660 KB |
Output is correct |
8 |
Correct |
7 ms |
8660 KB |
Output is correct |
9 |
Correct |
4 ms |
8592 KB |
Output is correct |
10 |
Correct |
4 ms |
8524 KB |
Output is correct |
11 |
Correct |
4 ms |
8520 KB |
Output is correct |
12 |
Correct |
5 ms |
8608 KB |
Output is correct |
13 |
Correct |
5 ms |
8660 KB |
Output is correct |
14 |
Correct |
5 ms |
8660 KB |
Output is correct |
15 |
Correct |
5 ms |
8600 KB |
Output is correct |
16 |
Correct |
6 ms |
8736 KB |
Output is correct |
17 |
Correct |
5 ms |
8660 KB |
Output is correct |
18 |
Correct |
5 ms |
8656 KB |
Output is correct |
19 |
Correct |
36 ms |
9804 KB |
Output is correct |
20 |
Correct |
35 ms |
9800 KB |
Output is correct |
21 |
Correct |
36 ms |
9984 KB |
Output is correct |
22 |
Correct |
31 ms |
10052 KB |
Output is correct |
23 |
Correct |
30 ms |
10100 KB |
Output is correct |
24 |
Correct |
28 ms |
10188 KB |
Output is correct |
25 |
Correct |
35 ms |
9784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8532 KB |
Output is correct |
3 |
Correct |
5 ms |
8660 KB |
Output is correct |
4 |
Correct |
5 ms |
8660 KB |
Output is correct |
5 |
Correct |
6 ms |
8660 KB |
Output is correct |
6 |
Correct |
6 ms |
8660 KB |
Output is correct |
7 |
Correct |
7 ms |
8660 KB |
Output is correct |
8 |
Correct |
7 ms |
8660 KB |
Output is correct |
9 |
Correct |
4 ms |
8592 KB |
Output is correct |
10 |
Correct |
5 ms |
8532 KB |
Output is correct |
11 |
Correct |
4 ms |
8520 KB |
Output is correct |
12 |
Correct |
5 ms |
8572 KB |
Output is correct |
13 |
Correct |
5 ms |
8660 KB |
Output is correct |
14 |
Correct |
5 ms |
8660 KB |
Output is correct |
15 |
Correct |
5 ms |
8660 KB |
Output is correct |
16 |
Correct |
6 ms |
8656 KB |
Output is correct |
17 |
Correct |
5 ms |
8660 KB |
Output is correct |
18 |
Correct |
4 ms |
8532 KB |
Output is correct |
19 |
Correct |
158 ms |
22844 KB |
Output is correct |
20 |
Correct |
157 ms |
23704 KB |
Output is correct |
21 |
Correct |
190 ms |
24384 KB |
Output is correct |
22 |
Correct |
205 ms |
26320 KB |
Output is correct |
23 |
Correct |
220 ms |
29252 KB |
Output is correct |
24 |
Correct |
247 ms |
29216 KB |
Output is correct |
25 |
Correct |
61 ms |
19168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
245 ms |
23168 KB |
Output is correct |
2 |
Correct |
193 ms |
26320 KB |
Output is correct |
3 |
Correct |
286 ms |
26432 KB |
Output is correct |
4 |
Correct |
230 ms |
31456 KB |
Output is correct |
5 |
Correct |
197 ms |
26592 KB |
Output is correct |
6 |
Correct |
326 ms |
31212 KB |
Output is correct |
7 |
Correct |
286 ms |
31052 KB |
Output is correct |
8 |
Correct |
349 ms |
31372 KB |
Output is correct |
9 |
Correct |
225 ms |
29132 KB |
Output is correct |
10 |
Correct |
267 ms |
30772 KB |
Output is correct |
11 |
Correct |
282 ms |
30600 KB |
Output is correct |
12 |
Correct |
271 ms |
30784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
221 ms |
23212 KB |
Output is correct |
3 |
Correct |
197 ms |
26228 KB |
Output is correct |
4 |
Correct |
254 ms |
26244 KB |
Output is correct |
5 |
Correct |
256 ms |
26840 KB |
Output is correct |
6 |
Correct |
195 ms |
27212 KB |
Output is correct |
7 |
Correct |
225 ms |
27484 KB |
Output is correct |
8 |
Correct |
231 ms |
31512 KB |
Output is correct |
9 |
Correct |
218 ms |
31392 KB |
Output is correct |
10 |
Correct |
212 ms |
26672 KB |
Output is correct |
11 |
Correct |
201 ms |
28492 KB |
Output is correct |
12 |
Correct |
126 ms |
26064 KB |
Output is correct |
13 |
Correct |
5 ms |
8532 KB |
Output is correct |
14 |
Correct |
4 ms |
8532 KB |
Output is correct |
15 |
Correct |
5 ms |
8660 KB |
Output is correct |
16 |
Correct |
5 ms |
8660 KB |
Output is correct |
17 |
Correct |
6 ms |
8660 KB |
Output is correct |
18 |
Correct |
6 ms |
8660 KB |
Output is correct |
19 |
Correct |
7 ms |
8660 KB |
Output is correct |
20 |
Correct |
7 ms |
8660 KB |
Output is correct |
21 |
Correct |
4 ms |
8592 KB |
Output is correct |
22 |
Correct |
4 ms |
8524 KB |
Output is correct |
23 |
Correct |
4 ms |
8520 KB |
Output is correct |
24 |
Correct |
5 ms |
8608 KB |
Output is correct |
25 |
Correct |
5 ms |
8660 KB |
Output is correct |
26 |
Correct |
5 ms |
8660 KB |
Output is correct |
27 |
Correct |
5 ms |
8600 KB |
Output is correct |
28 |
Correct |
6 ms |
8736 KB |
Output is correct |
29 |
Correct |
5 ms |
8660 KB |
Output is correct |
30 |
Correct |
5 ms |
8656 KB |
Output is correct |
31 |
Correct |
36 ms |
9804 KB |
Output is correct |
32 |
Correct |
35 ms |
9800 KB |
Output is correct |
33 |
Correct |
36 ms |
9984 KB |
Output is correct |
34 |
Correct |
31 ms |
10052 KB |
Output is correct |
35 |
Correct |
30 ms |
10100 KB |
Output is correct |
36 |
Correct |
28 ms |
10188 KB |
Output is correct |
37 |
Correct |
35 ms |
9784 KB |
Output is correct |
38 |
Correct |
5 ms |
8532 KB |
Output is correct |
39 |
Correct |
4 ms |
8520 KB |
Output is correct |
40 |
Correct |
5 ms |
8572 KB |
Output is correct |
41 |
Correct |
5 ms |
8660 KB |
Output is correct |
42 |
Correct |
5 ms |
8660 KB |
Output is correct |
43 |
Correct |
5 ms |
8660 KB |
Output is correct |
44 |
Correct |
6 ms |
8656 KB |
Output is correct |
45 |
Correct |
5 ms |
8660 KB |
Output is correct |
46 |
Correct |
4 ms |
8532 KB |
Output is correct |
47 |
Correct |
158 ms |
22844 KB |
Output is correct |
48 |
Correct |
157 ms |
23704 KB |
Output is correct |
49 |
Correct |
190 ms |
24384 KB |
Output is correct |
50 |
Correct |
205 ms |
26320 KB |
Output is correct |
51 |
Correct |
220 ms |
29252 KB |
Output is correct |
52 |
Correct |
247 ms |
29216 KB |
Output is correct |
53 |
Correct |
61 ms |
19168 KB |
Output is correct |
54 |
Correct |
245 ms |
23168 KB |
Output is correct |
55 |
Correct |
193 ms |
26320 KB |
Output is correct |
56 |
Correct |
286 ms |
26432 KB |
Output is correct |
57 |
Correct |
230 ms |
31456 KB |
Output is correct |
58 |
Correct |
197 ms |
26592 KB |
Output is correct |
59 |
Correct |
326 ms |
31212 KB |
Output is correct |
60 |
Correct |
286 ms |
31052 KB |
Output is correct |
61 |
Correct |
349 ms |
31372 KB |
Output is correct |
62 |
Correct |
225 ms |
29132 KB |
Output is correct |
63 |
Correct |
267 ms |
30772 KB |
Output is correct |
64 |
Correct |
282 ms |
30600 KB |
Output is correct |
65 |
Correct |
271 ms |
30784 KB |
Output is correct |
66 |
Correct |
5 ms |
8532 KB |
Output is correct |
67 |
Correct |
182 ms |
26264 KB |
Output is correct |
68 |
Correct |
204 ms |
26328 KB |
Output is correct |
69 |
Correct |
207 ms |
26212 KB |
Output is correct |
70 |
Correct |
245 ms |
26780 KB |
Output is correct |
71 |
Correct |
236 ms |
27260 KB |
Output is correct |
72 |
Correct |
211 ms |
27444 KB |
Output is correct |
73 |
Correct |
231 ms |
31472 KB |
Output is correct |
74 |
Correct |
257 ms |
31476 KB |
Output is correct |
75 |
Correct |
189 ms |
26688 KB |
Output is correct |
76 |
Correct |
226 ms |
28568 KB |
Output is correct |
77 |
Correct |
124 ms |
26056 KB |
Output is correct |
78 |
Correct |
4 ms |
8532 KB |
Output is correct |
79 |
Correct |
6 ms |
8660 KB |
Output is correct |
80 |
Correct |
5 ms |
8660 KB |
Output is correct |
81 |
Correct |
5 ms |
8660 KB |
Output is correct |
82 |
Correct |
6 ms |
8660 KB |
Output is correct |
83 |
Correct |
6 ms |
8660 KB |
Output is correct |
84 |
Correct |
6 ms |
8660 KB |
Output is correct |
85 |
Correct |
4 ms |
8660 KB |
Output is correct |
86 |
Correct |
37 ms |
10748 KB |
Output is correct |
87 |
Correct |
35 ms |
10736 KB |
Output is correct |
88 |
Correct |
36 ms |
10924 KB |
Output is correct |
89 |
Correct |
31 ms |
11108 KB |
Output is correct |
90 |
Correct |
30 ms |
11108 KB |
Output is correct |
91 |
Correct |
29 ms |
11140 KB |
Output is correct |
92 |
Correct |
33 ms |
10796 KB |
Output is correct |
93 |
Correct |
150 ms |
24504 KB |
Output is correct |
94 |
Correct |
145 ms |
23656 KB |
Output is correct |
95 |
Correct |
184 ms |
24472 KB |
Output is correct |
96 |
Correct |
232 ms |
26244 KB |
Output is correct |
97 |
Correct |
258 ms |
29168 KB |
Output is correct |
98 |
Correct |
260 ms |
29232 KB |
Output is correct |
99 |
Correct |
60 ms |
19084 KB |
Output is correct |
100 |
Correct |
304 ms |
31152 KB |
Output is correct |
101 |
Correct |
314 ms |
31104 KB |
Output is correct |
102 |
Correct |
280 ms |
31272 KB |
Output is correct |
103 |
Correct |
310 ms |
30776 KB |
Output is correct |
104 |
Correct |
279 ms |
30572 KB |
Output is correct |
105 |
Correct |
292 ms |
30760 KB |
Output is correct |
106 |
Correct |
184 ms |
24540 KB |
Output is correct |
107 |
Correct |
217 ms |
25768 KB |
Output is correct |
108 |
Correct |
212 ms |
26516 KB |
Output is correct |
109 |
Correct |
233 ms |
26384 KB |
Output is correct |
110 |
Correct |
344 ms |
30960 KB |
Output is correct |
111 |
Correct |
307 ms |
31052 KB |
Output is correct |