#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){
int _n=n+5*n;
vec(vec(pii)) adj(_n);
auto build=[&](auto self,int node,int l,int r)->void{
if(l==r-1){
adj[l].pb({0,n+node});
return;
}
int m=(l+r)/2;
self(self,node*2+1,l,m);
self(self,node*2+2,m,r);
adj[node*2+1+n].pb({0,node+n});
adj[node*2+2+n].pb({0,node+n});
};
build(build,0,0,n);
auto add=[&](auto self,int node,int l,int r,int _l,int _r,int v)->void{
if(_l<=l and r<=_r){
adj[node+n].pb({1,v});
return;
}
if(l>=_r or r<=_l){
return;
}
int m=(l+r)/2;
self(self,node*2+1,l,m,_l,_r,v);
self(self,node*2+2,m,r,_l,_r,v);
};
rep(v,n){
add(add,0,0,n,cover_ranges[v].fi,cover_ranges[v].se+1,v);
}
vi dp(_n,inf);
rep(v,n){
dp[v]=d[v];
}
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();
if(dp[v]!=cosu) continue;
for(auto [w,u]:adj[v]){
if(dp[u]>dp[v]+w){
dp[u]=dp[v]+w;
pq.push({dp[u],u});
}
}
}
vi ret(n);
rep(v,n){
ret[v]=dp[v];
// print(ret[v]);
}
return ret;
};
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);
// break;
d2=get(d2,cover_ranges2);
// if(!t){
// print(d1[2],d2[2]);
// }
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 |
Correct |
1761 ms |
101972 KB |
Output is correct |
5 |
Correct |
1180 ms |
86848 KB |
Output is correct |
6 |
Correct |
891 ms |
87264 KB |
Output is correct |
7 |
Correct |
572 ms |
69828 KB |
Output is correct |
8 |
Correct |
406 ms |
76204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
336 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 |
2 ms |
336 KB |
Output is correct |
12 |
Correct |
2 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
592 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
2 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
336 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 |
2 ms |
336 KB |
Output is correct |
12 |
Correct |
2 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
592 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
2 ms |
336 KB |
Output is correct |
16 |
Correct |
11 ms |
1360 KB |
Output is correct |
17 |
Correct |
10 ms |
1508 KB |
Output is correct |
18 |
Correct |
17 ms |
1616 KB |
Output is correct |
19 |
Correct |
11 ms |
1616 KB |
Output is correct |
20 |
Correct |
8 ms |
1264 KB |
Output is correct |
21 |
Correct |
9 ms |
1520 KB |
Output is correct |
22 |
Correct |
5 ms |
1256 KB |
Output is correct |
23 |
Correct |
10 ms |
1360 KB |
Output is correct |
24 |
Correct |
9 ms |
1552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
336 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 |
2 ms |
336 KB |
Output is correct |
12 |
Correct |
2 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
592 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
2 ms |
336 KB |
Output is correct |
16 |
Correct |
11 ms |
1360 KB |
Output is correct |
17 |
Correct |
10 ms |
1508 KB |
Output is correct |
18 |
Correct |
17 ms |
1616 KB |
Output is correct |
19 |
Correct |
11 ms |
1616 KB |
Output is correct |
20 |
Correct |
8 ms |
1264 KB |
Output is correct |
21 |
Correct |
9 ms |
1520 KB |
Output is correct |
22 |
Correct |
5 ms |
1256 KB |
Output is correct |
23 |
Correct |
10 ms |
1360 KB |
Output is correct |
24 |
Correct |
9 ms |
1552 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
12 ms |
1528 KB |
Output is correct |
28 |
Correct |
11 ms |
1520 KB |
Output is correct |
29 |
Correct |
9 ms |
1520 KB |
Output is correct |
30 |
Correct |
9 ms |
1520 KB |
Output is correct |
31 |
Correct |
10 ms |
1360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1761 ms |
101972 KB |
Output is correct |
5 |
Correct |
1180 ms |
86848 KB |
Output is correct |
6 |
Correct |
891 ms |
87264 KB |
Output is correct |
7 |
Correct |
572 ms |
69828 KB |
Output is correct |
8 |
Correct |
406 ms |
76204 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
592 KB |
Output is correct |
21 |
Correct |
2 ms |
592 KB |
Output is correct |
22 |
Correct |
2 ms |
592 KB |
Output is correct |
23 |
Correct |
2 ms |
336 KB |
Output is correct |
24 |
Correct |
11 ms |
1360 KB |
Output is correct |
25 |
Correct |
10 ms |
1508 KB |
Output is correct |
26 |
Correct |
17 ms |
1616 KB |
Output is correct |
27 |
Correct |
11 ms |
1616 KB |
Output is correct |
28 |
Correct |
8 ms |
1264 KB |
Output is correct |
29 |
Correct |
9 ms |
1520 KB |
Output is correct |
30 |
Correct |
5 ms |
1256 KB |
Output is correct |
31 |
Correct |
10 ms |
1360 KB |
Output is correct |
32 |
Correct |
9 ms |
1552 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
12 ms |
1528 KB |
Output is correct |
36 |
Correct |
11 ms |
1520 KB |
Output is correct |
37 |
Correct |
9 ms |
1520 KB |
Output is correct |
38 |
Correct |
9 ms |
1520 KB |
Output is correct |
39 |
Correct |
10 ms |
1360 KB |
Output is correct |
40 |
Correct |
1802 ms |
104300 KB |
Output is correct |
41 |
Correct |
1174 ms |
92696 KB |
Output is correct |
42 |
Correct |
1416 ms |
112056 KB |
Output is correct |
43 |
Correct |
1391 ms |
115040 KB |
Output is correct |
44 |
Correct |
970 ms |
93152 KB |
Output is correct |
45 |
Correct |
1016 ms |
98220 KB |
Output is correct |
46 |
Correct |
360 ms |
34032 KB |
Output is correct |
47 |
Correct |
1217 ms |
90908 KB |
Output is correct |
48 |
Correct |
1071 ms |
102168 KB |
Output is correct |