#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
#endif // LOCAL
int N;
cin >> N;
vector<vector<pair<int, int>>> adj(N * 4);
vector<int> ln(N * 4), rn(N * 4);
int n = N;
function<int(int, int)> build = [&](int l, int r){
if(l == r) return l;
int mid = l + r >> 1;
int o = n++, lc = build(l, mid), rc = build(mid + 1, r);
ln[o] = lc;
rn[o] = rc;
adj[lc].emplace_back(o, 0);
adj[rc].emplace_back(o, 0);
return o;
};
function<void(int, int, int, int, int, int)> addEdge = [&](
int cur, int l, int r, int u, int v, int s){
if(u <= l && r <= v){
adj[cur].emplace_back(s, 1);
} else{
int mid = l + r >> 1;
if(u <= mid) addEdge(ln[cur], l, mid, u, v, s);
if(mid < v) addEdge(rn[cur], mid + 1, r, u, v, s);
}
};
int rt = build(0, N - 1);
for(int i = 0; i < N; ++i){
int l, r;
cin >> l >> r;
--l, --r;
addEdge(rt, 0, N - 1, l, r, i);
}
auto bfs_deque = [&](int s, vector<vector<pair<int, int>>>& adj) -> vector<int>{
vector<int> dist(4 * N, -1);
dist[s] = 0;
deque<int> dq;
dq.push_back(s);
while(!dq.empty()){
int u = dq.front(); dq.pop_front();
for(auto [v, w] : adj[u]){
if(dist[v] == -1){
dist[v] = dist[u] + w;
if(w == 0) dq.push_front(v);
else dq.push_back(v);
}
}
}
return dist;
};
vector<int> distS = bfs_deque(0, adj), distT = bfs_deque(N - 1, adj);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
vector<int> answer(N * 4, -1);
for(int i = 0; i < N; ++i){
if(distS[i] != -1 && distT[i] != -1){
answer[i] = max(1, distS[i]) + max(1, distT[i]) - 1;
pq.push(make_pair(answer[i], i));
}
}
while(!pq.empty()){
int cur, u;
tie(cur, u) = pq.top(); pq.pop();
for(auto [v, w] : adj[u]){
if(answer[v] == -1 || answer[v] > answer[u] + w){
answer[v] = answer[u] + w;
pq.push(make_pair(answer[v], v));
}
}
}
int Q;
cin >> Q;
while(Q--){
int u;
cin >> u;
--u;
cout << answer[u] << '\n';
}
return 0;
}
Compilation message
passport.cpp: In lambda function:
passport.cpp:22:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
22 | int mid = l + r >> 1;
| ~~^~~
passport.cpp: In lambda function:
passport.cpp:37:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
37 | int mid = l + r >> 1;
| ~~^~~
passport.cpp: In lambda function:
passport.cpp:60:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
60 | for(auto [v, w] : adj[u]){
| ^
passport.cpp: In function 'int main()':
passport.cpp:87:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
87 | for(auto [v, w] : adj[u]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
500 ms |
80716 KB |
Output is correct |
5 |
Correct |
230 ms |
60860 KB |
Output is correct |
6 |
Correct |
147 ms |
56000 KB |
Output is correct |
7 |
Correct |
98 ms |
53944 KB |
Output is correct |
8 |
Correct |
104 ms |
51128 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 |
348 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 |
504 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
2 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 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 |
348 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 |
504 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
2 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 |
4 ms |
1096 KB |
Output is correct |
17 |
Correct |
4 ms |
1104 KB |
Output is correct |
18 |
Correct |
5 ms |
1360 KB |
Output is correct |
19 |
Correct |
4 ms |
1360 KB |
Output is correct |
20 |
Correct |
3 ms |
1104 KB |
Output is correct |
21 |
Correct |
3 ms |
1104 KB |
Output is correct |
22 |
Correct |
2 ms |
1104 KB |
Output is correct |
23 |
Correct |
3 ms |
1104 KB |
Output is correct |
24 |
Correct |
3 ms |
1104 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 |
348 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 |
504 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
2 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 |
4 ms |
1096 KB |
Output is correct |
17 |
Correct |
4 ms |
1104 KB |
Output is correct |
18 |
Correct |
5 ms |
1360 KB |
Output is correct |
19 |
Correct |
4 ms |
1360 KB |
Output is correct |
20 |
Correct |
3 ms |
1104 KB |
Output is correct |
21 |
Correct |
3 ms |
1104 KB |
Output is correct |
22 |
Correct |
2 ms |
1104 KB |
Output is correct |
23 |
Correct |
3 ms |
1104 KB |
Output is correct |
24 |
Correct |
3 ms |
1104 KB |
Output is correct |
25 |
Correct |
1 ms |
456 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
6 ms |
1116 KB |
Output is correct |
28 |
Correct |
4 ms |
1104 KB |
Output is correct |
29 |
Correct |
3 ms |
984 KB |
Output is correct |
30 |
Correct |
4 ms |
1104 KB |
Output is correct |
31 |
Correct |
3 ms |
1104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
500 ms |
80716 KB |
Output is correct |
5 |
Correct |
230 ms |
60860 KB |
Output is correct |
6 |
Correct |
147 ms |
56000 KB |
Output is correct |
7 |
Correct |
98 ms |
53944 KB |
Output is correct |
8 |
Correct |
104 ms |
51128 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 |
348 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 |
504 KB |
Output is correct |
17 |
Correct |
1 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
504 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
4 ms |
1096 KB |
Output is correct |
25 |
Correct |
4 ms |
1104 KB |
Output is correct |
26 |
Correct |
5 ms |
1360 KB |
Output is correct |
27 |
Correct |
4 ms |
1360 KB |
Output is correct |
28 |
Correct |
3 ms |
1104 KB |
Output is correct |
29 |
Correct |
3 ms |
1104 KB |
Output is correct |
30 |
Correct |
2 ms |
1104 KB |
Output is correct |
31 |
Correct |
3 ms |
1104 KB |
Output is correct |
32 |
Correct |
3 ms |
1104 KB |
Output is correct |
33 |
Correct |
1 ms |
456 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
6 ms |
1116 KB |
Output is correct |
36 |
Correct |
4 ms |
1104 KB |
Output is correct |
37 |
Correct |
3 ms |
984 KB |
Output is correct |
38 |
Correct |
4 ms |
1104 KB |
Output is correct |
39 |
Correct |
3 ms |
1104 KB |
Output is correct |
40 |
Correct |
543 ms |
84368 KB |
Output is correct |
41 |
Correct |
280 ms |
62904 KB |
Output is correct |
42 |
Correct |
388 ms |
89780 KB |
Output is correct |
43 |
Correct |
317 ms |
89628 KB |
Output is correct |
44 |
Correct |
160 ms |
57784 KB |
Output is correct |
45 |
Correct |
194 ms |
63800 KB |
Output is correct |
46 |
Correct |
87 ms |
23092 KB |
Output is correct |
47 |
Correct |
227 ms |
65376 KB |
Output is correct |
48 |
Correct |
235 ms |
71640 KB |
Output is correct |