#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 = 1e5 + 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 |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Runtime error |
25 ms |
34652 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14936 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
15192 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14736 KB |
Output is correct |
8 |
Correct |
3 ms |
14740 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14936 KB |
Output is correct |
11 |
Correct |
4 ms |
14996 KB |
Output is correct |
12 |
Correct |
4 ms |
14936 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
4 ms |
14940 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14936 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
15192 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14736 KB |
Output is correct |
8 |
Correct |
3 ms |
14740 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14936 KB |
Output is correct |
11 |
Correct |
4 ms |
14996 KB |
Output is correct |
12 |
Correct |
4 ms |
14936 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
4 ms |
14940 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
6 ms |
15192 KB |
Output is correct |
17 |
Correct |
6 ms |
15268 KB |
Output is correct |
18 |
Correct |
6 ms |
15448 KB |
Output is correct |
19 |
Correct |
7 ms |
15452 KB |
Output is correct |
20 |
Correct |
5 ms |
15196 KB |
Output is correct |
21 |
Correct |
5 ms |
15192 KB |
Output is correct |
22 |
Correct |
4 ms |
15192 KB |
Output is correct |
23 |
Correct |
6 ms |
15184 KB |
Output is correct |
24 |
Correct |
5 ms |
15196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14936 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
15192 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14736 KB |
Output is correct |
8 |
Correct |
3 ms |
14740 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14936 KB |
Output is correct |
11 |
Correct |
4 ms |
14996 KB |
Output is correct |
12 |
Correct |
4 ms |
14936 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
4 ms |
14940 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
6 ms |
15192 KB |
Output is correct |
17 |
Correct |
6 ms |
15268 KB |
Output is correct |
18 |
Correct |
6 ms |
15448 KB |
Output is correct |
19 |
Correct |
7 ms |
15452 KB |
Output is correct |
20 |
Correct |
5 ms |
15196 KB |
Output is correct |
21 |
Correct |
5 ms |
15192 KB |
Output is correct |
22 |
Correct |
4 ms |
15192 KB |
Output is correct |
23 |
Correct |
6 ms |
15184 KB |
Output is correct |
24 |
Correct |
5 ms |
15196 KB |
Output is correct |
25 |
Correct |
5 ms |
14940 KB |
Output is correct |
26 |
Correct |
4 ms |
14944 KB |
Output is correct |
27 |
Correct |
7 ms |
15200 KB |
Output is correct |
28 |
Correct |
7 ms |
15196 KB |
Output is correct |
29 |
Correct |
5 ms |
15196 KB |
Output is correct |
30 |
Correct |
5 ms |
15196 KB |
Output is correct |
31 |
Correct |
6 ms |
15196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Runtime error |
25 ms |
34652 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |