#include <iostream>
#include <vector>
#include <array>
#include <queue>
#define ll long long
using namespace std;
vector <array<ll, 2>> adj[1000000];
priority_queue <array<ll, 3>, vector<array<ll, 3>>, greater<array<ll, 3>>> pq;
ll n, q, a, b, A[200000], dist[1000000][4];
void build(ll id, ll l, ll r) {
if (l == r) {
adj[l].push_back({n+id, 1});
return;
}
ll mid = (l+r)/2;
build(id*2, l, mid);
build(id*2+1, mid+1, r);
adj[n+id*2].push_back({n+id, 0});
adj[n+id*2+1].push_back({n+id, 0});
}
void query(ll id, ll l, ll r, ll ql, ll qr, ll u) {
if (qr < l || r < ql) return;
else if (ql <= l && r <= qr) {
adj[n+id].push_back({u, 0});
return;
}
ll mid = (l+r)/2;
query(id*2, l, mid, ql, qr, u);
query(id*2+1, mid+1, r, ql, qr, u);
}
int main() {
cin >> n;
build(1, 0, n-1);
for (int i=0; i<5*n; ++i) {
for (int j=0; j<4; ++j) {
dist[i][j] = 1e18;
}
}
for (int i=0; i<n; ++i) {
cin >> a >> b;
--a, --b;
query(1, 0, n-1, a, b, i);
if (!a) A[i] |= 1;
if (b == n-1) A[i] |= 2;
if (A[i]) {
dist[i][A[i]] = 0;
pq.push({0, i, A[i]});
}
}
while (!pq.empty()) {
auto [w, u, x] = pq.top();
pq.pop();
if (dist[u][x] != w) continue;
if (dist[u][3] > dist[u][1] + dist[u][2]) {
dist[u][3] = dist[u][1] + dist[u][2];
pq.push({dist[u][3], u, 3});
}
for (auto [v, z] : adj[u]) {
if (dist[v][x] > w+z) {
dist[v][x] = w+z;
pq.push({w+z, v, x});
}
}
}
cin >> q;
while (q--) {
cin >> a;
--a;
if (dist[a][3] == 1e18) cout << "-1\n";
else cout << dist[a][3]+1 << '\n';
}
}
Compilation message
passport.cpp: In function 'int main()':
passport.cpp:57:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
57 | auto [w, u, x] = pq.top();
| ^
passport.cpp:64:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
64 | for (auto [v, z] : adj[u]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
25952 KB |
Output is correct |
2 |
Correct |
9 ms |
25692 KB |
Output is correct |
3 |
Correct |
8 ms |
25844 KB |
Output is correct |
4 |
Correct |
1221 ms |
162252 KB |
Output is correct |
5 |
Correct |
698 ms |
121832 KB |
Output is correct |
6 |
Correct |
513 ms |
111596 KB |
Output is correct |
7 |
Correct |
210 ms |
84032 KB |
Output is correct |
8 |
Correct |
228 ms |
95396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
25688 KB |
Output is correct |
2 |
Correct |
6 ms |
25692 KB |
Output is correct |
3 |
Correct |
6 ms |
25692 KB |
Output is correct |
4 |
Correct |
6 ms |
25692 KB |
Output is correct |
5 |
Correct |
6 ms |
25824 KB |
Output is correct |
6 |
Correct |
6 ms |
25836 KB |
Output is correct |
7 |
Correct |
6 ms |
25692 KB |
Output is correct |
8 |
Correct |
6 ms |
25692 KB |
Output is correct |
9 |
Correct |
6 ms |
25692 KB |
Output is correct |
10 |
Correct |
6 ms |
25692 KB |
Output is correct |
11 |
Correct |
7 ms |
25788 KB |
Output is correct |
12 |
Correct |
6 ms |
25948 KB |
Output is correct |
13 |
Correct |
6 ms |
25948 KB |
Output is correct |
14 |
Correct |
6 ms |
25948 KB |
Output is correct |
15 |
Correct |
6 ms |
25948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
25688 KB |
Output is correct |
2 |
Correct |
6 ms |
25692 KB |
Output is correct |
3 |
Correct |
6 ms |
25692 KB |
Output is correct |
4 |
Correct |
6 ms |
25692 KB |
Output is correct |
5 |
Correct |
6 ms |
25824 KB |
Output is correct |
6 |
Correct |
6 ms |
25836 KB |
Output is correct |
7 |
Correct |
6 ms |
25692 KB |
Output is correct |
8 |
Correct |
6 ms |
25692 KB |
Output is correct |
9 |
Correct |
6 ms |
25692 KB |
Output is correct |
10 |
Correct |
6 ms |
25692 KB |
Output is correct |
11 |
Correct |
7 ms |
25788 KB |
Output is correct |
12 |
Correct |
6 ms |
25948 KB |
Output is correct |
13 |
Correct |
6 ms |
25948 KB |
Output is correct |
14 |
Correct |
6 ms |
25948 KB |
Output is correct |
15 |
Correct |
6 ms |
25948 KB |
Output is correct |
16 |
Correct |
13 ms |
29020 KB |
Output is correct |
17 |
Correct |
13 ms |
28820 KB |
Output is correct |
18 |
Correct |
14 ms |
29136 KB |
Output is correct |
19 |
Correct |
14 ms |
29016 KB |
Output is correct |
20 |
Correct |
11 ms |
28760 KB |
Output is correct |
21 |
Correct |
11 ms |
28744 KB |
Output is correct |
22 |
Correct |
11 ms |
28524 KB |
Output is correct |
23 |
Correct |
13 ms |
28764 KB |
Output is correct |
24 |
Correct |
13 ms |
28872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
25688 KB |
Output is correct |
2 |
Correct |
6 ms |
25692 KB |
Output is correct |
3 |
Correct |
6 ms |
25692 KB |
Output is correct |
4 |
Correct |
6 ms |
25692 KB |
Output is correct |
5 |
Correct |
6 ms |
25824 KB |
Output is correct |
6 |
Correct |
6 ms |
25836 KB |
Output is correct |
7 |
Correct |
6 ms |
25692 KB |
Output is correct |
8 |
Correct |
6 ms |
25692 KB |
Output is correct |
9 |
Correct |
6 ms |
25692 KB |
Output is correct |
10 |
Correct |
6 ms |
25692 KB |
Output is correct |
11 |
Correct |
7 ms |
25788 KB |
Output is correct |
12 |
Correct |
6 ms |
25948 KB |
Output is correct |
13 |
Correct |
6 ms |
25948 KB |
Output is correct |
14 |
Correct |
6 ms |
25948 KB |
Output is correct |
15 |
Correct |
6 ms |
25948 KB |
Output is correct |
16 |
Correct |
13 ms |
29020 KB |
Output is correct |
17 |
Correct |
13 ms |
28820 KB |
Output is correct |
18 |
Correct |
14 ms |
29136 KB |
Output is correct |
19 |
Correct |
14 ms |
29016 KB |
Output is correct |
20 |
Correct |
11 ms |
28760 KB |
Output is correct |
21 |
Correct |
11 ms |
28744 KB |
Output is correct |
22 |
Correct |
11 ms |
28524 KB |
Output is correct |
23 |
Correct |
13 ms |
28764 KB |
Output is correct |
24 |
Correct |
13 ms |
28872 KB |
Output is correct |
25 |
Correct |
7 ms |
25688 KB |
Output is correct |
26 |
Correct |
5 ms |
25688 KB |
Output is correct |
27 |
Correct |
20 ms |
29016 KB |
Output is correct |
28 |
Correct |
16 ms |
28764 KB |
Output is correct |
29 |
Correct |
14 ms |
28760 KB |
Output is correct |
30 |
Correct |
13 ms |
28764 KB |
Output is correct |
31 |
Correct |
14 ms |
28764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
25952 KB |
Output is correct |
2 |
Correct |
9 ms |
25692 KB |
Output is correct |
3 |
Correct |
8 ms |
25844 KB |
Output is correct |
4 |
Correct |
1221 ms |
162252 KB |
Output is correct |
5 |
Correct |
698 ms |
121832 KB |
Output is correct |
6 |
Correct |
513 ms |
111596 KB |
Output is correct |
7 |
Correct |
210 ms |
84032 KB |
Output is correct |
8 |
Correct |
228 ms |
95396 KB |
Output is correct |
9 |
Correct |
6 ms |
25688 KB |
Output is correct |
10 |
Correct |
6 ms |
25692 KB |
Output is correct |
11 |
Correct |
6 ms |
25692 KB |
Output is correct |
12 |
Correct |
6 ms |
25692 KB |
Output is correct |
13 |
Correct |
6 ms |
25824 KB |
Output is correct |
14 |
Correct |
6 ms |
25836 KB |
Output is correct |
15 |
Correct |
6 ms |
25692 KB |
Output is correct |
16 |
Correct |
6 ms |
25692 KB |
Output is correct |
17 |
Correct |
6 ms |
25692 KB |
Output is correct |
18 |
Correct |
6 ms |
25692 KB |
Output is correct |
19 |
Correct |
7 ms |
25788 KB |
Output is correct |
20 |
Correct |
6 ms |
25948 KB |
Output is correct |
21 |
Correct |
6 ms |
25948 KB |
Output is correct |
22 |
Correct |
6 ms |
25948 KB |
Output is correct |
23 |
Correct |
6 ms |
25948 KB |
Output is correct |
24 |
Correct |
13 ms |
29020 KB |
Output is correct |
25 |
Correct |
13 ms |
28820 KB |
Output is correct |
26 |
Correct |
14 ms |
29136 KB |
Output is correct |
27 |
Correct |
14 ms |
29016 KB |
Output is correct |
28 |
Correct |
11 ms |
28760 KB |
Output is correct |
29 |
Correct |
11 ms |
28744 KB |
Output is correct |
30 |
Correct |
11 ms |
28524 KB |
Output is correct |
31 |
Correct |
13 ms |
28764 KB |
Output is correct |
32 |
Correct |
13 ms |
28872 KB |
Output is correct |
33 |
Correct |
7 ms |
25688 KB |
Output is correct |
34 |
Correct |
5 ms |
25688 KB |
Output is correct |
35 |
Correct |
20 ms |
29016 KB |
Output is correct |
36 |
Correct |
16 ms |
28764 KB |
Output is correct |
37 |
Correct |
14 ms |
28760 KB |
Output is correct |
38 |
Correct |
13 ms |
28764 KB |
Output is correct |
39 |
Correct |
14 ms |
28764 KB |
Output is correct |
40 |
Correct |
1433 ms |
162880 KB |
Output is correct |
41 |
Correct |
954 ms |
122696 KB |
Output is correct |
42 |
Correct |
1010 ms |
156636 KB |
Output is correct |
43 |
Correct |
923 ms |
157024 KB |
Output is correct |
44 |
Correct |
790 ms |
113408 KB |
Output is correct |
45 |
Correct |
766 ms |
109896 KB |
Output is correct |
46 |
Correct |
328 ms |
59736 KB |
Output is correct |
47 |
Correct |
989 ms |
118376 KB |
Output is correct |
48 |
Correct |
743 ms |
132056 KB |
Output is correct |