#include<bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pll pair<ll, ll>
using namespace std;
const ll N = (1 << 18);
const ll INF = 1e18;
ll n, q;
vector<pll> gr[3 * N + 10];
void build (ll l = 1, ll r = N, ll v = 1) {
if (l == r) {
gr[l].push_back({v + N, 0});
return;
}
ll 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 (ll l1, ll r1, ll x, ll v = 1, ll l = 1, ll r = N) {
if (l > r1 || r < l1)
return;
if (l1 <= l && r <= r1) {
gr[v + N].push_back({x, 1});
return;
}
ll 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<ll> deikstra (vector<pll> v) {
vector<ll> dist(3 * N, INF);
for (auto i : v)
dist[i.fi] = i.se;
set<pll> st;
for (auto i = 0; 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<ll> now(n + 1);
for (ll 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 (ll i = 1; i <= n; ++i) {
ll l, r;
cin >> l >> r;
add_road (l, r, i);
}
vector<ll> dist_from_n = deikstra({{n, 0}});
vector<ll> dist_from_one = deikstra({{1, 0}});
vector<pll> v;
for (ll i = 1; i <= n; ++i)
v.push_back({i, dist_from_n[i] + dist_from_one[i] - (1 < i && i < n)});
vector<ll> ans = deikstra(v);
cin >> q;
while (q--) {
ll 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 |
551 ms |
98852 KB |
Output is correct |
2 |
Correct |
540 ms |
98920 KB |
Output is correct |
3 |
Correct |
552 ms |
99168 KB |
Output is correct |
4 |
Correct |
1498 ms |
165808 KB |
Output is correct |
5 |
Correct |
1104 ms |
128100 KB |
Output is correct |
6 |
Correct |
950 ms |
118252 KB |
Output is correct |
7 |
Correct |
1008 ms |
142984 KB |
Output is correct |
8 |
Correct |
785 ms |
144548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
555 ms |
98924 KB |
Output is correct |
2 |
Correct |
569 ms |
98900 KB |
Output is correct |
3 |
Correct |
544 ms |
99104 KB |
Output is correct |
4 |
Correct |
550 ms |
98924 KB |
Output is correct |
5 |
Correct |
544 ms |
98804 KB |
Output is correct |
6 |
Correct |
545 ms |
98916 KB |
Output is correct |
7 |
Correct |
544 ms |
98788 KB |
Output is correct |
8 |
Correct |
545 ms |
98900 KB |
Output is correct |
9 |
Correct |
549 ms |
98924 KB |
Output is correct |
10 |
Correct |
541 ms |
99132 KB |
Output is correct |
11 |
Correct |
534 ms |
98960 KB |
Output is correct |
12 |
Correct |
570 ms |
98824 KB |
Output is correct |
13 |
Correct |
552 ms |
99152 KB |
Output is correct |
14 |
Correct |
548 ms |
98968 KB |
Output is correct |
15 |
Correct |
536 ms |
98892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
555 ms |
98924 KB |
Output is correct |
2 |
Correct |
569 ms |
98900 KB |
Output is correct |
3 |
Correct |
544 ms |
99104 KB |
Output is correct |
4 |
Correct |
550 ms |
98924 KB |
Output is correct |
5 |
Correct |
544 ms |
98804 KB |
Output is correct |
6 |
Correct |
545 ms |
98916 KB |
Output is correct |
7 |
Correct |
544 ms |
98788 KB |
Output is correct |
8 |
Correct |
545 ms |
98900 KB |
Output is correct |
9 |
Correct |
549 ms |
98924 KB |
Output is correct |
10 |
Correct |
541 ms |
99132 KB |
Output is correct |
11 |
Correct |
534 ms |
98960 KB |
Output is correct |
12 |
Correct |
570 ms |
98824 KB |
Output is correct |
13 |
Correct |
552 ms |
99152 KB |
Output is correct |
14 |
Correct |
548 ms |
98968 KB |
Output is correct |
15 |
Correct |
536 ms |
98892 KB |
Output is correct |
16 |
Correct |
577 ms |
99488 KB |
Output is correct |
17 |
Correct |
573 ms |
99516 KB |
Output is correct |
18 |
Correct |
561 ms |
99484 KB |
Output is correct |
19 |
Correct |
562 ms |
99868 KB |
Output is correct |
20 |
Correct |
569 ms |
99108 KB |
Output is correct |
21 |
Correct |
570 ms |
99532 KB |
Output is correct |
22 |
Correct |
564 ms |
99484 KB |
Output is correct |
23 |
Correct |
589 ms |
99532 KB |
Output is correct |
24 |
Correct |
563 ms |
99352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
555 ms |
98924 KB |
Output is correct |
2 |
Correct |
569 ms |
98900 KB |
Output is correct |
3 |
Correct |
544 ms |
99104 KB |
Output is correct |
4 |
Correct |
550 ms |
98924 KB |
Output is correct |
5 |
Correct |
544 ms |
98804 KB |
Output is correct |
6 |
Correct |
545 ms |
98916 KB |
Output is correct |
7 |
Correct |
544 ms |
98788 KB |
Output is correct |
8 |
Correct |
545 ms |
98900 KB |
Output is correct |
9 |
Correct |
549 ms |
98924 KB |
Output is correct |
10 |
Correct |
541 ms |
99132 KB |
Output is correct |
11 |
Correct |
534 ms |
98960 KB |
Output is correct |
12 |
Correct |
570 ms |
98824 KB |
Output is correct |
13 |
Correct |
552 ms |
99152 KB |
Output is correct |
14 |
Correct |
548 ms |
98968 KB |
Output is correct |
15 |
Correct |
536 ms |
98892 KB |
Output is correct |
16 |
Correct |
577 ms |
99488 KB |
Output is correct |
17 |
Correct |
573 ms |
99516 KB |
Output is correct |
18 |
Correct |
561 ms |
99484 KB |
Output is correct |
19 |
Correct |
562 ms |
99868 KB |
Output is correct |
20 |
Correct |
569 ms |
99108 KB |
Output is correct |
21 |
Correct |
570 ms |
99532 KB |
Output is correct |
22 |
Correct |
564 ms |
99484 KB |
Output is correct |
23 |
Correct |
589 ms |
99532 KB |
Output is correct |
24 |
Correct |
563 ms |
99352 KB |
Output is correct |
25 |
Correct |
544 ms |
98900 KB |
Output is correct |
26 |
Correct |
558 ms |
98944 KB |
Output is correct |
27 |
Correct |
587 ms |
99280 KB |
Output is correct |
28 |
Correct |
620 ms |
99216 KB |
Output is correct |
29 |
Correct |
577 ms |
99216 KB |
Output is correct |
30 |
Correct |
554 ms |
99528 KB |
Output is correct |
31 |
Correct |
585 ms |
99296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
551 ms |
98852 KB |
Output is correct |
2 |
Correct |
540 ms |
98920 KB |
Output is correct |
3 |
Correct |
552 ms |
99168 KB |
Output is correct |
4 |
Correct |
1498 ms |
165808 KB |
Output is correct |
5 |
Correct |
1104 ms |
128100 KB |
Output is correct |
6 |
Correct |
950 ms |
118252 KB |
Output is correct |
7 |
Correct |
1008 ms |
142984 KB |
Output is correct |
8 |
Correct |
785 ms |
144548 KB |
Output is correct |
9 |
Correct |
555 ms |
98924 KB |
Output is correct |
10 |
Correct |
569 ms |
98900 KB |
Output is correct |
11 |
Correct |
544 ms |
99104 KB |
Output is correct |
12 |
Correct |
550 ms |
98924 KB |
Output is correct |
13 |
Correct |
544 ms |
98804 KB |
Output is correct |
14 |
Correct |
545 ms |
98916 KB |
Output is correct |
15 |
Correct |
544 ms |
98788 KB |
Output is correct |
16 |
Correct |
545 ms |
98900 KB |
Output is correct |
17 |
Correct |
549 ms |
98924 KB |
Output is correct |
18 |
Correct |
541 ms |
99132 KB |
Output is correct |
19 |
Correct |
534 ms |
98960 KB |
Output is correct |
20 |
Correct |
570 ms |
98824 KB |
Output is correct |
21 |
Correct |
552 ms |
99152 KB |
Output is correct |
22 |
Correct |
548 ms |
98968 KB |
Output is correct |
23 |
Correct |
536 ms |
98892 KB |
Output is correct |
24 |
Correct |
577 ms |
99488 KB |
Output is correct |
25 |
Correct |
573 ms |
99516 KB |
Output is correct |
26 |
Correct |
561 ms |
99484 KB |
Output is correct |
27 |
Correct |
562 ms |
99868 KB |
Output is correct |
28 |
Correct |
569 ms |
99108 KB |
Output is correct |
29 |
Correct |
570 ms |
99532 KB |
Output is correct |
30 |
Correct |
564 ms |
99484 KB |
Output is correct |
31 |
Correct |
589 ms |
99532 KB |
Output is correct |
32 |
Correct |
563 ms |
99352 KB |
Output is correct |
33 |
Correct |
544 ms |
98900 KB |
Output is correct |
34 |
Correct |
558 ms |
98944 KB |
Output is correct |
35 |
Correct |
587 ms |
99280 KB |
Output is correct |
36 |
Correct |
620 ms |
99216 KB |
Output is correct |
37 |
Correct |
577 ms |
99216 KB |
Output is correct |
38 |
Correct |
554 ms |
99528 KB |
Output is correct |
39 |
Correct |
585 ms |
99296 KB |
Output is correct |
40 |
Correct |
1631 ms |
170116 KB |
Output is correct |
41 |
Correct |
1172 ms |
133268 KB |
Output is correct |
42 |
Correct |
1276 ms |
187716 KB |
Output is correct |
43 |
Correct |
1210 ms |
186404 KB |
Output is correct |
44 |
Correct |
1057 ms |
120476 KB |
Output is correct |
45 |
Correct |
1064 ms |
136428 KB |
Output is correct |
46 |
Correct |
871 ms |
113360 KB |
Output is correct |
47 |
Correct |
1341 ms |
145736 KB |
Output is correct |
48 |
Correct |
1083 ms |
152620 KB |
Output is correct |