#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define pb push_back
const int nax=2e5+5;
vector<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 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]=max(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 {0,-1};
else if(l>=left&&r<=right) return segtree[pos];
int mid=(r+l)/2;
return max(query(pos*2+1,l,mid,left,right),query(pos*2+2,mid+1,r,left,right));
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n,q;
cin>>n>>q;
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].se});
sweep.pb({tab[i].se,1,i,tab[i].se});
}
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==1){
dp[sweep[i].idx][0]=query(0,0,sweep.size()-1,pos[sweep[i].idx].fi,pos[sweep[i].idx].se).se;
}
}
/*for (int i = 0; i < n; ++i)
{
cout <<dp[i][0]<<" ";
}cout <<endl;*/
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=1;
if(s==e){
cout <<0<<endl;
continue;
}else if(tab[e].se<=tab[s].fi){
cout <<"impossible"<<endl;
continue;
}
for (int i = 19; i >= 0; --i)
{
if(tab[dp[s][i]].se<tab[e].fi){
ans+=(1<<i);
s=dp[s][i];
}
}
if(tab[dp[s][0]].se<tab[e].fi) cout <<"impossible"<<endl;
else cout <<ans+1<<endl;
}
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:51:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int i = 0; i < sweep.size(); ++i)
| ~~^~~~~~~~~~~~~~
events.cpp:61:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int i = 0; i < sweep.size(); ++i)
| ~~^~~~~~~~~~~~~~
events.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
events.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
7 ms |
13148 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
8 ms |
13148 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
8 ms |
13148 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
8 ms |
13148 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
6 ms |
13244 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
7 ms |
13148 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |