#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define file(name) \
if(fopen(name".inp", "r")) \
freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
const int MAX = 2e5 + 5;
const int INF = 1e9;
int n, q, dp[3][MAX << 2], p[MAX];
vector <pair <int, int>> adj[MAX << 2];
void build(int idx, int l, int r) {
if(l == r) {
p[l] = idx;
return;
} else {
int mid = l + r >> 1;
build(idx << 1, l, mid);
build(idx << 1 | 1, mid + 1, r);
adj[idx << 1].emplace_back(idx, 0);
adj[idx << 1 | 1].emplace_back(idx, 0);
}
}
void add_edge(int idx, int l, int r, int u, int v, int i) {
if(l > v || r < u) return;
if(l >= u && r <= v) {
adj[idx].emplace_back(p[i], 1);
return;
}
int mid = l + r >> 1;
add_edge(idx << 1, l, mid, u, v, i);
add_edge(idx << 1 | 1, mid + 1, r, u, v, i);
}
void calc(int dp[]) {
priority_queue <pair <int, int>, vector <pair <int, int>>, greater <pair <int, int>>> q;
for (int i = 1; i <= n; ++i) {
if(dp[p[i]] < INF) {
q.emplace(dp[p[i]], p[i]);
}
}
while(!q.empty()) {
auto [du, u] = q.top(); q.pop();
if(dp[u] != du) continue;
for (auto [v, w] : adj[u]) if(dp[v] > dp[u] + w) {
dp[v] = dp[u] + w;
q.emplace(dp[v], v);
}
}
}
void you_make_it(void) {
cin >> n;
build(1, 1, n);
for (int i = 1; i <= n; ++i) {
int l, r; cin >> l >> r;
add_edge(1, 1, n, l, r, i);
}
memset(dp, 0x3f, sizeof dp);
dp[0][p[1]] = 0; calc(dp[0]);
dp[1][p[n]] = 0; calc(dp[1]);
for (int i = 1; i <= n; ++i) {
dp[2][p[i]] = dp[0][p[i]] + dp[1][p[i]] - (1 < i && i < n);
}
calc(dp[2]);
cin >> q; while(q--) {
int x; cin >> x; cout << (dp[2][p[x]] >= INF ? -1 : dp[2][p[x]]) << '\n';
}
}
signed main() {
#ifdef LOCAL
freopen("TASK.inp", "r", stdin);
freopen("TASK.out", "w", stdout);
#endif
file("passport");
auto start_time = chrono::steady_clock::now();
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
you_make_it();
auto end_time = chrono::steady_clock::now();
cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
// Dream it. Wish it. Do it.
Compilation message
passport.cpp: In function 'void build(int, int, int)':
passport.cpp:23:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | int mid = l + r >> 1;
| ~~^~~
passport.cpp: In function 'void add_edge(int, int, int, int, int, int)':
passport.cpp:38:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | int mid = l + r >> 1;
| ~~^~~
passport.cpp: In function 'int main()':
passport.cpp:10:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:87:5: note: in expansion of macro 'file'
87 | file("passport");
| ^~~~
passport.cpp:10:49: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:87:5: note: in expansion of macro 'file'
87 | file("passport");
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29276 KB |
Output is correct |
2 |
Correct |
6 ms |
29276 KB |
Output is correct |
3 |
Correct |
6 ms |
29276 KB |
Output is correct |
4 |
Correct |
544 ms |
76048 KB |
Output is correct |
5 |
Correct |
267 ms |
53956 KB |
Output is correct |
6 |
Correct |
147 ms |
48704 KB |
Output is correct |
7 |
Correct |
176 ms |
48056 KB |
Output is correct |
8 |
Correct |
98 ms |
53644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29272 KB |
Output is correct |
2 |
Correct |
6 ms |
29276 KB |
Output is correct |
3 |
Correct |
5 ms |
29276 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
7 ms |
29272 KB |
Output is correct |
6 |
Correct |
6 ms |
29276 KB |
Output is correct |
7 |
Correct |
6 ms |
29276 KB |
Output is correct |
8 |
Correct |
6 ms |
29296 KB |
Output is correct |
9 |
Correct |
6 ms |
29276 KB |
Output is correct |
10 |
Correct |
6 ms |
29276 KB |
Output is correct |
11 |
Correct |
6 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29276 KB |
Output is correct |
13 |
Correct |
6 ms |
29276 KB |
Output is correct |
14 |
Correct |
6 ms |
29472 KB |
Output is correct |
15 |
Correct |
6 ms |
29272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29272 KB |
Output is correct |
2 |
Correct |
6 ms |
29276 KB |
Output is correct |
3 |
Correct |
5 ms |
29276 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
7 ms |
29272 KB |
Output is correct |
6 |
Correct |
6 ms |
29276 KB |
Output is correct |
7 |
Correct |
6 ms |
29276 KB |
Output is correct |
8 |
Correct |
6 ms |
29296 KB |
Output is correct |
9 |
Correct |
6 ms |
29276 KB |
Output is correct |
10 |
Correct |
6 ms |
29276 KB |
Output is correct |
11 |
Correct |
6 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29276 KB |
Output is correct |
13 |
Correct |
6 ms |
29276 KB |
Output is correct |
14 |
Correct |
6 ms |
29472 KB |
Output is correct |
15 |
Correct |
6 ms |
29272 KB |
Output is correct |
16 |
Correct |
8 ms |
29788 KB |
Output is correct |
17 |
Correct |
8 ms |
29528 KB |
Output is correct |
18 |
Correct |
9 ms |
29788 KB |
Output is correct |
19 |
Correct |
9 ms |
29788 KB |
Output is correct |
20 |
Correct |
7 ms |
29784 KB |
Output is correct |
21 |
Correct |
7 ms |
29532 KB |
Output is correct |
22 |
Correct |
7 ms |
29644 KB |
Output is correct |
23 |
Correct |
8 ms |
29784 KB |
Output is correct |
24 |
Correct |
7 ms |
29788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29272 KB |
Output is correct |
2 |
Correct |
6 ms |
29276 KB |
Output is correct |
3 |
Correct |
5 ms |
29276 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
7 ms |
29272 KB |
Output is correct |
6 |
Correct |
6 ms |
29276 KB |
Output is correct |
7 |
Correct |
6 ms |
29276 KB |
Output is correct |
8 |
Correct |
6 ms |
29296 KB |
Output is correct |
9 |
Correct |
6 ms |
29276 KB |
Output is correct |
10 |
Correct |
6 ms |
29276 KB |
Output is correct |
11 |
Correct |
6 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29276 KB |
Output is correct |
13 |
Correct |
6 ms |
29276 KB |
Output is correct |
14 |
Correct |
6 ms |
29472 KB |
Output is correct |
15 |
Correct |
6 ms |
29272 KB |
Output is correct |
16 |
Correct |
8 ms |
29788 KB |
Output is correct |
17 |
Correct |
8 ms |
29528 KB |
Output is correct |
18 |
Correct |
9 ms |
29788 KB |
Output is correct |
19 |
Correct |
9 ms |
29788 KB |
Output is correct |
20 |
Correct |
7 ms |
29784 KB |
Output is correct |
21 |
Correct |
7 ms |
29532 KB |
Output is correct |
22 |
Correct |
7 ms |
29644 KB |
Output is correct |
23 |
Correct |
8 ms |
29784 KB |
Output is correct |
24 |
Correct |
7 ms |
29788 KB |
Output is correct |
25 |
Correct |
6 ms |
29276 KB |
Output is correct |
26 |
Correct |
6 ms |
29272 KB |
Output is correct |
27 |
Correct |
9 ms |
29788 KB |
Output is correct |
28 |
Correct |
8 ms |
29532 KB |
Output is correct |
29 |
Correct |
7 ms |
29688 KB |
Output is correct |
30 |
Correct |
7 ms |
29532 KB |
Output is correct |
31 |
Correct |
8 ms |
29708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29276 KB |
Output is correct |
2 |
Correct |
6 ms |
29276 KB |
Output is correct |
3 |
Correct |
6 ms |
29276 KB |
Output is correct |
4 |
Correct |
544 ms |
76048 KB |
Output is correct |
5 |
Correct |
267 ms |
53956 KB |
Output is correct |
6 |
Correct |
147 ms |
48704 KB |
Output is correct |
7 |
Correct |
176 ms |
48056 KB |
Output is correct |
8 |
Correct |
98 ms |
53644 KB |
Output is correct |
9 |
Correct |
6 ms |
29272 KB |
Output is correct |
10 |
Correct |
6 ms |
29276 KB |
Output is correct |
11 |
Correct |
5 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29276 KB |
Output is correct |
13 |
Correct |
7 ms |
29272 KB |
Output is correct |
14 |
Correct |
6 ms |
29276 KB |
Output is correct |
15 |
Correct |
6 ms |
29276 KB |
Output is correct |
16 |
Correct |
6 ms |
29296 KB |
Output is correct |
17 |
Correct |
6 ms |
29276 KB |
Output is correct |
18 |
Correct |
6 ms |
29276 KB |
Output is correct |
19 |
Correct |
6 ms |
29276 KB |
Output is correct |
20 |
Correct |
6 ms |
29276 KB |
Output is correct |
21 |
Correct |
6 ms |
29276 KB |
Output is correct |
22 |
Correct |
6 ms |
29472 KB |
Output is correct |
23 |
Correct |
6 ms |
29272 KB |
Output is correct |
24 |
Correct |
8 ms |
29788 KB |
Output is correct |
25 |
Correct |
8 ms |
29528 KB |
Output is correct |
26 |
Correct |
9 ms |
29788 KB |
Output is correct |
27 |
Correct |
9 ms |
29788 KB |
Output is correct |
28 |
Correct |
7 ms |
29784 KB |
Output is correct |
29 |
Correct |
7 ms |
29532 KB |
Output is correct |
30 |
Correct |
7 ms |
29644 KB |
Output is correct |
31 |
Correct |
8 ms |
29784 KB |
Output is correct |
32 |
Correct |
7 ms |
29788 KB |
Output is correct |
33 |
Correct |
6 ms |
29276 KB |
Output is correct |
34 |
Correct |
6 ms |
29272 KB |
Output is correct |
35 |
Correct |
9 ms |
29788 KB |
Output is correct |
36 |
Correct |
8 ms |
29532 KB |
Output is correct |
37 |
Correct |
7 ms |
29688 KB |
Output is correct |
38 |
Correct |
7 ms |
29532 KB |
Output is correct |
39 |
Correct |
8 ms |
29708 KB |
Output is correct |
40 |
Correct |
603 ms |
80184 KB |
Output is correct |
41 |
Correct |
283 ms |
55496 KB |
Output is correct |
42 |
Correct |
369 ms |
81600 KB |
Output is correct |
43 |
Correct |
368 ms |
81464 KB |
Output is correct |
44 |
Correct |
171 ms |
49096 KB |
Output is correct |
45 |
Correct |
196 ms |
55880 KB |
Output is correct |
46 |
Correct |
105 ms |
38876 KB |
Output is correct |
47 |
Correct |
330 ms |
58340 KB |
Output is correct |
48 |
Correct |
247 ms |
64672 KB |
Output is correct |