#include<bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pii pair<int, int>
using namespace std;
const int N = (1 << 18);
const int INF = 1e9;
int n, q;
vector<pii> gr[3 * N];
void build (int l = 1, int r = N, int v = 1) {
if (l == r) {
gr[l].push_back({v + N, 0});
return;
}
int mid = (l + r) >> 1;
build(l, mid, v << 1);
build(mid + 1, r, v << 1 ^ 1);
gr[(v << 1) + N].push_back({v + N, 0});
gr[((v << 1) ^ 1) + N].push_back({v + N, 0});
}
void add_road (int l1, int r1, int x, int v = 1, int l = 1, int r = N) {
if (l > r1 || r < l1)
return;
if (l1 <= l && r <= r1) {
gr[v + N].push_back({x, 1});
return;
}
int mid = (l + r) >> 1;
add_road (l1, r1, x, v << 1, l, mid);
add_road (l1, r1, x, v << 1 ^ 1, mid + 1, r);
}
vector<int> deikstra (vector<pii> v) {
vector<int> dist(3 * N, INF);
for (auto i : v)
dist[i.fi] = i.se;
set<pii> st;
for (auto i = 1; i < 3 * N; ++i)
st.insert({dist[i], i});
while (st.size()) {
auto p = *st.begin();
st.erase(st.begin());
for (auto i : gr[p.se])
if (dist[i.fi] > p.fi + i.se) {
st.erase({dist[i.fi], i.fi});
dist[i.fi] = p.fi + i.se;
st.insert({dist[i.fi], i.fi});
}
}
vector<int> now(n + 1);
for (int i = 1; i <= n; ++i)
now[i] = dist[i];
return now;
}
signed main () {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
build();
cin >> n;
for (int i = 1; i <= n; ++i) {
int l, r;
cin >> l >> r;
add_road (l, r, i);
}
vector<int> dist_from_n = deikstra({{n, 0}});
vector<int> dist_from_one = deikstra({{1, 0}});
vector<pii> v;
for (int i = 1; i <= n; ++i)
v.push_back({i, dist_from_n[i] + dist_from_one[i] - (1 < i && i < n)});
vector<int> ans = deikstra(v);
cin >> q;
while (q--) {
int x;
cin >> x;
if (ans[x] == INF)
cout << "-1\n";
else
cout << ans[x] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
545 ms |
83608 KB |
Output is correct |
2 |
Correct |
505 ms |
83540 KB |
Output is correct |
3 |
Correct |
496 ms |
83536 KB |
Output is correct |
4 |
Correct |
1414 ms |
119596 KB |
Output is correct |
5 |
Correct |
1054 ms |
99984 KB |
Output is correct |
6 |
Correct |
873 ms |
96164 KB |
Output is correct |
7 |
Correct |
993 ms |
107236 KB |
Output is correct |
8 |
Correct |
705 ms |
108604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
524 ms |
83480 KB |
Output is correct |
2 |
Correct |
491 ms |
83752 KB |
Output is correct |
3 |
Correct |
493 ms |
83772 KB |
Output is correct |
4 |
Correct |
496 ms |
83448 KB |
Output is correct |
5 |
Correct |
489 ms |
83792 KB |
Output is correct |
6 |
Correct |
501 ms |
83776 KB |
Output is correct |
7 |
Correct |
505 ms |
83540 KB |
Output is correct |
8 |
Correct |
499 ms |
83776 KB |
Output is correct |
9 |
Correct |
493 ms |
83532 KB |
Output is correct |
10 |
Correct |
480 ms |
83540 KB |
Output is correct |
11 |
Correct |
498 ms |
83552 KB |
Output is correct |
12 |
Correct |
501 ms |
83560 KB |
Output is correct |
13 |
Correct |
504 ms |
83712 KB |
Output is correct |
14 |
Correct |
491 ms |
83460 KB |
Output is correct |
15 |
Correct |
489 ms |
83408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
524 ms |
83480 KB |
Output is correct |
2 |
Correct |
491 ms |
83752 KB |
Output is correct |
3 |
Correct |
493 ms |
83772 KB |
Output is correct |
4 |
Correct |
496 ms |
83448 KB |
Output is correct |
5 |
Correct |
489 ms |
83792 KB |
Output is correct |
6 |
Correct |
501 ms |
83776 KB |
Output is correct |
7 |
Correct |
505 ms |
83540 KB |
Output is correct |
8 |
Correct |
499 ms |
83776 KB |
Output is correct |
9 |
Correct |
493 ms |
83532 KB |
Output is correct |
10 |
Correct |
480 ms |
83540 KB |
Output is correct |
11 |
Correct |
498 ms |
83552 KB |
Output is correct |
12 |
Correct |
501 ms |
83560 KB |
Output is correct |
13 |
Correct |
504 ms |
83712 KB |
Output is correct |
14 |
Correct |
491 ms |
83460 KB |
Output is correct |
15 |
Correct |
489 ms |
83408 KB |
Output is correct |
16 |
Correct |
512 ms |
83688 KB |
Output is correct |
17 |
Correct |
512 ms |
83744 KB |
Output is correct |
18 |
Correct |
508 ms |
83880 KB |
Output is correct |
19 |
Correct |
502 ms |
83864 KB |
Output is correct |
20 |
Correct |
501 ms |
83636 KB |
Output is correct |
21 |
Correct |
504 ms |
83708 KB |
Output is correct |
22 |
Correct |
507 ms |
83788 KB |
Output is correct |
23 |
Correct |
506 ms |
83556 KB |
Output is correct |
24 |
Correct |
502 ms |
83760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
524 ms |
83480 KB |
Output is correct |
2 |
Correct |
491 ms |
83752 KB |
Output is correct |
3 |
Correct |
493 ms |
83772 KB |
Output is correct |
4 |
Correct |
496 ms |
83448 KB |
Output is correct |
5 |
Correct |
489 ms |
83792 KB |
Output is correct |
6 |
Correct |
501 ms |
83776 KB |
Output is correct |
7 |
Correct |
505 ms |
83540 KB |
Output is correct |
8 |
Correct |
499 ms |
83776 KB |
Output is correct |
9 |
Correct |
493 ms |
83532 KB |
Output is correct |
10 |
Correct |
480 ms |
83540 KB |
Output is correct |
11 |
Correct |
498 ms |
83552 KB |
Output is correct |
12 |
Correct |
501 ms |
83560 KB |
Output is correct |
13 |
Correct |
504 ms |
83712 KB |
Output is correct |
14 |
Correct |
491 ms |
83460 KB |
Output is correct |
15 |
Correct |
489 ms |
83408 KB |
Output is correct |
16 |
Correct |
512 ms |
83688 KB |
Output is correct |
17 |
Correct |
512 ms |
83744 KB |
Output is correct |
18 |
Correct |
508 ms |
83880 KB |
Output is correct |
19 |
Correct |
502 ms |
83864 KB |
Output is correct |
20 |
Correct |
501 ms |
83636 KB |
Output is correct |
21 |
Correct |
504 ms |
83708 KB |
Output is correct |
22 |
Correct |
507 ms |
83788 KB |
Output is correct |
23 |
Correct |
506 ms |
83556 KB |
Output is correct |
24 |
Correct |
502 ms |
83760 KB |
Output is correct |
25 |
Correct |
499 ms |
83524 KB |
Output is correct |
26 |
Correct |
512 ms |
83540 KB |
Output is correct |
27 |
Incorrect |
501 ms |
83776 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
545 ms |
83608 KB |
Output is correct |
2 |
Correct |
505 ms |
83540 KB |
Output is correct |
3 |
Correct |
496 ms |
83536 KB |
Output is correct |
4 |
Correct |
1414 ms |
119596 KB |
Output is correct |
5 |
Correct |
1054 ms |
99984 KB |
Output is correct |
6 |
Correct |
873 ms |
96164 KB |
Output is correct |
7 |
Correct |
993 ms |
107236 KB |
Output is correct |
8 |
Correct |
705 ms |
108604 KB |
Output is correct |
9 |
Correct |
524 ms |
83480 KB |
Output is correct |
10 |
Correct |
491 ms |
83752 KB |
Output is correct |
11 |
Correct |
493 ms |
83772 KB |
Output is correct |
12 |
Correct |
496 ms |
83448 KB |
Output is correct |
13 |
Correct |
489 ms |
83792 KB |
Output is correct |
14 |
Correct |
501 ms |
83776 KB |
Output is correct |
15 |
Correct |
505 ms |
83540 KB |
Output is correct |
16 |
Correct |
499 ms |
83776 KB |
Output is correct |
17 |
Correct |
493 ms |
83532 KB |
Output is correct |
18 |
Correct |
480 ms |
83540 KB |
Output is correct |
19 |
Correct |
498 ms |
83552 KB |
Output is correct |
20 |
Correct |
501 ms |
83560 KB |
Output is correct |
21 |
Correct |
504 ms |
83712 KB |
Output is correct |
22 |
Correct |
491 ms |
83460 KB |
Output is correct |
23 |
Correct |
489 ms |
83408 KB |
Output is correct |
24 |
Correct |
512 ms |
83688 KB |
Output is correct |
25 |
Correct |
512 ms |
83744 KB |
Output is correct |
26 |
Correct |
508 ms |
83880 KB |
Output is correct |
27 |
Correct |
502 ms |
83864 KB |
Output is correct |
28 |
Correct |
501 ms |
83636 KB |
Output is correct |
29 |
Correct |
504 ms |
83708 KB |
Output is correct |
30 |
Correct |
507 ms |
83788 KB |
Output is correct |
31 |
Correct |
506 ms |
83556 KB |
Output is correct |
32 |
Correct |
502 ms |
83760 KB |
Output is correct |
33 |
Correct |
499 ms |
83524 KB |
Output is correct |
34 |
Correct |
512 ms |
83540 KB |
Output is correct |
35 |
Incorrect |
501 ms |
83776 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |