#include<bits/stdc++.h>
#define ll long long
#define ntr "\n"
#define mod (ll)(1e9+7)
#define taskname "temp"
#define frep freopen(taskname".inp","r",stdin); freopen(taskname".out","w",stdout);
using namespace std;
vector<pair<ll,ll>> adj[800100];
ll id[200010];
void build(ll pos,ll l,ll r){
if(l==r){
id[l]=pos;
}
else{
ll mid=(l+r)/2;
build(2*pos,l,mid);
build(2*pos+1,mid+1,r);
adj[2*pos].push_back({pos,0});
adj[2*pos+1].push_back({pos,0});
}
}
void add(ll pos,ll l,ll r,ll u,ll v,ll x){
if(v<l||u>r) return ;
else if(u<=l&&r<=v){
adj[pos].push_back({id[x],1});
}
else{
ll mid=(l+r)/2;
add(2*pos,l,mid,u,v,x);
add(2*pos+1,mid+1,r,u,v,x);
}
}
ll n,q;
ll ans[800010];
ll dist[800010];
ll vis[800010];
ll a[200010];
ll b[200010];
ll l[200001];
ll r[200001];
void proc(ll st){
fill(dist+1,dist+4*n+100,1e18);
fill(vis+1,vis+4*n+100,0);
deque<ll> dq;
dq.push_front(id[st]);
dist[id[st]]=0;
while(!dq.empty()){
ll u=dq.front();
dq.pop_front();
if(vis[u]) continue;
for(auto [v,w]:adj[u]){
if(dist[v]>dist[u]+w){
dist[v]=dist[u]+w;
if(w==0){
dq.push_front(v);
}
else{
dq.push_back(v);
}
}
}
}
dist[id[st]]=1;
for(int i=1;i<=n;i++){
if(st==1) a[i]=dist[id[i]];
if(st==n) b[i]=dist[id[i]];
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//frep;
cin>>n;
build(1,1,n);
for(int i=1;i<=n;i++){
cin>>l[i]>>r[i];
add(1,1,n,l[i],r[i],i);
}
proc(1);
proc(n);
// for(int i=1;i<=n;i++){
// cout<<a[i]<<' ';
// }
// cout<<ntr;
// for(int i=1;i<=n;i++){
// cout<<b[i]<<' ';
// }
// cout<<ntr;
// for(int i=1;i<=n+1;i++){
// cout<<id[i]<<' ';
// }
// cout<<ntr;
// cout<<ntr;
// for(auto [v,w]:adj[id[n+1]]){
// cout<<id[n+1]<<' '<<v<<' '<<w<<ntr;
// }
priority_queue<pair<ll,ll>> pq;
fill(dist+1,dist+4*n+10,1e18);
fill(vis+1,vis+4*n+10,0);
for(int i=1;i<=n;i++){
dist[id[i]]=a[i]+b[i];
if(dist[id[i]]<=1e18){
pq.push({-dist[id[i]],id[i]});
}
}
while(!pq.empty()){
ll u=pq.top().second;
pq.pop();
if(vis[u]) continue;
vis[u]=1;
for(auto [v,w]:adj[u]){
if(dist[v]>dist[u]+w){
dist[v]=dist[u]+w;
pq.push({-dist[v],v});
}
}
}
for(int i=1;i<=n;i++){
ans[i]=dist[id[i]];
}
cin>>q;
for(int i=1;i<=q;i++){
ll x;
cin>>x;
cout<<(ans[x]<1e18 ? ans[x]-1 : -1)<<ntr;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19288 KB |
Output is correct |
2 |
Correct |
9 ms |
19352 KB |
Output is correct |
3 |
Correct |
9 ms |
19292 KB |
Output is correct |
4 |
Correct |
555 ms |
117704 KB |
Output is correct |
5 |
Correct |
256 ms |
76988 KB |
Output is correct |
6 |
Correct |
165 ms |
67000 KB |
Output is correct |
7 |
Correct |
107 ms |
62140 KB |
Output is correct |
8 |
Correct |
112 ms |
72884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19308 KB |
Output is correct |
2 |
Correct |
9 ms |
19292 KB |
Output is correct |
3 |
Correct |
9 ms |
19292 KB |
Output is correct |
4 |
Correct |
8 ms |
19084 KB |
Output is correct |
5 |
Correct |
8 ms |
19292 KB |
Output is correct |
6 |
Correct |
8 ms |
19292 KB |
Output is correct |
7 |
Correct |
8 ms |
19292 KB |
Output is correct |
8 |
Correct |
11 ms |
19292 KB |
Output is correct |
9 |
Correct |
8 ms |
19176 KB |
Output is correct |
10 |
Correct |
9 ms |
19104 KB |
Output is correct |
11 |
Correct |
11 ms |
19196 KB |
Output is correct |
12 |
Correct |
9 ms |
19292 KB |
Output is correct |
13 |
Correct |
9 ms |
19292 KB |
Output is correct |
14 |
Correct |
9 ms |
19288 KB |
Output is correct |
15 |
Correct |
9 ms |
19292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19308 KB |
Output is correct |
2 |
Correct |
9 ms |
19292 KB |
Output is correct |
3 |
Correct |
9 ms |
19292 KB |
Output is correct |
4 |
Correct |
8 ms |
19084 KB |
Output is correct |
5 |
Correct |
8 ms |
19292 KB |
Output is correct |
6 |
Correct |
8 ms |
19292 KB |
Output is correct |
7 |
Correct |
8 ms |
19292 KB |
Output is correct |
8 |
Correct |
11 ms |
19292 KB |
Output is correct |
9 |
Correct |
8 ms |
19176 KB |
Output is correct |
10 |
Correct |
9 ms |
19104 KB |
Output is correct |
11 |
Correct |
11 ms |
19196 KB |
Output is correct |
12 |
Correct |
9 ms |
19292 KB |
Output is correct |
13 |
Correct |
9 ms |
19292 KB |
Output is correct |
14 |
Correct |
9 ms |
19288 KB |
Output is correct |
15 |
Correct |
9 ms |
19292 KB |
Output is correct |
16 |
Correct |
12 ms |
20060 KB |
Output is correct |
17 |
Correct |
11 ms |
19892 KB |
Output is correct |
18 |
Correct |
11 ms |
20568 KB |
Output is correct |
19 |
Correct |
11 ms |
20316 KB |
Output is correct |
20 |
Correct |
10 ms |
19804 KB |
Output is correct |
21 |
Correct |
10 ms |
19880 KB |
Output is correct |
22 |
Correct |
9 ms |
19800 KB |
Output is correct |
23 |
Correct |
11 ms |
20116 KB |
Output is correct |
24 |
Correct |
10 ms |
20036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19308 KB |
Output is correct |
2 |
Correct |
9 ms |
19292 KB |
Output is correct |
3 |
Correct |
9 ms |
19292 KB |
Output is correct |
4 |
Correct |
8 ms |
19084 KB |
Output is correct |
5 |
Correct |
8 ms |
19292 KB |
Output is correct |
6 |
Correct |
8 ms |
19292 KB |
Output is correct |
7 |
Correct |
8 ms |
19292 KB |
Output is correct |
8 |
Correct |
11 ms |
19292 KB |
Output is correct |
9 |
Correct |
8 ms |
19176 KB |
Output is correct |
10 |
Correct |
9 ms |
19104 KB |
Output is correct |
11 |
Correct |
11 ms |
19196 KB |
Output is correct |
12 |
Correct |
9 ms |
19292 KB |
Output is correct |
13 |
Correct |
9 ms |
19292 KB |
Output is correct |
14 |
Correct |
9 ms |
19288 KB |
Output is correct |
15 |
Correct |
9 ms |
19292 KB |
Output is correct |
16 |
Correct |
12 ms |
20060 KB |
Output is correct |
17 |
Correct |
11 ms |
19892 KB |
Output is correct |
18 |
Correct |
11 ms |
20568 KB |
Output is correct |
19 |
Correct |
11 ms |
20316 KB |
Output is correct |
20 |
Correct |
10 ms |
19804 KB |
Output is correct |
21 |
Correct |
10 ms |
19880 KB |
Output is correct |
22 |
Correct |
9 ms |
19800 KB |
Output is correct |
23 |
Correct |
11 ms |
20116 KB |
Output is correct |
24 |
Correct |
10 ms |
20036 KB |
Output is correct |
25 |
Correct |
8 ms |
19288 KB |
Output is correct |
26 |
Correct |
8 ms |
19292 KB |
Output is correct |
27 |
Correct |
14 ms |
20316 KB |
Output is correct |
28 |
Correct |
12 ms |
20060 KB |
Output is correct |
29 |
Correct |
10 ms |
19804 KB |
Output is correct |
30 |
Correct |
12 ms |
20036 KB |
Output is correct |
31 |
Correct |
11 ms |
19988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19288 KB |
Output is correct |
2 |
Correct |
9 ms |
19352 KB |
Output is correct |
3 |
Correct |
9 ms |
19292 KB |
Output is correct |
4 |
Correct |
555 ms |
117704 KB |
Output is correct |
5 |
Correct |
256 ms |
76988 KB |
Output is correct |
6 |
Correct |
165 ms |
67000 KB |
Output is correct |
7 |
Correct |
107 ms |
62140 KB |
Output is correct |
8 |
Correct |
112 ms |
72884 KB |
Output is correct |
9 |
Correct |
9 ms |
19308 KB |
Output is correct |
10 |
Correct |
9 ms |
19292 KB |
Output is correct |
11 |
Correct |
9 ms |
19292 KB |
Output is correct |
12 |
Correct |
8 ms |
19084 KB |
Output is correct |
13 |
Correct |
8 ms |
19292 KB |
Output is correct |
14 |
Correct |
8 ms |
19292 KB |
Output is correct |
15 |
Correct |
8 ms |
19292 KB |
Output is correct |
16 |
Correct |
11 ms |
19292 KB |
Output is correct |
17 |
Correct |
8 ms |
19176 KB |
Output is correct |
18 |
Correct |
9 ms |
19104 KB |
Output is correct |
19 |
Correct |
11 ms |
19196 KB |
Output is correct |
20 |
Correct |
9 ms |
19292 KB |
Output is correct |
21 |
Correct |
9 ms |
19292 KB |
Output is correct |
22 |
Correct |
9 ms |
19288 KB |
Output is correct |
23 |
Correct |
9 ms |
19292 KB |
Output is correct |
24 |
Correct |
12 ms |
20060 KB |
Output is correct |
25 |
Correct |
11 ms |
19892 KB |
Output is correct |
26 |
Correct |
11 ms |
20568 KB |
Output is correct |
27 |
Correct |
11 ms |
20316 KB |
Output is correct |
28 |
Correct |
10 ms |
19804 KB |
Output is correct |
29 |
Correct |
10 ms |
19880 KB |
Output is correct |
30 |
Correct |
9 ms |
19800 KB |
Output is correct |
31 |
Correct |
11 ms |
20116 KB |
Output is correct |
32 |
Correct |
10 ms |
20036 KB |
Output is correct |
33 |
Correct |
8 ms |
19288 KB |
Output is correct |
34 |
Correct |
8 ms |
19292 KB |
Output is correct |
35 |
Correct |
14 ms |
20316 KB |
Output is correct |
36 |
Correct |
12 ms |
20060 KB |
Output is correct |
37 |
Correct |
10 ms |
19804 KB |
Output is correct |
38 |
Correct |
12 ms |
20036 KB |
Output is correct |
39 |
Correct |
11 ms |
19988 KB |
Output is correct |
40 |
Correct |
582 ms |
121788 KB |
Output is correct |
41 |
Correct |
301 ms |
80156 KB |
Output is correct |
42 |
Correct |
400 ms |
131712 KB |
Output is correct |
43 |
Incorrect |
224 ms |
128252 KB |
Output isn't correct |
44 |
Halted |
0 ms |
0 KB |
- |