This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 << "impossbile\n";
else
cans
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |