#include<bits/stdc++.h>
#define MAXN 400007
using namespace std;
struct event{
int s;
int e;
int id;
inline friend bool operator < (event fr,event sc){
if(fr.e!=sc.e)return fr.e<sc.e;
return fr.s<sc.s;
}
};
int n,cnt,q,S,E,ans;
int st[MAXN],et[MAXN];
int pos[MAXN],parent[MAXN];
event e[MAXN];
vector<int> w;
map<int,int> mp;
pair<int,int> mins[8*MAXN];
pair<int,int> best(pair<int,int> fr,pair<int,int> sc){
if(fr.second<=sc.second)return fr;
return sc;
}
void update(int v,int l,int r,int pos,int val,int id){
if(l==r){
if(val<mins[v].second or val==10000000){
mins[v]={id,val};
}
}else{
int tt=(l+r)/2;
if(pos<=tt)update(2*v,l,tt,pos,val,id);
else update(2*v+1,tt+1,r,pos,val,id);
mins[v]=best(mins[2*v],mins[2*v+1]);
}
}
pair<int,int> getmin(int v,int l,int r,int ll,int rr){
if(ll>rr)return {0,100000000};
if(l==ll and r==rr){
return mins[v];
}else{
int tt=(l+r)/2;
return best( getmin(2*v,l,tt,ll,min(tt,rr)) , getmin(2*v+1,tt+1,r,max(tt+1,ll),rr) );
}
}
int dp[MAXN][20];
bool li[MAXN][20];
int ff(int x,int p){
if(x==0)return 0;
if(p==0)return parent[x];
if(li[x][p])return dp[x][p];
li[x][p]=true;
dp[x][p]=ff(ff(x,p-1),p-1);
return dp[x][p];
}
int bin(int id,int id2){
if(id==id2)return 0;
if(et[id]<st[id2])return -1;
if(et[id2]>et[id])return -1;
if(et[id2]>=st[id])return 1;
int res=0;
for(int i=19;i>=0;i--){
if(ff(id,i)!=0 and st[ff(id,i)]>et[id2]){
id=ff(id,i);
res+=(1<<i);
}
}
if(parent[id]==0)return -1;
if(parent[id]==id2)return res+1;
return res+2;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>st[i]>>et[i];
e[i]={st[i],et[i],i};
w.push_back(e[i].s);
w.push_back(e[i].e);
}
sort(w.begin(),w.end());
for(int i=0;i<w.size();i++){
if(i>0 and w[i]!=w[i-1])cnt++;
mp[w[i]]=cnt;
}
for(int i=1;i<=n;i++){
e[i].s=mp[e[i].s];
e[i].e=mp[e[i].e];
pos[e[i].id]=i;
}
for(int i=0;i<=cnt;i++){
update(1,0,cnt,i,10000000,0);
}
sort(e+1,e+n+1);
for(int i=1;i<=n;i++){
parent[e[i].id]=getmin(1,0,cnt,e[i].s,e[i].e).first;
update(1,0,cnt,e[i].e,e[i].s,e[i].id);
}
for(int i=1;i<=q;i++){
cin>>S>>E;
ans=bin(E,S);
if(ans==-1)cout<<"impossible\n";
else cout<<ans<<"\n";
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:102:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int i=0;i<w.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
176 ms |
21032 KB |
Output is correct |
3 |
Correct |
259 ms |
20968 KB |
Output is correct |
4 |
Correct |
330 ms |
21124 KB |
Output is correct |
5 |
Correct |
253 ms |
21480 KB |
Output is correct |
6 |
Correct |
254 ms |
21960 KB |
Output is correct |
7 |
Correct |
263 ms |
22184 KB |
Output is correct |
8 |
Correct |
200 ms |
21264 KB |
Output is correct |
9 |
Correct |
236 ms |
21232 KB |
Output is correct |
10 |
Correct |
278 ms |
21316 KB |
Output is correct |
11 |
Correct |
267 ms |
25348 KB |
Output is correct |
12 |
Correct |
114 ms |
11720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
39 ms |
1896 KB |
Output is correct |
20 |
Correct |
36 ms |
1868 KB |
Output is correct |
21 |
Correct |
36 ms |
2124 KB |
Output is correct |
22 |
Correct |
29 ms |
2192 KB |
Output is correct |
23 |
Correct |
28 ms |
2388 KB |
Output is correct |
24 |
Correct |
33 ms |
1820 KB |
Output is correct |
25 |
Correct |
33 ms |
2000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
178 ms |
20384 KB |
Output is correct |
20 |
Correct |
168 ms |
20380 KB |
Output is correct |
21 |
Correct |
147 ms |
10864 KB |
Output is correct |
22 |
Correct |
157 ms |
24192 KB |
Output is correct |
23 |
Correct |
229 ms |
17304 KB |
Output is correct |
24 |
Correct |
206 ms |
26340 KB |
Output is correct |
25 |
Correct |
25 ms |
5220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
202 ms |
20936 KB |
Output is correct |
2 |
Correct |
257 ms |
21012 KB |
Output is correct |
3 |
Correct |
316 ms |
20920 KB |
Output is correct |
4 |
Correct |
207 ms |
21168 KB |
Output is correct |
5 |
Correct |
282 ms |
21444 KB |
Output is correct |
6 |
Correct |
373 ms |
27916 KB |
Output is correct |
7 |
Correct |
316 ms |
27900 KB |
Output is correct |
8 |
Correct |
250 ms |
27952 KB |
Output is correct |
9 |
Correct |
236 ms |
17304 KB |
Output is correct |
10 |
Correct |
250 ms |
27528 KB |
Output is correct |
11 |
Correct |
238 ms |
22116 KB |
Output is correct |
12 |
Correct |
265 ms |
27560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
176 ms |
21032 KB |
Output is correct |
3 |
Correct |
259 ms |
20968 KB |
Output is correct |
4 |
Correct |
330 ms |
21124 KB |
Output is correct |
5 |
Correct |
253 ms |
21480 KB |
Output is correct |
6 |
Correct |
254 ms |
21960 KB |
Output is correct |
7 |
Correct |
263 ms |
22184 KB |
Output is correct |
8 |
Correct |
200 ms |
21264 KB |
Output is correct |
9 |
Correct |
236 ms |
21232 KB |
Output is correct |
10 |
Correct |
278 ms |
21316 KB |
Output is correct |
11 |
Correct |
267 ms |
25348 KB |
Output is correct |
12 |
Correct |
114 ms |
11720 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
39 ms |
1896 KB |
Output is correct |
32 |
Correct |
36 ms |
1868 KB |
Output is correct |
33 |
Correct |
36 ms |
2124 KB |
Output is correct |
34 |
Correct |
29 ms |
2192 KB |
Output is correct |
35 |
Correct |
28 ms |
2388 KB |
Output is correct |
36 |
Correct |
33 ms |
1820 KB |
Output is correct |
37 |
Correct |
33 ms |
2000 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
2 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
178 ms |
20384 KB |
Output is correct |
48 |
Correct |
168 ms |
20380 KB |
Output is correct |
49 |
Correct |
147 ms |
10864 KB |
Output is correct |
50 |
Correct |
157 ms |
24192 KB |
Output is correct |
51 |
Correct |
229 ms |
17304 KB |
Output is correct |
52 |
Correct |
206 ms |
26340 KB |
Output is correct |
53 |
Correct |
25 ms |
5220 KB |
Output is correct |
54 |
Correct |
202 ms |
20936 KB |
Output is correct |
55 |
Correct |
257 ms |
21012 KB |
Output is correct |
56 |
Correct |
316 ms |
20920 KB |
Output is correct |
57 |
Correct |
207 ms |
21168 KB |
Output is correct |
58 |
Correct |
282 ms |
21444 KB |
Output is correct |
59 |
Correct |
373 ms |
27916 KB |
Output is correct |
60 |
Correct |
316 ms |
27900 KB |
Output is correct |
61 |
Correct |
250 ms |
27952 KB |
Output is correct |
62 |
Correct |
236 ms |
17304 KB |
Output is correct |
63 |
Correct |
250 ms |
27528 KB |
Output is correct |
64 |
Correct |
238 ms |
22116 KB |
Output is correct |
65 |
Correct |
265 ms |
27560 KB |
Output is correct |
66 |
Correct |
0 ms |
340 KB |
Output is correct |
67 |
Correct |
191 ms |
24116 KB |
Output is correct |
68 |
Correct |
266 ms |
24044 KB |
Output is correct |
69 |
Correct |
332 ms |
23924 KB |
Output is correct |
70 |
Correct |
252 ms |
24644 KB |
Output is correct |
71 |
Correct |
258 ms |
24984 KB |
Output is correct |
72 |
Correct |
236 ms |
25304 KB |
Output is correct |
73 |
Correct |
203 ms |
24428 KB |
Output is correct |
74 |
Correct |
205 ms |
24280 KB |
Output is correct |
75 |
Correct |
276 ms |
24348 KB |
Output is correct |
76 |
Correct |
249 ms |
28288 KB |
Output is correct |
77 |
Correct |
121 ms |
14008 KB |
Output is correct |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
2 ms |
596 KB |
Output is correct |
80 |
Correct |
2 ms |
504 KB |
Output is correct |
81 |
Correct |
2 ms |
544 KB |
Output is correct |
82 |
Correct |
2 ms |
468 KB |
Output is correct |
83 |
Correct |
2 ms |
640 KB |
Output is correct |
84 |
Correct |
2 ms |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
340 KB |
Output is correct |
86 |
Correct |
40 ms |
2860 KB |
Output is correct |
87 |
Correct |
39 ms |
2940 KB |
Output is correct |
88 |
Correct |
37 ms |
3048 KB |
Output is correct |
89 |
Correct |
27 ms |
3156 KB |
Output is correct |
90 |
Correct |
29 ms |
3404 KB |
Output is correct |
91 |
Correct |
28 ms |
2904 KB |
Output is correct |
92 |
Correct |
35 ms |
2980 KB |
Output is correct |
93 |
Correct |
164 ms |
22336 KB |
Output is correct |
94 |
Correct |
159 ms |
21500 KB |
Output is correct |
95 |
Correct |
147 ms |
12744 KB |
Output is correct |
96 |
Correct |
161 ms |
26060 KB |
Output is correct |
97 |
Correct |
209 ms |
19288 KB |
Output is correct |
98 |
Correct |
206 ms |
26244 KB |
Output is correct |
99 |
Correct |
28 ms |
5204 KB |
Output is correct |
100 |
Correct |
302 ms |
30872 KB |
Output is correct |
101 |
Correct |
296 ms |
30956 KB |
Output is correct |
102 |
Correct |
250 ms |
31020 KB |
Output is correct |
103 |
Correct |
261 ms |
30660 KB |
Output is correct |
104 |
Correct |
233 ms |
24992 KB |
Output is correct |
105 |
Correct |
268 ms |
30660 KB |
Output is correct |
106 |
Correct |
233 ms |
22380 KB |
Output is correct |
107 |
Correct |
283 ms |
23496 KB |
Output is correct |
108 |
Correct |
182 ms |
23868 KB |
Output is correct |
109 |
Correct |
203 ms |
24008 KB |
Output is correct |
110 |
Correct |
247 ms |
30400 KB |
Output is correct |
111 |
Correct |
256 ms |
30420 KB |
Output is correct |