#include <bits/stdc++.h>
#define MAX 10005
int N,Q;
typedef std::pair<int,int> pii;
std::vector<pii> eventos;
std::vector<int> add[MAX];
std::vector<int> rem[MAX];
int catalogo_avanco[MAX];
bool moscando[MAX];
int visitado[MAX];
void explora(int x){
memset(&catalogo_avanco,0,sizeof(catalogo_avanco));
memset(&visitado,0,sizeof(visitado));
memset(&moscando,0,sizeof(moscando));
catalogo_avanco[eventos[x].second]=1;
for(int i=0;i!=MAX;++i){
for(auto&x:add[i]){
moscando[x]=true;
}
int d = catalogo_avanco[i];
if(d){
for(int i=0;i!=N;++i){
auto& x = moscando[i];
if(!x)continue;
if(d){
if(!visitado[i])
visitado[i]=d+1;
else visitado[i]=std::min(visitado[i],d+1);
}
// std::cout<<"Visita "<<x.first<<" "<<d<<" "<<i<<"\n";
if(!catalogo_avanco[eventos[i].second])catalogo_avanco[eventos[i].second]=d+1;
else
catalogo_avanco[eventos[i].second]=std::min((int)catalogo_avanco[eventos[i].second],d+1);
}
}
for(auto&x:rem[i]){
moscando[x]=false;
}
}
}
typedef std::pair<int,int*> pipo;
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
std::cin>>N>>Q;
for(int i=0;i!=N;++i){
int a,b;
std::cin>>a>>b;
eventos.push_back({a,b});
}
{
std::vector<pipo> comprime;
for(auto&x:eventos){
comprime.push_back({x.first,&x.first});
comprime.push_back({x.second,&x.second});
}
std::sort(comprime.begin(),comprime.end());
int last=-1,cord=1;
for(auto&x:comprime){
if(x.first!=last){
last=x.first;
++cord;
}
*x.second=cord;
}
}
for(int i=0;i!=N;++i){
add[eventos[i].first].push_back(i);
rem[eventos[i].second].push_back(i);
}
for(int i=0;i!=Q;++i){
int s,e;
std::cin>>s>>e;--s;--e;
if(s==e){
std::cout<<"0\n";
continue;
}
explora(s);
if(visitado[e]){
std::cout<<visitado[e]-1<<"\n";
}else std::cout<<"impossible\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Runtime error |
49 ms |
5840 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
44 ms |
852 KB |
Output is correct |
4 |
Correct |
13 ms |
948 KB |
Output is correct |
5 |
Correct |
23 ms |
852 KB |
Output is correct |
6 |
Correct |
16 ms |
852 KB |
Output is correct |
7 |
Correct |
84 ms |
932 KB |
Output is correct |
8 |
Correct |
79 ms |
932 KB |
Output is correct |
9 |
Correct |
3 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
44 ms |
852 KB |
Output is correct |
4 |
Correct |
13 ms |
948 KB |
Output is correct |
5 |
Correct |
23 ms |
852 KB |
Output is correct |
6 |
Correct |
16 ms |
852 KB |
Output is correct |
7 |
Correct |
84 ms |
932 KB |
Output is correct |
8 |
Correct |
79 ms |
932 KB |
Output is correct |
9 |
Correct |
3 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
53 ms |
932 KB |
Output is correct |
13 |
Correct |
17 ms |
948 KB |
Output is correct |
14 |
Correct |
25 ms |
952 KB |
Output is correct |
15 |
Correct |
16 ms |
852 KB |
Output is correct |
16 |
Correct |
83 ms |
852 KB |
Output is correct |
17 |
Correct |
80 ms |
936 KB |
Output is correct |
18 |
Correct |
3 ms |
852 KB |
Output is correct |
19 |
Execution timed out |
1587 ms |
1236 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
44 ms |
852 KB |
Output is correct |
4 |
Correct |
13 ms |
948 KB |
Output is correct |
5 |
Correct |
23 ms |
852 KB |
Output is correct |
6 |
Correct |
16 ms |
852 KB |
Output is correct |
7 |
Correct |
84 ms |
932 KB |
Output is correct |
8 |
Correct |
79 ms |
932 KB |
Output is correct |
9 |
Correct |
3 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
44 ms |
932 KB |
Output is correct |
13 |
Correct |
13 ms |
852 KB |
Output is correct |
14 |
Correct |
24 ms |
972 KB |
Output is correct |
15 |
Correct |
16 ms |
852 KB |
Output is correct |
16 |
Correct |
82 ms |
852 KB |
Output is correct |
17 |
Correct |
79 ms |
852 KB |
Output is correct |
18 |
Correct |
3 ms |
852 KB |
Output is correct |
19 |
Runtime error |
39 ms |
5840 KB |
Execution killed with signal 11 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
40 ms |
5808 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Runtime error |
49 ms |
5840 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |