#include<bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
int n, q, x;
vector<vector<pii>> v;
int main() {
scanf("%d", &n);
vector<int> L(n + 5), R(n + 5);
v.resize(n * 5 + 5, vector<pii>());
auto build = [&] (auto &&build, int l, int r, int idx) {
if (l == r) return void(v[l].emplace_back(idx + n, 0));
int mid = (l + r) >> 1;
build(build, l, mid, idx * 2);
build(build, mid + 1, r, idx * 2 + 1);
v[idx * 2 + n].emplace_back(idx + n, 0);
v[idx * 2 + 1 + n].emplace_back(idx + n, 0);
};
auto upd = [&] (auto &&upd, int l, int r, int idx, int tl, int tr, int curidx) {
if (l > tr || r < tl) return;
if (l >= tl && r <= tr) return void(v[idx + n].emplace_back(curidx, 1));
int mid = (l + r) >> 1;
upd(upd, l, mid, idx * 2, tl, tr, curidx);
upd(upd, mid + 1, r, idx * 2 + 1, tl, tr, curidx);
};
build(build, 1, n, 1);
for (int i = 1; i <= n; i++) scanf("%d %d", &L[i], &R[i]), upd(upd, 1, n, 1, L[i], R[i], i);
scanf("%d", &q);
vector<int> d(n * 5 + 5, 1e9), d2(n * 5 + 5, 1e9), ans(n * 5 + 5, 1e9);
auto dijk = [&] (int s, vector<int> &curd) {
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.emplace(curd[s] = 0, s);
while (!pq.empty()) {
auto [nd, nn] = pq.top(); pq.pop();
for (auto &[tn, td]: v[nn]) if (curd[tn] > curd[nn] + td) pq.emplace(curd[tn] = curd[nn] + td, tn);
}
};
dijk(1, d); dijk(n, d2);
priority_queue<pii, vector<pii>, greater<pii>> pq;
for (int i = 1; i <= n; i++) pq.emplace(ans[i] = min(ans[i], d[i] + d2[i] - (i > 1 && i < n)), i);
priority_queue<pii, vector<pii>, greater<pii>> pq2;
while (!pq.empty()) {
auto [nd, nn] = pq.top(); pq.pop();
pq2.emplace(nd, nn);
}
while (!pq2.empty()) pq.emplace(pq2.top()), pq2.pop();
while (!pq.empty()) {
auto [nd, nn] = pq.top(); pq.pop();
for (auto &[tn, td]: v[nn]) if (ans[tn] > ans[nn] + td) pq.emplace(ans[tn] = ans[nn] + td, tn);
}
while (q--) scanf("%d", &x), printf("%d\n", ans[x] == 1e9 ? -1 : ans[x]);
}
Compilation message
passport.cpp: In function 'int main()':
passport.cpp:10:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
passport.cpp:29:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | for (int i = 1; i <= n; i++) scanf("%d %d", &L[i], &R[i]), upd(upd, 1, n, 1, L[i], R[i], i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:30:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
passport.cpp:53:22: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | while (q--) scanf("%d", &x), printf("%d\n", ans[x] == 1e9 ? -1 : ans[x]);
| ~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
736 ms |
92312 KB |
Output is correct |
5 |
Correct |
365 ms |
71812 KB |
Output is correct |
6 |
Correct |
234 ms |
66244 KB |
Output is correct |
7 |
Correct |
292 ms |
62932 KB |
Output is correct |
8 |
Correct |
177 ms |
61276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
5 ms |
1372 KB |
Output is correct |
17 |
Correct |
4 ms |
1116 KB |
Output is correct |
18 |
Correct |
4 ms |
1372 KB |
Output is correct |
19 |
Correct |
4 ms |
1416 KB |
Output is correct |
20 |
Correct |
2 ms |
1116 KB |
Output is correct |
21 |
Correct |
3 ms |
1116 KB |
Output is correct |
22 |
Correct |
2 ms |
1116 KB |
Output is correct |
23 |
Correct |
4 ms |
1116 KB |
Output is correct |
24 |
Correct |
3 ms |
1288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
5 ms |
1372 KB |
Output is correct |
17 |
Correct |
4 ms |
1116 KB |
Output is correct |
18 |
Correct |
4 ms |
1372 KB |
Output is correct |
19 |
Correct |
4 ms |
1416 KB |
Output is correct |
20 |
Correct |
2 ms |
1116 KB |
Output is correct |
21 |
Correct |
3 ms |
1116 KB |
Output is correct |
22 |
Correct |
2 ms |
1116 KB |
Output is correct |
23 |
Correct |
4 ms |
1116 KB |
Output is correct |
24 |
Correct |
3 ms |
1288 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
6 ms |
1380 KB |
Output is correct |
28 |
Correct |
5 ms |
1256 KB |
Output is correct |
29 |
Correct |
3 ms |
1116 KB |
Output is correct |
30 |
Correct |
3 ms |
1116 KB |
Output is correct |
31 |
Correct |
4 ms |
1304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
736 ms |
92312 KB |
Output is correct |
5 |
Correct |
365 ms |
71812 KB |
Output is correct |
6 |
Correct |
234 ms |
66244 KB |
Output is correct |
7 |
Correct |
292 ms |
62932 KB |
Output is correct |
8 |
Correct |
177 ms |
61276 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
5 ms |
1372 KB |
Output is correct |
25 |
Correct |
4 ms |
1116 KB |
Output is correct |
26 |
Correct |
4 ms |
1372 KB |
Output is correct |
27 |
Correct |
4 ms |
1416 KB |
Output is correct |
28 |
Correct |
2 ms |
1116 KB |
Output is correct |
29 |
Correct |
3 ms |
1116 KB |
Output is correct |
30 |
Correct |
2 ms |
1116 KB |
Output is correct |
31 |
Correct |
4 ms |
1116 KB |
Output is correct |
32 |
Correct |
3 ms |
1288 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
6 ms |
1380 KB |
Output is correct |
36 |
Correct |
5 ms |
1256 KB |
Output is correct |
37 |
Correct |
3 ms |
1116 KB |
Output is correct |
38 |
Correct |
3 ms |
1116 KB |
Output is correct |
39 |
Correct |
4 ms |
1304 KB |
Output is correct |
40 |
Correct |
817 ms |
96464 KB |
Output is correct |
41 |
Correct |
426 ms |
73468 KB |
Output is correct |
42 |
Correct |
488 ms |
99560 KB |
Output is correct |
43 |
Correct |
491 ms |
99764 KB |
Output is correct |
44 |
Correct |
270 ms |
67388 KB |
Output is correct |
45 |
Correct |
286 ms |
74692 KB |
Output is correct |
46 |
Correct |
161 ms |
27288 KB |
Output is correct |
47 |
Correct |
531 ms |
75312 KB |
Output is correct |
48 |
Correct |
364 ms |
82608 KB |
Output is correct |