#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int) a.size()
#define all(a) a.begin(),a.end()
#define vec(...) vector<__VA_ARGS__>
#define _3M8yqhy ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
const int inf=1e9;
void slv(){
int n;
cin>>n;
vec(pii) a;
rep(i,n){
int l,r;
cin>>l>>r;
l-=1,r-=1;
a.pb({l,r});
}
vi pns(n,inf);
rep(t,2){
vi d1(n,inf);
{
vec(vi) adj(n);
rep(i,n){
rng(j,a[i].fi,a[i].se+1){
adj[j].pb(i);
}
}
d1[0]=0;
queue<int> que;
que.push(0);
while(sz(que)){
int v=que.front();
que.pop();
for(auto u:adj[v]){
if(d1[u]==inf){
d1[u]=d1[v]+1;
que.push(u);
}
}
}
}
vi d12(n,inf);
{
rep(s,n){
auto [l,r]=a[s];
d12[s]=d1[s];
int i=0,nr=r;
int cnt=0;
while(i<=n-1){
if(i<n-1 and r==i){
if(nr==r){
cnt=inf;
break;
}
r=nr;
cnt+=1;
}
nr=max(nr,a[i].se);
i+=1;
}
d12[s]+=cnt;
d12[s]=min(d12[s],inf);
}
}
vi dp(n,inf);
dp=d12;
{
vec(vi) adj(n);
rep(i,n){
rng(j,a[i].fi,a[i].se+1){
adj[j].pb(i);
}
}
priority_queue<pii,vec(pii),greater<pii>> pq;
rep(v,n){
pq.push({dp[v],v});
}
while(sz(pq)){
auto [cosu,v]=pq.top();
pq.pop();
for(auto u:adj[v]){
if(dp[u]>dp[v]+1){
dp[u]=dp[v]+1;
pq.push({dp[u],u});
}
}
}
}
rep(v,n){
if(!t){
pns[v]=min(pns[v],dp[v]);
}else{
pns[n-1-v]=min(pns[n-1-v],dp[v]);
}
}
rep(i,n){
a[i].fi=n-1-a[i].fi;
a[i].se=n-1-a[i].se;
swap(a[i].fi,a[i].se);
}
reverse(all(a));
}
int q;
cin>>q;
rep(i,q){
int x;
cin>>x;
x-=1;
cout<<(pns[x]==inf?-1:pns[x])<<"\n";
}
}
signed main(){
_3M8yqhy;
slv();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |