#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define pb push_back
const int nax=3e5+5;
pair<int,int> segtree[4*nax];
struct query{
int x,type,idx,value;
};
bool compare(query a,query b){
if(a.x!=b.x) return a.x<b.x;
else if(a.type!=b.type) return a.type<b.type;
else if(a.value!=b.value) return a.value<b.value;
else return a.idx<b.idx;
}
vector<query> sweep;
void build(int pos,int l,int r){
if(l==r){
segtree[pos]={sweep[l].value,sweep[l].idx};
return;
}
int mid=(r+l)/2;
build(pos*2+1,l,mid);
build(pos*2+2,mid+1,r);
segtree[pos]=min(segtree[pos*2+1],segtree[pos*2+2]);
return;
}
pair<int,int> query(int pos,int l,int r,int left,int right){
if(l>r||l>right||r<left) return {1e9,1e9};
else if(l>=left&&r<=right) {
return segtree[pos];
}
int mid=(r+l)/2;
return min(query(pos*2+1,l,mid,left,right),query(pos*2+2,mid+1,r,left,right));
}
int main(){
int n,q;
cin>>n>>q;
for (int i = 0; i < n*2*4; ++i)
{
segtree[i]={1e9,1e9};
}
vector<pair<int,int>> tab(n);
for (int i = 0; i < n; ++i)
{
cin>>tab[i].fi>>tab[i].se;
sweep.pb({tab[i].fi,0,i,tab[i].fi});
sweep.pb({tab[i].se,1,i,tab[i].fi});
}
sort(sweep.begin(),sweep.end(),compare);
pair<int,int> pos[n];
for (int i = 0; i < sweep.size(); ++i)
{
if(sweep[i].type==0){
pos[sweep[i].idx].fi=i;
}else{
pos[sweep[i].idx].se=i;
}
}
build(0,0,sweep.size()-1);
int dp[n][20];
for (int i = 0; i < sweep.size(); ++i)
{
if(sweep[i].type==0){
dp[sweep[i].idx][0]=query(0,0,sweep.size()-1,pos[sweep[i].idx].fi,pos[sweep[i].idx].se).se;
}
}
for (int j = 1; j < 20; ++j)
{
for (int i = 0; i < n; ++i)
{
dp[i][j]=dp[dp[i][j-1]][j-1];
}
}
while(q--){
int s,e;
cin>>s>>e;
s--;e--;
int ans=0;
if(s==e){
cout <<0<<endl;
continue;
}else if(tab[e].se<tab[s].se){
cout <<"impossible"<<endl;
continue;
}else if(tab[e].fi<=tab[s].se&&tab[s].se<=tab[e].se){
cout <<1<<endl;
continue;
}
for (int i = 19; i >= 0 ; i--)
{
if(tab[dp[e][i]].fi>tab[s].fi){
ans+=(1<<i);
e=dp[e][i];
}
}
//cout <<e<<endl;
if(tab[dp[e][0]].fi > tab[s].se){
cout <<"impossible"<<endl;
continue;
}else if(tab[dp[e][0]].se>tab[s].se) cout <<ans+2<<endl;
else cout <<ans+1<<endl;
}
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:54:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int i = 0; i < sweep.size(); ++i)
| ~~^~~~~~~~~~~~~~
events.cpp:64:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for (int i = 0; i < sweep.size(); ++i)
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
247 ms |
20444 KB |
Output is correct |
3 |
Correct |
240 ms |
20668 KB |
Output is correct |
4 |
Correct |
286 ms |
20668 KB |
Output is correct |
5 |
Correct |
307 ms |
20028 KB |
Output is correct |
6 |
Correct |
247 ms |
20348 KB |
Output is correct |
7 |
Correct |
247 ms |
20044 KB |
Output is correct |
8 |
Correct |
236 ms |
24252 KB |
Output is correct |
9 |
Correct |
238 ms |
23484 KB |
Output is correct |
10 |
Correct |
249 ms |
23412 KB |
Output is correct |
11 |
Correct |
269 ms |
23412 KB |
Output is correct |
12 |
Correct |
215 ms |
22788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
600 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Incorrect |
2 ms |
592 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
600 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Incorrect |
2 ms |
592 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
600 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Incorrect |
2 ms |
592 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
239 ms |
20168 KB |
Output is correct |
2 |
Correct |
260 ms |
19840 KB |
Output is correct |
3 |
Correct |
253 ms |
20328 KB |
Output is correct |
4 |
Correct |
252 ms |
20400 KB |
Output is correct |
5 |
Correct |
252 ms |
20276 KB |
Output is correct |
6 |
Correct |
315 ms |
20120 KB |
Output is correct |
7 |
Correct |
249 ms |
20060 KB |
Output is correct |
8 |
Correct |
254 ms |
20152 KB |
Output is correct |
9 |
Correct |
99 ms |
19640 KB |
Output is correct |
10 |
Correct |
250 ms |
20076 KB |
Output is correct |
11 |
Correct |
248 ms |
19952 KB |
Output is correct |
12 |
Correct |
261 ms |
19720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
247 ms |
20444 KB |
Output is correct |
3 |
Correct |
240 ms |
20668 KB |
Output is correct |
4 |
Correct |
286 ms |
20668 KB |
Output is correct |
5 |
Correct |
307 ms |
20028 KB |
Output is correct |
6 |
Correct |
247 ms |
20348 KB |
Output is correct |
7 |
Correct |
247 ms |
20044 KB |
Output is correct |
8 |
Correct |
236 ms |
24252 KB |
Output is correct |
9 |
Correct |
238 ms |
23484 KB |
Output is correct |
10 |
Correct |
249 ms |
23412 KB |
Output is correct |
11 |
Correct |
269 ms |
23412 KB |
Output is correct |
12 |
Correct |
215 ms |
22788 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Incorrect |
2 ms |
592 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |