#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 8e5 + 10;
int f[N], g[N], h[N], p[N];
bool mark[N];
vector<ii> adj[N];
priority_queue<ii, vector<ii>, greater<ii> > q;
void calc(int start, int d[]) {
d[start] = 0;
q.push({0, start});
while (!q.empty()) {
int cost, u; tie(cost, u) = q.top(); q.pop();
if (d[u] != cost) continue;
for(auto &[v, w] : adj[u]) if (d[v] > d[u] + w) d[v] = d[u] + w, q.push({d[v], v});
}
}
void init(int s, int l, int r) {
if (l == r) {
mark[s] = 1;
p[l] = s;
return;
}
adj[s << 1].emplace_back(s, 0);
adj[s << 1 | 1].emplace_back(s, 0);
int mid = l + r >> 1;
init(s << 1, l, mid);
init(s << 1 | 1, mid + 1, r);
}
void add(int s, int l, int r, int from, int to, int idx) {
if (l > to || r < from) return;
if (from <= l && r <= to) {
adj[s].emplace_back(p[idx], 1);
return;
}
int mid = l + r >> 1;
add(s << 1, l, mid, from, to, idx);
add(s << 1 | 1, mid + 1, r, from, to, idx);
}
int main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen ("task.inp", "r")) {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
}
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
int n; cin >> n;
init(1, 1, n);
for(int i = 1; i <= n; i++) {
int l, r; cin >> l >> r;
add(1, 1, n, l, r, i);
}
for(int i = 1; i <= 4 * n; i++) f[i] = g[i] = 1e9;
calc(p[1], f);
calc(p[n], g);
for(int i = 1; i <= 4 * n; i++) {
if (i == p[1] || i == p[n]) h[i] = f[i] + g[i];
else h[i] = f[i] + g[i] - mark[i];
q.push({h[i], i});
}
while (!q.empty()) {
int cost, u; tie(cost, u) = q.top(); q.pop();
if (cost != h[u]) continue;
for(auto &[v, w] : adj[u]) if (h[v] > h[u] + w) {
h[v] = h[u] + w;
q.push({h[v], v});
}
}
for(int i = 1; i <= 4 * n; i++) if (h[i] > 1e8) h[i] = -1;
int q; cin >> q;
while (q--) {
int x; cin >> x;
cout << h[p[x]] << endl;
}
}
Compilation message
passport.cpp: In function 'void init(int, int, int)':
passport.cpp:30:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
30 | int mid = l + r >> 1;
| ~~^~~
passport.cpp: In function 'void add(int, int, int, int, int, int)':
passport.cpp:41:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | int mid = l + r >> 1;
| ~~^~~
passport.cpp: In function 'int main()':
passport.cpp:50:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | freopen ("task.inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:51:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | freopen ("task.out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:54:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | freopen (task".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:55:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | freopen (task".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19028 KB |
Output is correct |
2 |
Correct |
11 ms |
19024 KB |
Output is correct |
3 |
Correct |
10 ms |
19028 KB |
Output is correct |
4 |
Correct |
826 ms |
77844 KB |
Output is correct |
5 |
Correct |
392 ms |
57836 KB |
Output is correct |
6 |
Correct |
234 ms |
52540 KB |
Output is correct |
7 |
Correct |
275 ms |
50536 KB |
Output is correct |
8 |
Correct |
235 ms |
56844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19028 KB |
Output is correct |
2 |
Correct |
11 ms |
19112 KB |
Output is correct |
3 |
Correct |
10 ms |
19144 KB |
Output is correct |
4 |
Correct |
11 ms |
19120 KB |
Output is correct |
5 |
Correct |
12 ms |
19084 KB |
Output is correct |
6 |
Correct |
11 ms |
19108 KB |
Output is correct |
7 |
Correct |
11 ms |
19092 KB |
Output is correct |
8 |
Correct |
11 ms |
19028 KB |
Output is correct |
9 |
Correct |
11 ms |
19152 KB |
Output is correct |
10 |
Correct |
10 ms |
19028 KB |
Output is correct |
11 |
Correct |
11 ms |
19156 KB |
Output is correct |
12 |
Correct |
12 ms |
19224 KB |
Output is correct |
13 |
Correct |
12 ms |
19156 KB |
Output is correct |
14 |
Correct |
10 ms |
19156 KB |
Output is correct |
15 |
Correct |
11 ms |
19252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19028 KB |
Output is correct |
2 |
Correct |
11 ms |
19112 KB |
Output is correct |
3 |
Correct |
10 ms |
19144 KB |
Output is correct |
4 |
Correct |
11 ms |
19120 KB |
Output is correct |
5 |
Correct |
12 ms |
19084 KB |
Output is correct |
6 |
Correct |
11 ms |
19108 KB |
Output is correct |
7 |
Correct |
11 ms |
19092 KB |
Output is correct |
8 |
Correct |
11 ms |
19028 KB |
Output is correct |
9 |
Correct |
11 ms |
19152 KB |
Output is correct |
10 |
Correct |
10 ms |
19028 KB |
Output is correct |
11 |
Correct |
11 ms |
19156 KB |
Output is correct |
12 |
Correct |
12 ms |
19224 KB |
Output is correct |
13 |
Correct |
12 ms |
19156 KB |
Output is correct |
14 |
Correct |
10 ms |
19156 KB |
Output is correct |
15 |
Correct |
11 ms |
19252 KB |
Output is correct |
16 |
Correct |
14 ms |
19796 KB |
Output is correct |
17 |
Correct |
14 ms |
19668 KB |
Output is correct |
18 |
Correct |
14 ms |
19924 KB |
Output is correct |
19 |
Correct |
14 ms |
19956 KB |
Output is correct |
20 |
Correct |
14 ms |
19604 KB |
Output is correct |
21 |
Correct |
14 ms |
19668 KB |
Output is correct |
22 |
Correct |
12 ms |
19648 KB |
Output is correct |
23 |
Correct |
14 ms |
19740 KB |
Output is correct |
24 |
Correct |
13 ms |
19852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19028 KB |
Output is correct |
2 |
Correct |
11 ms |
19112 KB |
Output is correct |
3 |
Correct |
10 ms |
19144 KB |
Output is correct |
4 |
Correct |
11 ms |
19120 KB |
Output is correct |
5 |
Correct |
12 ms |
19084 KB |
Output is correct |
6 |
Correct |
11 ms |
19108 KB |
Output is correct |
7 |
Correct |
11 ms |
19092 KB |
Output is correct |
8 |
Correct |
11 ms |
19028 KB |
Output is correct |
9 |
Correct |
11 ms |
19152 KB |
Output is correct |
10 |
Correct |
10 ms |
19028 KB |
Output is correct |
11 |
Correct |
11 ms |
19156 KB |
Output is correct |
12 |
Correct |
12 ms |
19224 KB |
Output is correct |
13 |
Correct |
12 ms |
19156 KB |
Output is correct |
14 |
Correct |
10 ms |
19156 KB |
Output is correct |
15 |
Correct |
11 ms |
19252 KB |
Output is correct |
16 |
Correct |
14 ms |
19796 KB |
Output is correct |
17 |
Correct |
14 ms |
19668 KB |
Output is correct |
18 |
Correct |
14 ms |
19924 KB |
Output is correct |
19 |
Correct |
14 ms |
19956 KB |
Output is correct |
20 |
Correct |
14 ms |
19604 KB |
Output is correct |
21 |
Correct |
14 ms |
19668 KB |
Output is correct |
22 |
Correct |
12 ms |
19648 KB |
Output is correct |
23 |
Correct |
14 ms |
19740 KB |
Output is correct |
24 |
Correct |
13 ms |
19852 KB |
Output is correct |
25 |
Correct |
10 ms |
19028 KB |
Output is correct |
26 |
Correct |
10 ms |
19148 KB |
Output is correct |
27 |
Correct |
15 ms |
19796 KB |
Output is correct |
28 |
Correct |
15 ms |
19668 KB |
Output is correct |
29 |
Correct |
14 ms |
19708 KB |
Output is correct |
30 |
Correct |
13 ms |
19668 KB |
Output is correct |
31 |
Correct |
14 ms |
19668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19028 KB |
Output is correct |
2 |
Correct |
11 ms |
19024 KB |
Output is correct |
3 |
Correct |
10 ms |
19028 KB |
Output is correct |
4 |
Correct |
826 ms |
77844 KB |
Output is correct |
5 |
Correct |
392 ms |
57836 KB |
Output is correct |
6 |
Correct |
234 ms |
52540 KB |
Output is correct |
7 |
Correct |
275 ms |
50536 KB |
Output is correct |
8 |
Correct |
235 ms |
56844 KB |
Output is correct |
9 |
Correct |
11 ms |
19028 KB |
Output is correct |
10 |
Correct |
11 ms |
19112 KB |
Output is correct |
11 |
Correct |
10 ms |
19144 KB |
Output is correct |
12 |
Correct |
11 ms |
19120 KB |
Output is correct |
13 |
Correct |
12 ms |
19084 KB |
Output is correct |
14 |
Correct |
11 ms |
19108 KB |
Output is correct |
15 |
Correct |
11 ms |
19092 KB |
Output is correct |
16 |
Correct |
11 ms |
19028 KB |
Output is correct |
17 |
Correct |
11 ms |
19152 KB |
Output is correct |
18 |
Correct |
10 ms |
19028 KB |
Output is correct |
19 |
Correct |
11 ms |
19156 KB |
Output is correct |
20 |
Correct |
12 ms |
19224 KB |
Output is correct |
21 |
Correct |
12 ms |
19156 KB |
Output is correct |
22 |
Correct |
10 ms |
19156 KB |
Output is correct |
23 |
Correct |
11 ms |
19252 KB |
Output is correct |
24 |
Correct |
14 ms |
19796 KB |
Output is correct |
25 |
Correct |
14 ms |
19668 KB |
Output is correct |
26 |
Correct |
14 ms |
19924 KB |
Output is correct |
27 |
Correct |
14 ms |
19956 KB |
Output is correct |
28 |
Correct |
14 ms |
19604 KB |
Output is correct |
29 |
Correct |
14 ms |
19668 KB |
Output is correct |
30 |
Correct |
12 ms |
19648 KB |
Output is correct |
31 |
Correct |
14 ms |
19740 KB |
Output is correct |
32 |
Correct |
13 ms |
19852 KB |
Output is correct |
33 |
Correct |
10 ms |
19028 KB |
Output is correct |
34 |
Correct |
10 ms |
19148 KB |
Output is correct |
35 |
Correct |
15 ms |
19796 KB |
Output is correct |
36 |
Correct |
15 ms |
19668 KB |
Output is correct |
37 |
Correct |
14 ms |
19708 KB |
Output is correct |
38 |
Correct |
13 ms |
19668 KB |
Output is correct |
39 |
Correct |
14 ms |
19668 KB |
Output is correct |
40 |
Correct |
862 ms |
78220 KB |
Output is correct |
41 |
Correct |
427 ms |
58288 KB |
Output is correct |
42 |
Correct |
532 ms |
85324 KB |
Output is correct |
43 |
Correct |
521 ms |
85192 KB |
Output is correct |
44 |
Correct |
268 ms |
53136 KB |
Output is correct |
45 |
Correct |
365 ms |
60324 KB |
Output is correct |
46 |
Correct |
183 ms |
34884 KB |
Output is correct |
47 |
Correct |
498 ms |
60792 KB |
Output is correct |
48 |
Correct |
448 ms |
68732 KB |
Output is correct |