#include <bits/stdc++.h>
using namespace std;
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define all(x) begin(x), end(x)
#define sz(x) (int)size(x)
#define pb push_back
#define ff first
#define ss second
typedef long long ll;
typedef pair<ll, ll> pp;
void dbg(){
cerr << endl;
}
template<typename H, typename... T> void dbg(H h, T... t){
cerr << h << ", ";
dbg(t...);
}
void IOS(){
cin.tie(0) -> sync_with_stdio(0);
#ifndef ONLINE_JUDGE
// freopen("inp.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#define er(...) cerr << __LINE__ << " <" << #__VA_ARGS__ << ">: ", dbg(__VA_ARGS__)
#else
#define er(...) 0
#endif
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 6e5 + 5, lg = 21, inf = ll(1e18) + 5;
vector<pp> adj[maxn];
int n, t;
ll dist[3][maxn];
void build(){
rep(i,2,n<<1){
adj[i+t].pb({(i>>1)+t, 0});
}
rep(i,n,n<<1){
adj[i-n].pb({i+t, 0});
}
}
void addEdge(int i, int l, int r, int w){
for(l += n, r += n; l < r; l >>= 1, r >>= 1){
if(l&1) adj[l+t].pb({i, w}), l++;
if(r&1) r--, adj[r+t].pb({i, w});
}
}
void dij(ll* dist, int st){
priority_queue<pp, vector<pp>, greater<pp>> q;
if(st == -1){
rep(i,0,maxn) q.push({dist[i], i});
} else{
fill(dist, dist + maxn, inf);
q.push({dist[st] = 0, st});
}
while(sz(q)){
auto[w, r] = q.top(); q.pop();
if(w - dist[r] || w >= inf) continue;
for(auto[c, d]: adj[r]) if(dist[c] > w + d){
q.push({dist[c] = w + d, c});
}
}
}
int P[maxn];
int main(){ IOS();
cin >> n;
t = n<<1;
build();
fill(dist[2], dist[2] + maxn, inf);
rep(i,0,n){
int a, b; cin >> a >> b; a--;
adj[i + n].pb({i, 0});
adj[i].pb({i + n, 1});
addEdge(i + n, a, b, 1);
}
dij(dist[0], 0);
dij(dist[1], n-1);
rep(i,n,t){
dist[2][i] = dist[0][i] + dist[1][i] - 1;
}
dij(dist[2], -1);
int q; cin >> q;
rep(i,0,q){
int x; cin >> x; x--;
cout << (dist[2][x] >= inf? -1: dist[2][x]) << '\n';
}
return 0-0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
44984 KB |
Output is correct |
2 |
Correct |
100 ms |
44948 KB |
Output is correct |
3 |
Correct |
101 ms |
45000 KB |
Output is correct |
4 |
Runtime error |
36 ms |
42292 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
44988 KB |
Output is correct |
2 |
Correct |
98 ms |
45016 KB |
Output is correct |
3 |
Correct |
100 ms |
44988 KB |
Output is correct |
4 |
Correct |
94 ms |
44980 KB |
Output is correct |
5 |
Correct |
95 ms |
45040 KB |
Output is correct |
6 |
Correct |
94 ms |
44992 KB |
Output is correct |
7 |
Correct |
102 ms |
45068 KB |
Output is correct |
8 |
Correct |
102 ms |
45100 KB |
Output is correct |
9 |
Correct |
100 ms |
44984 KB |
Output is correct |
10 |
Correct |
96 ms |
45000 KB |
Output is correct |
11 |
Correct |
98 ms |
45116 KB |
Output is correct |
12 |
Correct |
105 ms |
45116 KB |
Output is correct |
13 |
Correct |
94 ms |
45108 KB |
Output is correct |
14 |
Correct |
95 ms |
45052 KB |
Output is correct |
15 |
Correct |
99 ms |
45128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
44988 KB |
Output is correct |
2 |
Correct |
98 ms |
45016 KB |
Output is correct |
3 |
Correct |
100 ms |
44988 KB |
Output is correct |
4 |
Correct |
94 ms |
44980 KB |
Output is correct |
5 |
Correct |
95 ms |
45040 KB |
Output is correct |
6 |
Correct |
94 ms |
44992 KB |
Output is correct |
7 |
Correct |
102 ms |
45068 KB |
Output is correct |
8 |
Correct |
102 ms |
45100 KB |
Output is correct |
9 |
Correct |
100 ms |
44984 KB |
Output is correct |
10 |
Correct |
96 ms |
45000 KB |
Output is correct |
11 |
Correct |
98 ms |
45116 KB |
Output is correct |
12 |
Correct |
105 ms |
45116 KB |
Output is correct |
13 |
Correct |
94 ms |
45108 KB |
Output is correct |
14 |
Correct |
95 ms |
45052 KB |
Output is correct |
15 |
Correct |
99 ms |
45128 KB |
Output is correct |
16 |
Correct |
104 ms |
45768 KB |
Output is correct |
17 |
Correct |
104 ms |
45636 KB |
Output is correct |
18 |
Correct |
100 ms |
46032 KB |
Output is correct |
19 |
Correct |
101 ms |
45968 KB |
Output is correct |
20 |
Correct |
99 ms |
45476 KB |
Output is correct |
21 |
Correct |
104 ms |
45612 KB |
Output is correct |
22 |
Correct |
99 ms |
46016 KB |
Output is correct |
23 |
Correct |
104 ms |
45764 KB |
Output is correct |
24 |
Correct |
101 ms |
45684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
44988 KB |
Output is correct |
2 |
Correct |
98 ms |
45016 KB |
Output is correct |
3 |
Correct |
100 ms |
44988 KB |
Output is correct |
4 |
Correct |
94 ms |
44980 KB |
Output is correct |
5 |
Correct |
95 ms |
45040 KB |
Output is correct |
6 |
Correct |
94 ms |
44992 KB |
Output is correct |
7 |
Correct |
102 ms |
45068 KB |
Output is correct |
8 |
Correct |
102 ms |
45100 KB |
Output is correct |
9 |
Correct |
100 ms |
44984 KB |
Output is correct |
10 |
Correct |
96 ms |
45000 KB |
Output is correct |
11 |
Correct |
98 ms |
45116 KB |
Output is correct |
12 |
Correct |
105 ms |
45116 KB |
Output is correct |
13 |
Correct |
94 ms |
45108 KB |
Output is correct |
14 |
Correct |
95 ms |
45052 KB |
Output is correct |
15 |
Correct |
99 ms |
45128 KB |
Output is correct |
16 |
Correct |
104 ms |
45768 KB |
Output is correct |
17 |
Correct |
104 ms |
45636 KB |
Output is correct |
18 |
Correct |
100 ms |
46032 KB |
Output is correct |
19 |
Correct |
101 ms |
45968 KB |
Output is correct |
20 |
Correct |
99 ms |
45476 KB |
Output is correct |
21 |
Correct |
104 ms |
45612 KB |
Output is correct |
22 |
Correct |
99 ms |
46016 KB |
Output is correct |
23 |
Correct |
104 ms |
45764 KB |
Output is correct |
24 |
Correct |
101 ms |
45684 KB |
Output is correct |
25 |
Correct |
97 ms |
45004 KB |
Output is correct |
26 |
Correct |
100 ms |
45000 KB |
Output is correct |
27 |
Correct |
106 ms |
45820 KB |
Output is correct |
28 |
Correct |
100 ms |
45620 KB |
Output is correct |
29 |
Correct |
102 ms |
45520 KB |
Output is correct |
30 |
Correct |
100 ms |
45644 KB |
Output is correct |
31 |
Correct |
100 ms |
45664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
44984 KB |
Output is correct |
2 |
Correct |
100 ms |
44948 KB |
Output is correct |
3 |
Correct |
101 ms |
45000 KB |
Output is correct |
4 |
Runtime error |
36 ms |
42292 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |