#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<pair<int,int>,int> a,pair<pair<int,int>,int> b){
return a.first.second<b.first.second;
}
int P[100001][17],ind[100001];
pair<long long,long long> seg[800001];
void build(int p,int l,int r){
if(l==r){
seg[p] = {1e18,-1};
return ;
}
int md = (l+r)/2;
build(p*2,l,md);build(p*2+1,md+1,r);
seg[p] = min(seg[p*2],seg[p*2+1]);
}
pair<long long,long long> query(int p,int l,int r,int lq,int rq){
if(l>=lq&&r<=rq)return seg[p];
if(r<lq||l>rq)return {1e18,-1};
int md = (l+r)/2;
return min(query(p*2,l,md,lq,rq),query(p*2+1,md+1,r,lq,rq));
}
void update(int p,int l,int r,int idx,pair<long long,long long> val){
if(l==r){
seg[p] = min(seg[p],val);
return ;
}
int md = (l+r)/2;
if(idx<=md)update(p*2,l,md,idx,val);
else update(p*2+1,md+1,r,idx,val);
seg[p] = min(seg[p*2],seg[p*2+1]);
}
signed main(){
int n,q;cin>>n>>q;
vector<pair<pair<long long,long long>,int>> v;
map<int,int> comp,sav;
for(int i = 0;i<n;i++){
int a,b;
cin>>a>>b;
comp[a]++;comp[b]++;
v.push_back({{a,b},i});
}
long long z = 0;
for(auto i:comp){
sav[i.first] = ++z;
}
for(int i = 0;i<n;i++){
v[i].first.first = sav[v[i].first.first];
v[i].first.second = sav[v[i].first.second];
}
sort(v.begin(),v.end());
build(1,0,z);
for(long long i = 0;i<v.size();i++){
update(1,0,z,v[i].first.second,{v[i].first.first,i});
}
int in = 0;
for(int i = 0;i<v.size();i++){
ind[v[i].second] = i;
P[i][0] = query(1,0,z,v[i].first.first,v[i].first.second).second;
//cout<<P[i][0]<<" "<<v[i].first.first<<" "<<v[i].first.second<<endl;
}
for(int i = 0;i<n;i++){
for(int j = 1;j<17;j++){
P[i][j] = P[P[i][j-1]][j-1];
}
}
while(q--){
int a,b;cin>>a>>b;
a--;b--;a = ind[a] , b = ind[b];
if(a==b){
cout<<0<<endl;
continue;
}else if(v[b].first.first<=v[a].first.second&&v[a].first.second<=v[b].first.second){
cout<<1<<endl;
continue;
}
int sum = 1 , cur = b;
//cout<<cur<<endl;
for(int i = 16;i>=0;i--){
if(v[P[cur][i]].first.first>v[a].first.second){
sum+=(1<<i);
cur = P[cur][i];
}
}
if(v[P[cur][0]].first.first<=v[a].first.second&&v[P[cur][0]].first.second>=v[a].first.second){
cout<<sum+1<<endl;
}else cout<<"impossible\n";
}
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:53:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(long long i = 0;i<v.size();i++){
| ~^~~~~~~~~
events.cpp:57:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<long long int, long long int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int i = 0;i<v.size();i++){
| ~^~~~~~~~~
events.cpp:56:9: warning: unused variable 'in' [-Wunused-variable]
56 | int in = 0;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
344 ms |
24108 KB |
Output is correct |
3 |
Correct |
341 ms |
24100 KB |
Output is correct |
4 |
Correct |
347 ms |
24060 KB |
Output is correct |
5 |
Correct |
355 ms |
25020 KB |
Output is correct |
6 |
Correct |
355 ms |
26120 KB |
Output is correct |
7 |
Correct |
348 ms |
26364 KB |
Output is correct |
8 |
Correct |
388 ms |
37884 KB |
Output is correct |
9 |
Correct |
379 ms |
38008 KB |
Output is correct |
10 |
Correct |
347 ms |
24632 KB |
Output is correct |
11 |
Correct |
380 ms |
32764 KB |
Output is correct |
12 |
Correct |
273 ms |
24736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2392 KB |
Output is correct |
5 |
Correct |
2 ms |
2392 KB |
Output is correct |
6 |
Correct |
2 ms |
2648 KB |
Output is correct |
7 |
Correct |
2 ms |
2648 KB |
Output is correct |
8 |
Correct |
2 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2392 KB |
Output is correct |
5 |
Correct |
2 ms |
2392 KB |
Output is correct |
6 |
Correct |
2 ms |
2648 KB |
Output is correct |
7 |
Correct |
2 ms |
2648 KB |
Output is correct |
8 |
Correct |
2 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2392 KB |
Output is correct |
12 |
Correct |
2 ms |
2392 KB |
Output is correct |
13 |
Correct |
2 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
2392 KB |
Output is correct |
15 |
Correct |
2 ms |
2392 KB |
Output is correct |
16 |
Correct |
2 ms |
2648 KB |
Output is correct |
17 |
Correct |
2 ms |
2648 KB |
Output is correct |
18 |
Correct |
2 ms |
2648 KB |
Output is correct |
19 |
Correct |
164 ms |
5712 KB |
Output is correct |
20 |
Correct |
168 ms |
5612 KB |
Output is correct |
21 |
Correct |
164 ms |
5964 KB |
Output is correct |
22 |
Correct |
160 ms |
6228 KB |
Output is correct |
23 |
Correct |
161 ms |
6224 KB |
Output is correct |
24 |
Correct |
161 ms |
6252 KB |
Output is correct |
25 |
Correct |
161 ms |
5968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2392 KB |
Output is correct |
5 |
Correct |
2 ms |
2392 KB |
Output is correct |
6 |
Correct |
2 ms |
2648 KB |
Output is correct |
7 |
Correct |
2 ms |
2648 KB |
Output is correct |
8 |
Correct |
2 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
0 ms |
2392 KB |
Output is correct |
12 |
Correct |
2 ms |
2648 KB |
Output is correct |
13 |
Correct |
2 ms |
2392 KB |
Output is correct |
14 |
Correct |
2 ms |
2392 KB |
Output is correct |
15 |
Correct |
2 ms |
2648 KB |
Output is correct |
16 |
Correct |
2 ms |
2648 KB |
Output is correct |
17 |
Correct |
2 ms |
2648 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
174 ms |
23296 KB |
Output is correct |
20 |
Correct |
158 ms |
23436 KB |
Output is correct |
21 |
Correct |
191 ms |
23292 KB |
Output is correct |
22 |
Correct |
199 ms |
30968 KB |
Output is correct |
23 |
Correct |
250 ms |
36860 KB |
Output is correct |
24 |
Correct |
244 ms |
36852 KB |
Output is correct |
25 |
Correct |
52 ms |
10172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
336 ms |
24060 KB |
Output is correct |
2 |
Correct |
342 ms |
24152 KB |
Output is correct |
3 |
Correct |
344 ms |
24572 KB |
Output is correct |
4 |
Correct |
368 ms |
38396 KB |
Output is correct |
5 |
Correct |
357 ms |
24352 KB |
Output is correct |
6 |
Correct |
471 ms |
37880 KB |
Output is correct |
7 |
Correct |
417 ms |
37884 KB |
Output is correct |
8 |
Correct |
416 ms |
37880 KB |
Output is correct |
9 |
Correct |
247 ms |
37116 KB |
Output is correct |
10 |
Correct |
401 ms |
37500 KB |
Output is correct |
11 |
Correct |
424 ms |
37336 KB |
Output is correct |
12 |
Correct |
402 ms |
37368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
344 ms |
24108 KB |
Output is correct |
3 |
Correct |
341 ms |
24100 KB |
Output is correct |
4 |
Correct |
347 ms |
24060 KB |
Output is correct |
5 |
Correct |
355 ms |
25020 KB |
Output is correct |
6 |
Correct |
355 ms |
26120 KB |
Output is correct |
7 |
Correct |
348 ms |
26364 KB |
Output is correct |
8 |
Correct |
388 ms |
37884 KB |
Output is correct |
9 |
Correct |
379 ms |
38008 KB |
Output is correct |
10 |
Correct |
347 ms |
24632 KB |
Output is correct |
11 |
Correct |
380 ms |
32764 KB |
Output is correct |
12 |
Correct |
273 ms |
24736 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
2396 KB |
Output is correct |
16 |
Correct |
2 ms |
2392 KB |
Output is correct |
17 |
Correct |
2 ms |
2392 KB |
Output is correct |
18 |
Correct |
2 ms |
2648 KB |
Output is correct |
19 |
Correct |
2 ms |
2648 KB |
Output is correct |
20 |
Correct |
2 ms |
2648 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
0 ms |
2392 KB |
Output is correct |
24 |
Correct |
2 ms |
2392 KB |
Output is correct |
25 |
Correct |
2 ms |
2396 KB |
Output is correct |
26 |
Correct |
2 ms |
2392 KB |
Output is correct |
27 |
Correct |
2 ms |
2392 KB |
Output is correct |
28 |
Correct |
2 ms |
2648 KB |
Output is correct |
29 |
Correct |
2 ms |
2648 KB |
Output is correct |
30 |
Correct |
2 ms |
2648 KB |
Output is correct |
31 |
Correct |
164 ms |
5712 KB |
Output is correct |
32 |
Correct |
168 ms |
5612 KB |
Output is correct |
33 |
Correct |
164 ms |
5964 KB |
Output is correct |
34 |
Correct |
160 ms |
6228 KB |
Output is correct |
35 |
Correct |
161 ms |
6224 KB |
Output is correct |
36 |
Correct |
161 ms |
6252 KB |
Output is correct |
37 |
Correct |
161 ms |
5968 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
0 ms |
2392 KB |
Output is correct |
40 |
Correct |
2 ms |
2648 KB |
Output is correct |
41 |
Correct |
2 ms |
2392 KB |
Output is correct |
42 |
Correct |
2 ms |
2392 KB |
Output is correct |
43 |
Correct |
2 ms |
2648 KB |
Output is correct |
44 |
Correct |
2 ms |
2648 KB |
Output is correct |
45 |
Correct |
2 ms |
2648 KB |
Output is correct |
46 |
Correct |
1 ms |
2392 KB |
Output is correct |
47 |
Correct |
174 ms |
23296 KB |
Output is correct |
48 |
Correct |
158 ms |
23436 KB |
Output is correct |
49 |
Correct |
191 ms |
23292 KB |
Output is correct |
50 |
Correct |
199 ms |
30968 KB |
Output is correct |
51 |
Correct |
250 ms |
36860 KB |
Output is correct |
52 |
Correct |
244 ms |
36852 KB |
Output is correct |
53 |
Correct |
52 ms |
10172 KB |
Output is correct |
54 |
Correct |
336 ms |
24060 KB |
Output is correct |
55 |
Correct |
342 ms |
24152 KB |
Output is correct |
56 |
Correct |
344 ms |
24572 KB |
Output is correct |
57 |
Correct |
368 ms |
38396 KB |
Output is correct |
58 |
Correct |
357 ms |
24352 KB |
Output is correct |
59 |
Correct |
471 ms |
37880 KB |
Output is correct |
60 |
Correct |
417 ms |
37884 KB |
Output is correct |
61 |
Correct |
416 ms |
37880 KB |
Output is correct |
62 |
Correct |
247 ms |
37116 KB |
Output is correct |
63 |
Correct |
401 ms |
37500 KB |
Output is correct |
64 |
Correct |
424 ms |
37336 KB |
Output is correct |
65 |
Correct |
402 ms |
37368 KB |
Output is correct |
66 |
Correct |
1 ms |
2392 KB |
Output is correct |
67 |
Correct |
333 ms |
24080 KB |
Output is correct |
68 |
Correct |
344 ms |
24984 KB |
Output is correct |
69 |
Correct |
343 ms |
24060 KB |
Output is correct |
70 |
Correct |
355 ms |
25224 KB |
Output is correct |
71 |
Correct |
345 ms |
25852 KB |
Output is correct |
72 |
Correct |
358 ms |
26836 KB |
Output is correct |
73 |
Correct |
387 ms |
38908 KB |
Output is correct |
74 |
Correct |
370 ms |
38652 KB |
Output is correct |
75 |
Correct |
338 ms |
25084 KB |
Output is correct |
76 |
Correct |
359 ms |
32508 KB |
Output is correct |
77 |
Correct |
273 ms |
24572 KB |
Output is correct |
78 |
Correct |
1 ms |
2392 KB |
Output is correct |
79 |
Correct |
2 ms |
2392 KB |
Output is correct |
80 |
Correct |
2 ms |
2392 KB |
Output is correct |
81 |
Correct |
2 ms |
2392 KB |
Output is correct |
82 |
Correct |
2 ms |
2796 KB |
Output is correct |
83 |
Correct |
2 ms |
2652 KB |
Output is correct |
84 |
Correct |
3 ms |
2648 KB |
Output is correct |
85 |
Correct |
2 ms |
2392 KB |
Output is correct |
86 |
Correct |
180 ms |
5712 KB |
Output is correct |
87 |
Correct |
159 ms |
5660 KB |
Output is correct |
88 |
Correct |
166 ms |
5968 KB |
Output is correct |
89 |
Correct |
159 ms |
6032 KB |
Output is correct |
90 |
Correct |
162 ms |
6360 KB |
Output is correct |
91 |
Correct |
176 ms |
6440 KB |
Output is correct |
92 |
Correct |
161 ms |
5968 KB |
Output is correct |
93 |
Correct |
177 ms |
23288 KB |
Output is correct |
94 |
Correct |
162 ms |
23432 KB |
Output is correct |
95 |
Correct |
189 ms |
23376 KB |
Output is correct |
96 |
Correct |
196 ms |
30968 KB |
Output is correct |
97 |
Correct |
247 ms |
37116 KB |
Output is correct |
98 |
Correct |
286 ms |
37172 KB |
Output is correct |
99 |
Correct |
52 ms |
9920 KB |
Output is correct |
100 |
Correct |
410 ms |
37896 KB |
Output is correct |
101 |
Correct |
420 ms |
37632 KB |
Output is correct |
102 |
Correct |
406 ms |
37852 KB |
Output is correct |
103 |
Correct |
405 ms |
37188 KB |
Output is correct |
104 |
Correct |
435 ms |
37112 KB |
Output is correct |
105 |
Correct |
403 ms |
37344 KB |
Output is correct |
106 |
Correct |
334 ms |
22336 KB |
Output is correct |
107 |
Correct |
349 ms |
24312 KB |
Output is correct |
108 |
Correct |
362 ms |
24316 KB |
Output is correct |
109 |
Correct |
357 ms |
24712 KB |
Output is correct |
110 |
Correct |
460 ms |
37836 KB |
Output is correct |
111 |
Correct |
442 ms |
37624 KB |
Output is correct |