#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});
}
auto get=[&](vi d,vec(pii) cover_ranges){
vec(vi) adj(n);
rep(v,n){
rng(j,cover_ranges[v].fi,cover_ranges[v].se+1){
adj[j].pb(v);
}
}
priority_queue<pii,vec(pii),greater<pii>> pq;
rep(v,n){
pq.push({d[v],v});
}
while(sz(pq)){
auto [cosu,v]=pq.top();
pq.pop();
if(d[v]!=cosu) continue;
for(auto u:adj[v]){
if(d[u]>d[v]){
d[u]=d[v]+1;
pq.push({d[u],u});
}
}
}
return d;
};
vi pns(n,inf);
// 12 way
rep(t,2){
vi d1(n,inf);
vec(pii) cover_ranges1;
{
rep(i,n){
cover_ranges1.pb({a[i].fi,a[i].se});
if(a[i].fi==0){
d1[i]=1;
}
}
}
vi d2(n,inf);
vec(pii) cover_ranges2;
{
rep(i,n){
cover_ranges2.pb({0,a[i].se});
if(a[i].se==n-1){
d2[i]=1;
}
}
}
d1=get(d1,cover_ranges1);
d2=get(d2,cover_ranges2);
vi d3(n,inf);
{
d3[0]=d2[0];
rng(v,1,n-1){
d3[v]=d1[v]+d2[v]-1;
}
d3[n-1]=d1[n-1];
}
vec(pii) cover_ranges3;
{
rep(i,n){
cover_ranges3.pb({a[i].fi,a[i].se});
}
}
d3=get(d3,cover_ranges3);
rep(v,n){
if(!t){
pns[v]=min(pns[v],d3[v]);
}else{
pns[n-1-v]=min(pns[n-1-v],d3[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;
print(pns[x]==inf?-1:pns[x]);
}
}
signed main(){
_3M8yqhy;
slv();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Runtime error |
1444 ms |
1048576 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
508 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
5 ms |
844 KB |
Output is correct |
12 |
Execution timed out |
2073 ms |
265092 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
508 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
5 ms |
844 KB |
Output is correct |
12 |
Execution timed out |
2073 ms |
265092 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
508 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
5 ms |
844 KB |
Output is correct |
12 |
Execution timed out |
2073 ms |
265092 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Runtime error |
1444 ms |
1048576 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |