#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
const int MAX_N=1e5+10;
const int MAX=1e9;
vector<vi> G;
vii ev;
int dis[MAX_N];
int n;
bool isq[MAX_N];
void bfs(int u){
for(int i=0;i<n;i++) dis[i]=MAX;
dis[u]=0; isq[u]=1;
queue<int> q; q.push(u);
while(!q.empty()){
int x=q.front(); q.pop();
isq[x]=0;
for(auto &v:G[x]){
if(dis[v]>dis[x]+1){
dis[v]=dis[x]+1;
if(!isq[v]){
isq[v]=1;
q.push(v);
}
}
}
}
}
int con[MAX_N];
int ord[MAX_N];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int q; cin>>n>>q;
vector<iii> t1;
for(int i=0;i<n;i++){
int a,b; cin>>a>>b;
ev.push_back(ii(a,b));
t1.push_back(iii(ii(a,b),i));
}
memset(ord,-1,sizeof ord);
sort(t1.begin(),t1.end());
int c=0;
int o=0;
for(int i=0;i<n;i++) ord[t1[i].second]=o++;
for(int i=0;i<n-1;i++){
int u=t1[i].second;
con[u]=c;
if(!(t1[i+1].first.first<=t1[i].first.second && t1[i].first.second<=t1[i+1].first.second)){
c++;
}
}
con[t1[n-1].second]=c;
/*G.resize(n+1);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(ev[j].first<=ev[i].second && ev[i].second<=ev[j].second){
G[i].push_back(j);
}
}
}*/
while(q--){
int s,e; cin>>s>>e;
s--; e--;
bool im=0;
if(con[s]==con[e]){
if(ord[s]<ord[e]) cout<<ord[e]-ord[s]<<"\n";
else im=1;
}
else im=1;
if(im) cout<<"impossible\n";
/*bfs(s);
if(dis[e]>=MAX) cout<<"impossible\n";
else cout<<dis[e]<<endl;*/
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
4740 KB |
Output is correct |
2 |
Correct |
68 ms |
5276 KB |
Output is correct |
3 |
Correct |
63 ms |
5420 KB |
Output is correct |
4 |
Correct |
53 ms |
5756 KB |
Output is correct |
5 |
Incorrect |
60 ms |
5632 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |