#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];
map<ll,pll> mx;
vector<ll> adj[N];
vector<pll> v;
bool vis[N];
ll idx[N];
ll g[N];
ll c;
map<ll,vector<ll>> mp;
void dfs(ll x , ll id)
{
if(vis[x])
return;
vis[x]=1;
idx[x]=id;
g[x]=c;
ll ne = -1;
if(mx[r[x]].F>r[x])
ne=mx[r[x]].S;
if(mp[r[x]].size()==2)
{
if(mp[r[x]][0]==x)
ne=mp[r[x]][1];
else
ne=mp[r[x]][0];
}
if(ne!=-1)
dfs(ne,id+1);
}
int main()
{
d3
cin >> n >> q;
for(int i = 1 ; n>=i ; i++)
{
cin >> l[i] >> r[i];
if(mx[l[i]].F<r[i])
mx[l[i]]={r[i],i};
mx[r[i]]={0,0};
v.pb({r[i],i});
mp[r[i]].pb(i);
}
ll pmx = 0;
ll pidx = 0;
for(auto it : mx)
{
if(it.S.F<pmx)
{
mx[it.F]={pmx,pidx};
}
else
{
pmx=it.S.F;
pidx=it.S.S;
}
}
sort(v.begin(),v.end());
for(auto it : v)
{
if(vis[it.S])
continue;
dfs(it.S,0);
c++;
}
while(q--)
{
ll s , e;
cin >> s >> e;
if(s==e)
{
cout << 0 << "\n";
continue;
}
if(r[s]==r[e])
{
cout << 1 << "\n";
continue;
}
if(g[s]!=g[e]||idx[s]>idx[e])
{
cout << "impossible\n";
continue;
}
cout << idx[e]-idx[s] << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Incorrect |
163 ms |
29008 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
155 ms |
28864 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Incorrect |
163 ms |
29008 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |