#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ull unsigned long long
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define int long long
// #define int unsigned long long
// #define ordered_set(T) tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
// #define ordered_multiset(T) tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>
void open_file(string filename) {
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
// const ll mod = 1e9 + 7;
// const ll mod = 998244353;
const ll inf = 1e9;
const ll biginf = 1e18;
const int maxN = 1e6 + 305;
vector<int> g[maxN];
int n, q, d[3][maxN];
int build(int v, int tl, int tr) {
if (tl == tr) return tl;
int tm = (tl + tr) / 2;
int x = build(2 * v, tl, tm);
int y = build(2 * v + 1, tm + 1, tr);
g[x].pb(v + n); g[y].pb(v + n);
return v + n;
}
void update(int v, int tl, int tr, int l, int r, int pos) {
if (r < tl || tr < l) return;
if (l <= tl && tr <= r) {
if (tl == tr) g[tl].pb(pos);
else g[v + n].pb(pos);
return;
} int tm = (tl + tr) / 2;
update(2 * v, tl, tm, l, r, pos);
update(2 * v + 1, tm + 1, tr, l, r, pos);
}
void dijkstra(int _) {
set<pii> st;
for (int i = 1; i <= 5 * n; i++) {
if (d[_][i]) st.insert({d[_][i], i});
else d[_][i] = maxN;
} while (!st.empty()) {
pii cur = *st.begin();
st.erase(st.begin());
int w = cur.ff, x = cur.sc;
for (int u : g[x]) {
if (u <= n && d[_][u] > w + 1) {
st.erase({d[_][u], u});
d[_][u] = w + 1;
st.insert({d[_][u], u});
} else if (u > n && d[_][u] > w) {
st.erase({d[_][u], u});
d[_][u] = w;
st.insert({d[_][u], u});
}
}
}
}
void solve() {
cin >> n;
build(1, 1, n);
int l[n + 1], r[n + 1];
for (int i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
update(1, 1, n, l[i], r[i], i);
} for (int i = 1; i <= n; i++)
if (l[i] == 1) d[1][i] = 1;
dijkstra(1);
for (int i = 1; i <= n; i++)
if (r[i] == n) d[2][i] = 1;
dijkstra(2);
for (int i = 1; i <= n; i++)
d[0][i] = d[1][i] + d[2][i] - 1;
dijkstra(0);
cin >> q;
while (q--) {
int x; cin >> x; cout << (d[0][x] > n + 5 ? -1 : d[0][x]) << '\n';
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << '\n';
}
return 0;
}
Compilation message
passport.cpp: In function 'void open_file(std::string)':
passport.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen((filename + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:28:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | freopen((filename + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
28752 KB |
Output is correct |
2 |
Correct |
6 ms |
28752 KB |
Output is correct |
3 |
Correct |
6 ms |
28752 KB |
Output is correct |
4 |
Correct |
738 ms |
103392 KB |
Output is correct |
5 |
Correct |
343 ms |
83528 KB |
Output is correct |
6 |
Correct |
220 ms |
78672 KB |
Output is correct |
7 |
Correct |
287 ms |
76588 KB |
Output is correct |
8 |
Correct |
151 ms |
79320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
28752 KB |
Output is correct |
2 |
Correct |
6 ms |
28720 KB |
Output is correct |
3 |
Correct |
6 ms |
28756 KB |
Output is correct |
4 |
Correct |
6 ms |
28756 KB |
Output is correct |
5 |
Correct |
6 ms |
28756 KB |
Output is correct |
6 |
Correct |
6 ms |
28628 KB |
Output is correct |
7 |
Correct |
7 ms |
28624 KB |
Output is correct |
8 |
Correct |
7 ms |
29012 KB |
Output is correct |
9 |
Correct |
6 ms |
28756 KB |
Output is correct |
10 |
Correct |
7 ms |
28768 KB |
Output is correct |
11 |
Correct |
7 ms |
28756 KB |
Output is correct |
12 |
Correct |
7 ms |
28756 KB |
Output is correct |
13 |
Correct |
7 ms |
28756 KB |
Output is correct |
14 |
Correct |
7 ms |
28756 KB |
Output is correct |
15 |
Correct |
7 ms |
28756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
28752 KB |
Output is correct |
2 |
Correct |
6 ms |
28720 KB |
Output is correct |
3 |
Correct |
6 ms |
28756 KB |
Output is correct |
4 |
Correct |
6 ms |
28756 KB |
Output is correct |
5 |
Correct |
6 ms |
28756 KB |
Output is correct |
6 |
Correct |
6 ms |
28628 KB |
Output is correct |
7 |
Correct |
7 ms |
28624 KB |
Output is correct |
8 |
Correct |
7 ms |
29012 KB |
Output is correct |
9 |
Correct |
6 ms |
28756 KB |
Output is correct |
10 |
Correct |
7 ms |
28768 KB |
Output is correct |
11 |
Correct |
7 ms |
28756 KB |
Output is correct |
12 |
Correct |
7 ms |
28756 KB |
Output is correct |
13 |
Correct |
7 ms |
28756 KB |
Output is correct |
14 |
Correct |
7 ms |
28756 KB |
Output is correct |
15 |
Correct |
7 ms |
28756 KB |
Output is correct |
16 |
Correct |
10 ms |
29268 KB |
Output is correct |
17 |
Correct |
9 ms |
29268 KB |
Output is correct |
18 |
Correct |
12 ms |
29536 KB |
Output is correct |
19 |
Correct |
11 ms |
29524 KB |
Output is correct |
20 |
Correct |
8 ms |
29268 KB |
Output is correct |
21 |
Correct |
9 ms |
29372 KB |
Output is correct |
22 |
Correct |
9 ms |
29432 KB |
Output is correct |
23 |
Correct |
10 ms |
29268 KB |
Output is correct |
24 |
Correct |
9 ms |
29268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
28752 KB |
Output is correct |
2 |
Correct |
6 ms |
28720 KB |
Output is correct |
3 |
Correct |
6 ms |
28756 KB |
Output is correct |
4 |
Correct |
6 ms |
28756 KB |
Output is correct |
5 |
Correct |
6 ms |
28756 KB |
Output is correct |
6 |
Correct |
6 ms |
28628 KB |
Output is correct |
7 |
Correct |
7 ms |
28624 KB |
Output is correct |
8 |
Correct |
7 ms |
29012 KB |
Output is correct |
9 |
Correct |
6 ms |
28756 KB |
Output is correct |
10 |
Correct |
7 ms |
28768 KB |
Output is correct |
11 |
Correct |
7 ms |
28756 KB |
Output is correct |
12 |
Correct |
7 ms |
28756 KB |
Output is correct |
13 |
Correct |
7 ms |
28756 KB |
Output is correct |
14 |
Correct |
7 ms |
28756 KB |
Output is correct |
15 |
Correct |
7 ms |
28756 KB |
Output is correct |
16 |
Correct |
10 ms |
29268 KB |
Output is correct |
17 |
Correct |
9 ms |
29268 KB |
Output is correct |
18 |
Correct |
12 ms |
29536 KB |
Output is correct |
19 |
Correct |
11 ms |
29524 KB |
Output is correct |
20 |
Correct |
8 ms |
29268 KB |
Output is correct |
21 |
Correct |
9 ms |
29372 KB |
Output is correct |
22 |
Correct |
9 ms |
29432 KB |
Output is correct |
23 |
Correct |
10 ms |
29268 KB |
Output is correct |
24 |
Correct |
9 ms |
29268 KB |
Output is correct |
25 |
Correct |
6 ms |
28888 KB |
Output is correct |
26 |
Correct |
6 ms |
28756 KB |
Output is correct |
27 |
Correct |
11 ms |
29268 KB |
Output is correct |
28 |
Correct |
10 ms |
29268 KB |
Output is correct |
29 |
Correct |
9 ms |
29268 KB |
Output is correct |
30 |
Correct |
9 ms |
29268 KB |
Output is correct |
31 |
Correct |
10 ms |
29268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
28752 KB |
Output is correct |
2 |
Correct |
6 ms |
28752 KB |
Output is correct |
3 |
Correct |
6 ms |
28752 KB |
Output is correct |
4 |
Correct |
738 ms |
103392 KB |
Output is correct |
5 |
Correct |
343 ms |
83528 KB |
Output is correct |
6 |
Correct |
220 ms |
78672 KB |
Output is correct |
7 |
Correct |
287 ms |
76588 KB |
Output is correct |
8 |
Correct |
151 ms |
79320 KB |
Output is correct |
9 |
Correct |
6 ms |
28752 KB |
Output is correct |
10 |
Correct |
6 ms |
28720 KB |
Output is correct |
11 |
Correct |
6 ms |
28756 KB |
Output is correct |
12 |
Correct |
6 ms |
28756 KB |
Output is correct |
13 |
Correct |
6 ms |
28756 KB |
Output is correct |
14 |
Correct |
6 ms |
28628 KB |
Output is correct |
15 |
Correct |
7 ms |
28624 KB |
Output is correct |
16 |
Correct |
7 ms |
29012 KB |
Output is correct |
17 |
Correct |
6 ms |
28756 KB |
Output is correct |
18 |
Correct |
7 ms |
28768 KB |
Output is correct |
19 |
Correct |
7 ms |
28756 KB |
Output is correct |
20 |
Correct |
7 ms |
28756 KB |
Output is correct |
21 |
Correct |
7 ms |
28756 KB |
Output is correct |
22 |
Correct |
7 ms |
28756 KB |
Output is correct |
23 |
Correct |
7 ms |
28756 KB |
Output is correct |
24 |
Correct |
10 ms |
29268 KB |
Output is correct |
25 |
Correct |
9 ms |
29268 KB |
Output is correct |
26 |
Correct |
12 ms |
29536 KB |
Output is correct |
27 |
Correct |
11 ms |
29524 KB |
Output is correct |
28 |
Correct |
8 ms |
29268 KB |
Output is correct |
29 |
Correct |
9 ms |
29372 KB |
Output is correct |
30 |
Correct |
9 ms |
29432 KB |
Output is correct |
31 |
Correct |
10 ms |
29268 KB |
Output is correct |
32 |
Correct |
9 ms |
29268 KB |
Output is correct |
33 |
Correct |
6 ms |
28888 KB |
Output is correct |
34 |
Correct |
6 ms |
28756 KB |
Output is correct |
35 |
Correct |
11 ms |
29268 KB |
Output is correct |
36 |
Correct |
10 ms |
29268 KB |
Output is correct |
37 |
Correct |
9 ms |
29268 KB |
Output is correct |
38 |
Correct |
9 ms |
29268 KB |
Output is correct |
39 |
Correct |
10 ms |
29268 KB |
Output is correct |
40 |
Correct |
789 ms |
107532 KB |
Output is correct |
41 |
Correct |
336 ms |
88908 KB |
Output is correct |
42 |
Correct |
436 ms |
115872 KB |
Output is correct |
43 |
Correct |
423 ms |
115616 KB |
Output is correct |
44 |
Correct |
234 ms |
83760 KB |
Output is correct |
45 |
Correct |
261 ms |
90884 KB |
Output is correct |
46 |
Correct |
144 ms |
50764 KB |
Output is correct |
47 |
Correct |
415 ms |
91004 KB |
Output is correct |
48 |
Correct |
315 ms |
98344 KB |
Output is correct |