#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
#define int long long
using namespace std;
inline void chmin(int& a,int b){
a=min(a,b);
}
typedef pair<int,int> pii;
struct TC{
int c,p,a,b;
};
struct ST{
struct no{
vector<int> s;
int vis=0;
};
vector<no> st;
void init(int x){
st.resize(4*x);
}
void update(int v,int L,int R,int l,int r,int a){
if(L==l && r==R){
st[v].s.push_back(a);
return;
}
int m=(L+R)/2;
if(r<=m){
update(2*v+1,L,m,l,r,a);
}
else if(l>m){
update(2*v+2,m+1,R,l,r,a);
}
else{
update(2*v+1,L,m,l,m,a);
update(2*v+2,m+1,R,m+1,r,a);
}
}
void find(int v,int L,int R,int t,int g,vector<int>& a){
if(st[v].vis!=g){
st[v].vis=g;
a.insert(a.end(),st[v].s.begin(),st[v].s.end());
}
if(L==R){
return;
}
int m=(L+R)/2;
if(t<=m){
find(2*v+1,L,m,t,g,a);
}
else{
find(2*v+2,m+1,R,t,g,a);
}
}
}st;
const int inf=1e18;
signed main(){
AquA;
int n,k;
cin >> n;
k=n;
vector<TC> v(k);
for(int i=0;i<k;i++){
v[i].c=i;
v[i].p=1;
cin >> v[i].a >> v[i].b;
v[i].a--;
v[i].b--;
}
st.init(n);
for(int i=0;i<k;i++){
st.update(0,0,n-1,v[i].a,v[i].b,i+n);
}
auto dijk=[&](vector<int>& dis,int g){
p_q<pii,vector<pii>,greater<pii> > pq;
for(int i=0;i<n+k;i++){
pq.push({dis[i],i});
}
while(pq.size()){
auto h=pq.top();
pq.pop();
if(dis[h.sc]!=h.fs){
continue;
}
if(h.sc>=n){
int r=h.sc-n;
if(dis[v[r].c]>h.fs+v[r].p){
dis[v[r].c]=h.fs+v[r].p;
pq.push({dis[v[r].c],v[r].c});
}
}
else{
vector<int> e;
st.find(0,0,n-1,h.sc,g,e);
for(auto y:e){
if(dis[y]>h.fs){
dis[y]=h.fs;
pq.push({dis[y],y});
}
}
}
}
};
vector<int> a(n+k,inf),b(n+k,inf),ans(n+k);
a[0]=0;
dijk(a,1);
b[n-1]=0;
dijk(b,2);
for(int i=0;i<n+k;i++){
ans[i]=a[i]+b[i];
}
for(int i=n;i<n+k;i++){
chmin(ans[v[i-n].c],ans[i]+v[i-n].p);
}
dijk(ans,3);
int q;
cin >> q;
while(q--){
int a;
cin >> a;
a--;
if(ans[a]>=inf){
cout << -1 << "\n";
}
else{
cout << ans[a] << "\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
635 ms |
91316 KB |
Output is correct |
5 |
Correct |
391 ms |
70780 KB |
Output is correct |
6 |
Correct |
293 ms |
68416 KB |
Output is correct |
7 |
Correct |
271 ms |
68772 KB |
Output is correct |
8 |
Correct |
210 ms |
59684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
320 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
320 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
1300 KB |
Output is correct |
17 |
Correct |
4 ms |
1204 KB |
Output is correct |
18 |
Correct |
5 ms |
1420 KB |
Output is correct |
19 |
Correct |
5 ms |
1412 KB |
Output is correct |
20 |
Correct |
3 ms |
1156 KB |
Output is correct |
21 |
Correct |
4 ms |
1236 KB |
Output is correct |
22 |
Correct |
3 ms |
1108 KB |
Output is correct |
23 |
Correct |
4 ms |
1264 KB |
Output is correct |
24 |
Correct |
4 ms |
1348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
320 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
1300 KB |
Output is correct |
17 |
Correct |
4 ms |
1204 KB |
Output is correct |
18 |
Correct |
5 ms |
1420 KB |
Output is correct |
19 |
Correct |
5 ms |
1412 KB |
Output is correct |
20 |
Correct |
3 ms |
1156 KB |
Output is correct |
21 |
Correct |
4 ms |
1236 KB |
Output is correct |
22 |
Correct |
3 ms |
1108 KB |
Output is correct |
23 |
Correct |
4 ms |
1264 KB |
Output is correct |
24 |
Correct |
4 ms |
1348 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
320 KB |
Output is correct |
27 |
Correct |
5 ms |
1276 KB |
Output is correct |
28 |
Correct |
5 ms |
1236 KB |
Output is correct |
29 |
Correct |
4 ms |
1204 KB |
Output is correct |
30 |
Correct |
5 ms |
1272 KB |
Output is correct |
31 |
Correct |
6 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
635 ms |
91316 KB |
Output is correct |
5 |
Correct |
391 ms |
70780 KB |
Output is correct |
6 |
Correct |
293 ms |
68416 KB |
Output is correct |
7 |
Correct |
271 ms |
68772 KB |
Output is correct |
8 |
Correct |
210 ms |
59684 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
320 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
6 ms |
1300 KB |
Output is correct |
25 |
Correct |
4 ms |
1204 KB |
Output is correct |
26 |
Correct |
5 ms |
1420 KB |
Output is correct |
27 |
Correct |
5 ms |
1412 KB |
Output is correct |
28 |
Correct |
3 ms |
1156 KB |
Output is correct |
29 |
Correct |
4 ms |
1236 KB |
Output is correct |
30 |
Correct |
3 ms |
1108 KB |
Output is correct |
31 |
Correct |
4 ms |
1264 KB |
Output is correct |
32 |
Correct |
4 ms |
1348 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
320 KB |
Output is correct |
35 |
Correct |
5 ms |
1276 KB |
Output is correct |
36 |
Correct |
5 ms |
1236 KB |
Output is correct |
37 |
Correct |
4 ms |
1204 KB |
Output is correct |
38 |
Correct |
5 ms |
1272 KB |
Output is correct |
39 |
Correct |
6 ms |
1236 KB |
Output is correct |
40 |
Correct |
684 ms |
100296 KB |
Output is correct |
41 |
Correct |
471 ms |
71468 KB |
Output is correct |
42 |
Correct |
500 ms |
93844 KB |
Output is correct |
43 |
Correct |
517 ms |
95168 KB |
Output is correct |
44 |
Correct |
332 ms |
68612 KB |
Output is correct |
45 |
Correct |
396 ms |
73244 KB |
Output is correct |
46 |
Correct |
151 ms |
28216 KB |
Output is correct |
47 |
Correct |
417 ms |
75868 KB |
Output is correct |
48 |
Correct |
413 ms |
80280 KB |
Output is correct |