#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9;
const int M = (1 << 18);
vector<pair<int, int>> g[3 * M];
void build(int l = 0, int r = M - 1, int v = 1) {
if(l == r) {
g[l].push_back({v + M, 0});
return;
}
int mid = (l + r) / 2;
build(l, mid, 2 * v);
build(mid + 1, r, 2 * v + 1);
g[2 * v + M].push_back({v + M, 0});
g[2 * v + M + 1].push_back({v + M, 0});
}
void add(int ql, int qr, int u, int l = 0, int r = M - 1, int v = 1) {
if(qr < l || ql > r)
return;
if(ql <= l && r <= qr) {
g[v + M].push_back({u, 1});
return;
}
int mid = (l + r) / 2;
add(ql, qr, u, l, mid, 2 * v);
add(ql, qr, u, mid + 1, r, 2 * v + 1);
}
int n;
vector<ll> dijkstra(vector<pair<int, ll>> start) {
vector<ll> dist(3 * M, INF + 10);
for(auto [i, d] : start) dist[i] = d;
set<pair<ll, int>> st;
for(int i = 0; i < (int)dist.size(); i++)
st.insert({dist[i], i});
while(!st.empty()) {
int s = st.begin()->second;
st.erase(st.begin());
for(auto [to, w] : g[s]) {
if(dist[to] > dist[s] + w) {
st.erase({dist[to], to});
dist[to] = dist[s] + w;
st.insert({dist[to], to});
}
}
}
vector<ll> res(n + 1);
for(int i = 1; i <= n; i++)
res[i] = dist[i];
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
build();
cin >> n;
int l[n + 1], r[n + 1];
for(int i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
add(l[i], r[i], i);
}
auto A = dijkstra({{1, 0}});
auto B = dijkstra({{n, 0}});
// for(int i = 1; i <= n; i++)
// cout << A[i] << ' ';
// cout << '\n';
// for(int i = 1; i <= n; i++)
// cout << B[i] << ' ';
// cout << '\n';
vector<pair<int, ll>> C;
for(int i = 1; i <= n; i++)
C.push_back({i, A[i] + B[i] - (1 < i && i < n)});
auto F = dijkstra(C);
// for(int i = 1; i <= n; i++)
// cout << F[i] << ' ';
// cout << '\n';
int q;
cin >> q;
while(q--) {
int x;
cin >> x;
if(F[x] >= INF) cout << -1 << '\n';
else cout << F[x] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
585 ms |
98780 KB |
Output is correct |
2 |
Correct |
568 ms |
98776 KB |
Output is correct |
3 |
Correct |
558 ms |
98904 KB |
Output is correct |
4 |
Correct |
1492 ms |
139092 KB |
Output is correct |
5 |
Correct |
1084 ms |
121904 KB |
Output is correct |
6 |
Correct |
919 ms |
118064 KB |
Output is correct |
7 |
Correct |
1237 ms |
155088 KB |
Output is correct |
8 |
Correct |
834 ms |
138808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
571 ms |
98764 KB |
Output is correct |
2 |
Correct |
580 ms |
98780 KB |
Output is correct |
3 |
Correct |
549 ms |
98848 KB |
Output is correct |
4 |
Correct |
551 ms |
98836 KB |
Output is correct |
5 |
Correct |
568 ms |
98792 KB |
Output is correct |
6 |
Correct |
564 ms |
98764 KB |
Output is correct |
7 |
Correct |
592 ms |
98828 KB |
Output is correct |
8 |
Correct |
563 ms |
98764 KB |
Output is correct |
9 |
Correct |
554 ms |
98764 KB |
Output is correct |
10 |
Correct |
550 ms |
98788 KB |
Output is correct |
11 |
Correct |
562 ms |
98816 KB |
Output is correct |
12 |
Correct |
590 ms |
98828 KB |
Output is correct |
13 |
Correct |
592 ms |
98764 KB |
Output is correct |
14 |
Correct |
561 ms |
98876 KB |
Output is correct |
15 |
Correct |
573 ms |
98796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
571 ms |
98764 KB |
Output is correct |
2 |
Correct |
580 ms |
98780 KB |
Output is correct |
3 |
Correct |
549 ms |
98848 KB |
Output is correct |
4 |
Correct |
551 ms |
98836 KB |
Output is correct |
5 |
Correct |
568 ms |
98792 KB |
Output is correct |
6 |
Correct |
564 ms |
98764 KB |
Output is correct |
7 |
Correct |
592 ms |
98828 KB |
Output is correct |
8 |
Correct |
563 ms |
98764 KB |
Output is correct |
9 |
Correct |
554 ms |
98764 KB |
Output is correct |
10 |
Correct |
550 ms |
98788 KB |
Output is correct |
11 |
Correct |
562 ms |
98816 KB |
Output is correct |
12 |
Correct |
590 ms |
98828 KB |
Output is correct |
13 |
Correct |
592 ms |
98764 KB |
Output is correct |
14 |
Correct |
561 ms |
98876 KB |
Output is correct |
15 |
Correct |
573 ms |
98796 KB |
Output is correct |
16 |
Correct |
564 ms |
99148 KB |
Output is correct |
17 |
Correct |
554 ms |
99140 KB |
Output is correct |
18 |
Correct |
570 ms |
99328 KB |
Output is correct |
19 |
Correct |
577 ms |
99180 KB |
Output is correct |
20 |
Correct |
551 ms |
99116 KB |
Output is correct |
21 |
Correct |
551 ms |
99024 KB |
Output is correct |
22 |
Correct |
563 ms |
99244 KB |
Output is correct |
23 |
Correct |
587 ms |
99128 KB |
Output is correct |
24 |
Correct |
566 ms |
99172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
571 ms |
98764 KB |
Output is correct |
2 |
Correct |
580 ms |
98780 KB |
Output is correct |
3 |
Correct |
549 ms |
98848 KB |
Output is correct |
4 |
Correct |
551 ms |
98836 KB |
Output is correct |
5 |
Correct |
568 ms |
98792 KB |
Output is correct |
6 |
Correct |
564 ms |
98764 KB |
Output is correct |
7 |
Correct |
592 ms |
98828 KB |
Output is correct |
8 |
Correct |
563 ms |
98764 KB |
Output is correct |
9 |
Correct |
554 ms |
98764 KB |
Output is correct |
10 |
Correct |
550 ms |
98788 KB |
Output is correct |
11 |
Correct |
562 ms |
98816 KB |
Output is correct |
12 |
Correct |
590 ms |
98828 KB |
Output is correct |
13 |
Correct |
592 ms |
98764 KB |
Output is correct |
14 |
Correct |
561 ms |
98876 KB |
Output is correct |
15 |
Correct |
573 ms |
98796 KB |
Output is correct |
16 |
Correct |
564 ms |
99148 KB |
Output is correct |
17 |
Correct |
554 ms |
99140 KB |
Output is correct |
18 |
Correct |
570 ms |
99328 KB |
Output is correct |
19 |
Correct |
577 ms |
99180 KB |
Output is correct |
20 |
Correct |
551 ms |
99116 KB |
Output is correct |
21 |
Correct |
551 ms |
99024 KB |
Output is correct |
22 |
Correct |
563 ms |
99244 KB |
Output is correct |
23 |
Correct |
587 ms |
99128 KB |
Output is correct |
24 |
Correct |
566 ms |
99172 KB |
Output is correct |
25 |
Correct |
589 ms |
98776 KB |
Output is correct |
26 |
Correct |
549 ms |
98760 KB |
Output is correct |
27 |
Correct |
558 ms |
99276 KB |
Output is correct |
28 |
Correct |
546 ms |
99052 KB |
Output is correct |
29 |
Correct |
582 ms |
99124 KB |
Output is correct |
30 |
Correct |
584 ms |
99068 KB |
Output is correct |
31 |
Correct |
559 ms |
99112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
585 ms |
98780 KB |
Output is correct |
2 |
Correct |
568 ms |
98776 KB |
Output is correct |
3 |
Correct |
558 ms |
98904 KB |
Output is correct |
4 |
Correct |
1492 ms |
139092 KB |
Output is correct |
5 |
Correct |
1084 ms |
121904 KB |
Output is correct |
6 |
Correct |
919 ms |
118064 KB |
Output is correct |
7 |
Correct |
1237 ms |
155088 KB |
Output is correct |
8 |
Correct |
834 ms |
138808 KB |
Output is correct |
9 |
Correct |
571 ms |
98764 KB |
Output is correct |
10 |
Correct |
580 ms |
98780 KB |
Output is correct |
11 |
Correct |
549 ms |
98848 KB |
Output is correct |
12 |
Correct |
551 ms |
98836 KB |
Output is correct |
13 |
Correct |
568 ms |
98792 KB |
Output is correct |
14 |
Correct |
564 ms |
98764 KB |
Output is correct |
15 |
Correct |
592 ms |
98828 KB |
Output is correct |
16 |
Correct |
563 ms |
98764 KB |
Output is correct |
17 |
Correct |
554 ms |
98764 KB |
Output is correct |
18 |
Correct |
550 ms |
98788 KB |
Output is correct |
19 |
Correct |
562 ms |
98816 KB |
Output is correct |
20 |
Correct |
590 ms |
98828 KB |
Output is correct |
21 |
Correct |
592 ms |
98764 KB |
Output is correct |
22 |
Correct |
561 ms |
98876 KB |
Output is correct |
23 |
Correct |
573 ms |
98796 KB |
Output is correct |
24 |
Correct |
564 ms |
99148 KB |
Output is correct |
25 |
Correct |
554 ms |
99140 KB |
Output is correct |
26 |
Correct |
570 ms |
99328 KB |
Output is correct |
27 |
Correct |
577 ms |
99180 KB |
Output is correct |
28 |
Correct |
551 ms |
99116 KB |
Output is correct |
29 |
Correct |
551 ms |
99024 KB |
Output is correct |
30 |
Correct |
563 ms |
99244 KB |
Output is correct |
31 |
Correct |
587 ms |
99128 KB |
Output is correct |
32 |
Correct |
566 ms |
99172 KB |
Output is correct |
33 |
Correct |
589 ms |
98776 KB |
Output is correct |
34 |
Correct |
549 ms |
98760 KB |
Output is correct |
35 |
Correct |
558 ms |
99276 KB |
Output is correct |
36 |
Correct |
546 ms |
99052 KB |
Output is correct |
37 |
Correct |
582 ms |
99124 KB |
Output is correct |
38 |
Correct |
584 ms |
99068 KB |
Output is correct |
39 |
Correct |
559 ms |
99112 KB |
Output is correct |
40 |
Correct |
1592 ms |
143204 KB |
Output is correct |
41 |
Correct |
1137 ms |
124604 KB |
Output is correct |
42 |
Correct |
1303 ms |
151608 KB |
Output is correct |
43 |
Correct |
1185 ms |
151036 KB |
Output is correct |
44 |
Correct |
950 ms |
118064 KB |
Output is correct |
45 |
Correct |
995 ms |
124600 KB |
Output is correct |
46 |
Correct |
829 ms |
110880 KB |
Output is correct |
47 |
Correct |
1387 ms |
136356 KB |
Output is correct |
48 |
Correct |
1034 ms |
135488 KB |
Output is correct |