#include<bits/stdc++.h>
#include<random>
#define ll long long
#define F first
#define S second
#define in insert
#define pb push_back
#define ppb pop_back()
#define d3 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define cans cout << ans << "\n";
#define yes cout << "Yes" << "\n";
#define no cout << "No" << "\n";
#define pll pair<ll,ll>
#define lin cout << "\n";
#define sqr 340
#define mod 1000000007
#define mid ((l+r)/2)
#define lc (2*x)
#define rc (2*x+1)
using namespace std;
const ll N = 100009;
ll n , q;
ll l[N] , r[N];
vector<ll> adj[N];
ll s , e;
bool vis[N];
ll sb[5009][5009];
ll ans;
void bfs(ll x , ll d , ll o)
{
queue<pll> qu;
qu.push({x,d});
while(!qu.empty())
{
pll nd = qu.front();
qu.pop();
if(vis[nd.F])
continue;
vis[nd.F]=1;
sb[o][nd.F]=nd.S;
for(auto it : adj[nd.F])
qu.push({it,nd.S+1});
}
}
void bfs(ll x , ll d)
{
queue<pll> qu;
qu.push({x,d});
while(!qu.empty())
{
pll nd = qu.front();
qu.pop();
if(vis[nd.F])
continue;
vis[nd.F]=1;
if(nd.F==e)
ans=nd.S;
for(auto it : adj[nd.F])
qu.push({it,nd.S+1});
}
}
int main()
{
d3
cin >> n >> q;
for(int i = 1 ; n>=i ; i++)
for(int j = 1 ; n>=j ; j++)
sb[i][j]=-1;
for(int i = 1 ; n>=i ; i++)
cin >> l[i] >> r[i];
for(int i = 1 ; n>=i ; i++)
{
for(int j = 1 ; n>=j ; j++)
{
if(i==j)
continue;
if(r[i]>=l[j]&&r[i]<=r[j])
adj[i].pb(j);
}
}
if(n<=5000)
{
for(int i = 1 ; n>=i ; i++)
{
for(int j = 1 ; n>=j ; j++)
vis[j]=0;
bfs(i,0,i);
}
}
while(q--)
{
cin >> s >> e;
ans=-1;
if(n<=5000)
ans=sb[s][e];
else
{
for(int i = 1 ; n>=i ; i++)
vis[i]=0;
bfs(s,0);
}
if(ans==-1)
cout << "impossible\n";
else
cans
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Runtime error |
328 ms |
406868 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2648 KB |
Output is correct |
3 |
Correct |
21 ms |
14760 KB |
Output is correct |
4 |
Correct |
14 ms |
14680 KB |
Output is correct |
5 |
Correct |
27 ms |
14680 KB |
Output is correct |
6 |
Correct |
383 ms |
16988 KB |
Output is correct |
7 |
Execution timed out |
1536 ms |
22980 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2648 KB |
Output is correct |
3 |
Correct |
21 ms |
14760 KB |
Output is correct |
4 |
Correct |
14 ms |
14680 KB |
Output is correct |
5 |
Correct |
27 ms |
14680 KB |
Output is correct |
6 |
Correct |
383 ms |
16988 KB |
Output is correct |
7 |
Execution timed out |
1536 ms |
22980 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2648 KB |
Output is correct |
3 |
Correct |
21 ms |
14760 KB |
Output is correct |
4 |
Correct |
14 ms |
14680 KB |
Output is correct |
5 |
Correct |
27 ms |
14680 KB |
Output is correct |
6 |
Correct |
383 ms |
16988 KB |
Output is correct |
7 |
Execution timed out |
1536 ms |
22980 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
321 ms |
406796 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Runtime error |
328 ms |
406868 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |