///#include "art.h"
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5;
const long long mod = 1e9+7;
const int logn=30;
int main()
{
ios::sync_with_stdio(false);
int n,q;
cin>>n>>q;
vector<pair<int,int>>v;
for(int i=0;i<n;i++)
{
int x,y;
cin>>x>>y;
v.push_back({x,y});
}
vector<int>g[n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(j==i)continue;
if(v[j].first<=v[i].second)
{
if(v[j].second>=v[i].second)
{
g[i].push_back(j);
}
}
}
}
while(q--)
{
int ans=-1;
int l,r;
cin>>l>>r;
l--;r--;
bool vis[n];
memset(vis,0,sizeof vis);
queue<int>Q;
Q.push(l);
Q.push(0);
vis[l]=true;
while(!Q.empty())
{
int teme=Q.front();
Q.pop();
int k=Q.front();Q.pop();
if(teme==r)
{
ans=k;
break;
}
for(auto ax:g[teme])
{
if(vis[ax])continue;
Q.push(ax);
Q.push(k+1);
vis[ax]=true;
}
}
if(ans==-1)
{
cout<<"impossible"<<endl;
}
else
{
cout<<ans<<endl;
}
}
return 0;
}
/*
void solve(int n)
{
if(n<=6)
{
vector<int>v;
for(int i=1;i<=n;i++)
{
v.push_back(i);
}
do
{
int kol=publish(v);
if(kol==0)
{
answer(v);
}
}while(next_permutation(v.begin(),v.end()));
}
/// publish(order);
/// answer(order);
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
1553 ms |
5708 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
5 ms |
604 KB |
Output is correct |
6 |
Correct |
8 ms |
1116 KB |
Output is correct |
7 |
Correct |
15 ms |
1884 KB |
Output is correct |
8 |
Correct |
16 ms |
2964 KB |
Output is correct |
9 |
Correct |
47 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
5 ms |
604 KB |
Output is correct |
6 |
Correct |
8 ms |
1116 KB |
Output is correct |
7 |
Correct |
15 ms |
1884 KB |
Output is correct |
8 |
Correct |
16 ms |
2964 KB |
Output is correct |
9 |
Correct |
47 ms |
4440 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
452 KB |
Output is correct |
12 |
Correct |
5 ms |
348 KB |
Output is correct |
13 |
Correct |
4 ms |
348 KB |
Output is correct |
14 |
Correct |
4 ms |
456 KB |
Output is correct |
15 |
Correct |
8 ms |
1116 KB |
Output is correct |
16 |
Correct |
16 ms |
1884 KB |
Output is correct |
17 |
Correct |
22 ms |
3164 KB |
Output is correct |
18 |
Correct |
47 ms |
4444 KB |
Output is correct |
19 |
Execution timed out |
1525 ms |
30584 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
5 ms |
604 KB |
Output is correct |
6 |
Correct |
8 ms |
1116 KB |
Output is correct |
7 |
Correct |
15 ms |
1884 KB |
Output is correct |
8 |
Correct |
16 ms |
2964 KB |
Output is correct |
9 |
Correct |
47 ms |
4440 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
536 KB |
Output is correct |
13 |
Correct |
4 ms |
348 KB |
Output is correct |
14 |
Correct |
4 ms |
468 KB |
Output is correct |
15 |
Correct |
7 ms |
1304 KB |
Output is correct |
16 |
Correct |
21 ms |
1884 KB |
Output is correct |
17 |
Correct |
16 ms |
3164 KB |
Output is correct |
18 |
Correct |
47 ms |
4444 KB |
Output is correct |
19 |
Execution timed out |
1552 ms |
5584 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1550 ms |
5848 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
1553 ms |
5708 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |