#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
pair<int,int>max(pair<int,int>a,pair<int,int>b) {
return a.first>b.first?a:b;
}
const int k=(1<<20);
pair<int,int>maxv[k*2+1];
void upd(int x,int va,int ind) {
pair<int,int>val={va,ind};
while(x>0) {
maxv[x]=max(maxv[x],val);
x>>=1;
}
}
pair<int,int> ask(int &from,int &to,int cur=1,int beg=0,int en=k-1) {
if(from<=beg && en<=to)return maxv[cur];
if(to<beg || en<from)return {-1,-1};
int mid=(beg+en)>>1;
return max(ask(from,to,cur<<1,beg,mid),ask(from,to,(cur<<1)|1,mid+1,en));
}
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n,q;cin>>n>>q;
pair<int,int>a[n];
vector<int>v;
for(int i=0;i<n;i++) {
int x,y;
cin>>x>>y;
v.push_back(x);
v.push_back(y);
a[i]={x,y};
}
sort(v.begin(),v.end());
map<int,int>m;
for(int i=0;i<v.size();i++)m[v[i]]=i;
for(int i=0;i<n;i++) {
int x,y;
a[i].first=m[a[i].first];
a[i].second=m[a[i].second];
x=a[i].first;
y=a[i].second;
upd(y+k,k-x,i);
}
pair<int,int> prev[n][20];
for(int i=0;i<n;i++) {
pair<int,int>d=ask(a[i].first,a[i].second);
d.first=k-d.first;
prev[i][0]=d;
}
for(int j=1;j<20;j++) {
for(int i=0;i<n;i++) {
prev[i][j]=prev[prev[i][j-1].second][j-1];
}
}
for(int i=0;i<q;i++) {
int x,y;cin>>x>>y;x--;y--;
if(x==y)cout<<0<<endl;
else if(a[x].second>=a[y].first && a[x].second<=a[y].second)cout<<1<<endl;
else if(a[x].second>a[y].second){cout<<"impossible"<<endl;}
else {
int cnt=0;
int cur=y;
int en=a[x].second;
for(int j=19;j>=0;j--) {
if(prev[cur][j].first>en) {
cur=prev[cur][j].second;
cnt+=(1<<j);
}
}
if(prev[cur][0].first>en)cout<<"impossible"<<endl;
else cout<<cnt+2<<endl;
}
}
return 0;
}
///9:30
Compilation message
events.cpp: In function 'int main()':
events.cpp:36:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for(int i=0;i<v.size();i++)m[v[i]]=i;
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
193 ms |
25964 KB |
Output is correct |
3 |
Correct |
213 ms |
25948 KB |
Output is correct |
4 |
Correct |
253 ms |
25956 KB |
Output is correct |
5 |
Correct |
207 ms |
26480 KB |
Output is correct |
6 |
Correct |
226 ms |
26960 KB |
Output is correct |
7 |
Correct |
215 ms |
27116 KB |
Output is correct |
8 |
Correct |
222 ms |
31112 KB |
Output is correct |
9 |
Correct |
207 ms |
31176 KB |
Output is correct |
10 |
Correct |
204 ms |
26340 KB |
Output is correct |
11 |
Correct |
226 ms |
28256 KB |
Output is correct |
12 |
Correct |
130 ms |
26488 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 |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
468 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 |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
34 ms |
2088 KB |
Output is correct |
20 |
Correct |
31 ms |
2048 KB |
Output is correct |
21 |
Correct |
32 ms |
2304 KB |
Output is correct |
22 |
Correct |
27 ms |
2436 KB |
Output is correct |
23 |
Correct |
28 ms |
2424 KB |
Output is correct |
24 |
Correct |
38 ms |
2484 KB |
Output is correct |
25 |
Correct |
29 ms |
2132 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 |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
162 ms |
25360 KB |
Output is correct |
20 |
Correct |
158 ms |
25392 KB |
Output is correct |
21 |
Correct |
172 ms |
25424 KB |
Output is correct |
22 |
Correct |
186 ms |
27104 KB |
Output is correct |
23 |
Correct |
211 ms |
28528 KB |
Output is correct |
24 |
Correct |
221 ms |
30044 KB |
Output is correct |
25 |
Correct |
80 ms |
17676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
181 ms |
25992 KB |
Output is correct |
2 |
Correct |
223 ms |
25940 KB |
Output is correct |
3 |
Correct |
228 ms |
26092 KB |
Output is correct |
4 |
Correct |
226 ms |
31176 KB |
Output is correct |
5 |
Correct |
209 ms |
26344 KB |
Output is correct |
6 |
Correct |
243 ms |
30784 KB |
Output is correct |
7 |
Correct |
247 ms |
30924 KB |
Output is correct |
8 |
Correct |
262 ms |
30956 KB |
Output is correct |
9 |
Correct |
205 ms |
28500 KB |
Output is correct |
10 |
Correct |
232 ms |
30440 KB |
Output is correct |
11 |
Correct |
243 ms |
29904 KB |
Output is correct |
12 |
Correct |
240 ms |
30408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
193 ms |
25964 KB |
Output is correct |
3 |
Correct |
213 ms |
25948 KB |
Output is correct |
4 |
Correct |
253 ms |
25956 KB |
Output is correct |
5 |
Correct |
207 ms |
26480 KB |
Output is correct |
6 |
Correct |
226 ms |
26960 KB |
Output is correct |
7 |
Correct |
215 ms |
27116 KB |
Output is correct |
8 |
Correct |
222 ms |
31112 KB |
Output is correct |
9 |
Correct |
207 ms |
31176 KB |
Output is correct |
10 |
Correct |
204 ms |
26340 KB |
Output is correct |
11 |
Correct |
226 ms |
28256 KB |
Output is correct |
12 |
Correct |
130 ms |
26488 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 |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
2 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
34 ms |
2088 KB |
Output is correct |
32 |
Correct |
31 ms |
2048 KB |
Output is correct |
33 |
Correct |
32 ms |
2304 KB |
Output is correct |
34 |
Correct |
27 ms |
2436 KB |
Output is correct |
35 |
Correct |
28 ms |
2424 KB |
Output is correct |
36 |
Correct |
38 ms |
2484 KB |
Output is correct |
37 |
Correct |
29 ms |
2132 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
2 ms |
596 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
2 ms |
596 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
2 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
162 ms |
25360 KB |
Output is correct |
48 |
Correct |
158 ms |
25392 KB |
Output is correct |
49 |
Correct |
172 ms |
25424 KB |
Output is correct |
50 |
Correct |
186 ms |
27104 KB |
Output is correct |
51 |
Correct |
211 ms |
28528 KB |
Output is correct |
52 |
Correct |
221 ms |
30044 KB |
Output is correct |
53 |
Correct |
80 ms |
17676 KB |
Output is correct |
54 |
Correct |
181 ms |
25992 KB |
Output is correct |
55 |
Correct |
223 ms |
25940 KB |
Output is correct |
56 |
Correct |
228 ms |
26092 KB |
Output is correct |
57 |
Correct |
226 ms |
31176 KB |
Output is correct |
58 |
Correct |
209 ms |
26344 KB |
Output is correct |
59 |
Correct |
243 ms |
30784 KB |
Output is correct |
60 |
Correct |
247 ms |
30924 KB |
Output is correct |
61 |
Correct |
262 ms |
30956 KB |
Output is correct |
62 |
Correct |
205 ms |
28500 KB |
Output is correct |
63 |
Correct |
232 ms |
30440 KB |
Output is correct |
64 |
Correct |
243 ms |
29904 KB |
Output is correct |
65 |
Correct |
240 ms |
30408 KB |
Output is correct |
66 |
Correct |
0 ms |
340 KB |
Output is correct |
67 |
Correct |
184 ms |
25932 KB |
Output is correct |
68 |
Correct |
193 ms |
25976 KB |
Output is correct |
69 |
Correct |
239 ms |
25960 KB |
Output is correct |
70 |
Correct |
207 ms |
26512 KB |
Output is correct |
71 |
Correct |
216 ms |
26944 KB |
Output is correct |
72 |
Correct |
203 ms |
27080 KB |
Output is correct |
73 |
Correct |
227 ms |
31136 KB |
Output is correct |
74 |
Correct |
223 ms |
31196 KB |
Output is correct |
75 |
Correct |
219 ms |
26372 KB |
Output is correct |
76 |
Correct |
206 ms |
28272 KB |
Output is correct |
77 |
Correct |
126 ms |
26432 KB |
Output is correct |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
2 ms |
604 KB |
Output is correct |
80 |
Correct |
1 ms |
596 KB |
Output is correct |
81 |
Correct |
1 ms |
596 KB |
Output is correct |
82 |
Correct |
1 ms |
596 KB |
Output is correct |
83 |
Correct |
1 ms |
596 KB |
Output is correct |
84 |
Correct |
1 ms |
596 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
30 ms |
2168 KB |
Output is correct |
87 |
Correct |
30 ms |
2024 KB |
Output is correct |
88 |
Correct |
31 ms |
2388 KB |
Output is correct |
89 |
Correct |
27 ms |
2388 KB |
Output is correct |
90 |
Correct |
28 ms |
2488 KB |
Output is correct |
91 |
Correct |
26 ms |
2388 KB |
Output is correct |
92 |
Correct |
29 ms |
2124 KB |
Output is correct |
93 |
Correct |
165 ms |
25328 KB |
Output is correct |
94 |
Correct |
166 ms |
25336 KB |
Output is correct |
95 |
Correct |
168 ms |
25396 KB |
Output is correct |
96 |
Correct |
179 ms |
27136 KB |
Output is correct |
97 |
Correct |
210 ms |
28528 KB |
Output is correct |
98 |
Correct |
211 ms |
30080 KB |
Output is correct |
99 |
Correct |
75 ms |
17744 KB |
Output is correct |
100 |
Correct |
256 ms |
30848 KB |
Output is correct |
101 |
Correct |
258 ms |
33912 KB |
Output is correct |
102 |
Correct |
238 ms |
34072 KB |
Output is correct |
103 |
Correct |
232 ms |
33480 KB |
Output is correct |
104 |
Correct |
236 ms |
32912 KB |
Output is correct |
105 |
Correct |
246 ms |
33544 KB |
Output is correct |
106 |
Correct |
192 ms |
26952 KB |
Output is correct |
107 |
Correct |
234 ms |
28568 KB |
Output is correct |
108 |
Correct |
196 ms |
29356 KB |
Output is correct |
109 |
Correct |
196 ms |
29140 KB |
Output is correct |
110 |
Correct |
253 ms |
33868 KB |
Output is correct |
111 |
Correct |
251 ms |
33752 KB |
Output is correct |