#include <bits/stdc++.h>
using namespace std;
#define pii pair <int, int>
#define fi first
#define se second
#define mp make_pair
const int NM = 2e5, inf = 1e9+7;
int N, Q, L[NM+5], R[NM+5];
vector <pii> adj[4*NM+5];
int dist[2][4*NM+5];
priority_queue <pii, vector <pii>, greater <pii> > pq;
int res[4*NM+5];
void update(int id, int l, int r, int u, int v, int x){
if (v < l || u > r) return;
if (l >= u && r <= v){
if (l == r){
adj[l].push_back(mp(x, 1));
}
else{
adj[N+id].push_back(mp(x, 1));
}
return;
}
int mid = (l+r)/2;
update(2*id, l, mid, u, v, x);
update(2*id+1, mid+1, r, u, v, x);
}
void solve(int id, int l, int r){
if (l == r) return;
int mid = (l+r)/2;
solve(2*id, l, mid);
solve(2*id+1, mid+1, r);
adj[(l < mid ? N+2*id : l)].push_back(mp(N+id, 0));
adj[(mid+1 < r ? N+2*id+1 : r)].push_back(mp(N+id, 0));
}
void binary_bfs(int s, int dist[NM+5]){
for (int i = 1; i <= 4*N; i++) dist[i] = +inf;
dist[s] = 0;
pq.push(mp(0, s));
while (!pq.empty()){
int u = pq.top().se;
if (dist[u] != pq.top().fi){
pq.pop();
continue;
}
pq.pop();
for (pii p : adj[u]){
int v = p.fi, w = p.se;
if (dist[u]+w >= dist[v]) continue;
dist[v] = dist[u]+w;
pq.push(mp(dist[v], v));
}
}
}
void dijkstra(){
for (int i = 1; i <= 4*N; i++){
res[i] = min(dist[0][i]+dist[1][i]-(i > 1 && i < N), +inf);
pq.push(mp(res[i], i));
}
while (!pq.empty()){
int u = pq.top().se;
if (res[u] != pq.top().fi){
pq.pop();
continue;
}
pq.pop();
for (pii p : adj[u]){
int v = p.fi, w = p.se;
if (res[u]+w >= res[v]) continue;
res[v] = res[u]+w;
pq.push(mp(res[v], v));
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 1; i <= N; i++){
cin >> L[i] >> R[i];
update(1, 1, N, L[i], R[i], i);
}
solve(1, 1, N);
binary_bfs(1, dist[0]);
binary_bfs(N, dist[1]);
dijkstra();
cin >> Q;
while (Q--){
int X; cin >> X;
if (res[X] == +inf) cout << -1; else cout << res[X];
cout << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19256 KB |
Output is correct |
2 |
Correct |
8 ms |
19036 KB |
Output is correct |
3 |
Correct |
9 ms |
19036 KB |
Output is correct |
4 |
Correct |
546 ms |
77468 KB |
Output is correct |
5 |
Correct |
338 ms |
58308 KB |
Output is correct |
6 |
Correct |
171 ms |
53444 KB |
Output is correct |
7 |
Correct |
220 ms |
52540 KB |
Output is correct |
8 |
Correct |
159 ms |
58728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
19036 KB |
Output is correct |
2 |
Correct |
9 ms |
19036 KB |
Output is correct |
3 |
Correct |
8 ms |
19036 KB |
Output is correct |
4 |
Correct |
9 ms |
19056 KB |
Output is correct |
5 |
Correct |
8 ms |
19220 KB |
Output is correct |
6 |
Correct |
9 ms |
19032 KB |
Output is correct |
7 |
Correct |
14 ms |
19272 KB |
Output is correct |
8 |
Correct |
11 ms |
19036 KB |
Output is correct |
9 |
Correct |
9 ms |
19156 KB |
Output is correct |
10 |
Correct |
8 ms |
19036 KB |
Output is correct |
11 |
Correct |
8 ms |
19292 KB |
Output is correct |
12 |
Correct |
9 ms |
19356 KB |
Output is correct |
13 |
Correct |
9 ms |
19372 KB |
Output is correct |
14 |
Correct |
9 ms |
19288 KB |
Output is correct |
15 |
Correct |
13 ms |
19292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
19036 KB |
Output is correct |
2 |
Correct |
9 ms |
19036 KB |
Output is correct |
3 |
Correct |
8 ms |
19036 KB |
Output is correct |
4 |
Correct |
9 ms |
19056 KB |
Output is correct |
5 |
Correct |
8 ms |
19220 KB |
Output is correct |
6 |
Correct |
9 ms |
19032 KB |
Output is correct |
7 |
Correct |
14 ms |
19272 KB |
Output is correct |
8 |
Correct |
11 ms |
19036 KB |
Output is correct |
9 |
Correct |
9 ms |
19156 KB |
Output is correct |
10 |
Correct |
8 ms |
19036 KB |
Output is correct |
11 |
Correct |
8 ms |
19292 KB |
Output is correct |
12 |
Correct |
9 ms |
19356 KB |
Output is correct |
13 |
Correct |
9 ms |
19372 KB |
Output is correct |
14 |
Correct |
9 ms |
19288 KB |
Output is correct |
15 |
Correct |
13 ms |
19292 KB |
Output is correct |
16 |
Correct |
14 ms |
19880 KB |
Output is correct |
17 |
Correct |
12 ms |
19800 KB |
Output is correct |
18 |
Correct |
12 ms |
20060 KB |
Output is correct |
19 |
Correct |
13 ms |
20056 KB |
Output is correct |
20 |
Correct |
9 ms |
19732 KB |
Output is correct |
21 |
Correct |
10 ms |
19804 KB |
Output is correct |
22 |
Correct |
9 ms |
19804 KB |
Output is correct |
23 |
Correct |
10 ms |
19804 KB |
Output is correct |
24 |
Correct |
10 ms |
19816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
19036 KB |
Output is correct |
2 |
Correct |
9 ms |
19036 KB |
Output is correct |
3 |
Correct |
8 ms |
19036 KB |
Output is correct |
4 |
Correct |
9 ms |
19056 KB |
Output is correct |
5 |
Correct |
8 ms |
19220 KB |
Output is correct |
6 |
Correct |
9 ms |
19032 KB |
Output is correct |
7 |
Correct |
14 ms |
19272 KB |
Output is correct |
8 |
Correct |
11 ms |
19036 KB |
Output is correct |
9 |
Correct |
9 ms |
19156 KB |
Output is correct |
10 |
Correct |
8 ms |
19036 KB |
Output is correct |
11 |
Correct |
8 ms |
19292 KB |
Output is correct |
12 |
Correct |
9 ms |
19356 KB |
Output is correct |
13 |
Correct |
9 ms |
19372 KB |
Output is correct |
14 |
Correct |
9 ms |
19288 KB |
Output is correct |
15 |
Correct |
13 ms |
19292 KB |
Output is correct |
16 |
Correct |
14 ms |
19880 KB |
Output is correct |
17 |
Correct |
12 ms |
19800 KB |
Output is correct |
18 |
Correct |
12 ms |
20060 KB |
Output is correct |
19 |
Correct |
13 ms |
20056 KB |
Output is correct |
20 |
Correct |
9 ms |
19732 KB |
Output is correct |
21 |
Correct |
10 ms |
19804 KB |
Output is correct |
22 |
Correct |
9 ms |
19804 KB |
Output is correct |
23 |
Correct |
10 ms |
19804 KB |
Output is correct |
24 |
Correct |
10 ms |
19816 KB |
Output is correct |
25 |
Correct |
7 ms |
19036 KB |
Output is correct |
26 |
Correct |
7 ms |
19036 KB |
Output is correct |
27 |
Correct |
11 ms |
19804 KB |
Output is correct |
28 |
Correct |
11 ms |
19700 KB |
Output is correct |
29 |
Correct |
11 ms |
19788 KB |
Output is correct |
30 |
Correct |
11 ms |
19804 KB |
Output is correct |
31 |
Correct |
11 ms |
19800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19256 KB |
Output is correct |
2 |
Correct |
8 ms |
19036 KB |
Output is correct |
3 |
Correct |
9 ms |
19036 KB |
Output is correct |
4 |
Correct |
546 ms |
77468 KB |
Output is correct |
5 |
Correct |
338 ms |
58308 KB |
Output is correct |
6 |
Correct |
171 ms |
53444 KB |
Output is correct |
7 |
Correct |
220 ms |
52540 KB |
Output is correct |
8 |
Correct |
159 ms |
58728 KB |
Output is correct |
9 |
Correct |
11 ms |
19036 KB |
Output is correct |
10 |
Correct |
9 ms |
19036 KB |
Output is correct |
11 |
Correct |
8 ms |
19036 KB |
Output is correct |
12 |
Correct |
9 ms |
19056 KB |
Output is correct |
13 |
Correct |
8 ms |
19220 KB |
Output is correct |
14 |
Correct |
9 ms |
19032 KB |
Output is correct |
15 |
Correct |
14 ms |
19272 KB |
Output is correct |
16 |
Correct |
11 ms |
19036 KB |
Output is correct |
17 |
Correct |
9 ms |
19156 KB |
Output is correct |
18 |
Correct |
8 ms |
19036 KB |
Output is correct |
19 |
Correct |
8 ms |
19292 KB |
Output is correct |
20 |
Correct |
9 ms |
19356 KB |
Output is correct |
21 |
Correct |
9 ms |
19372 KB |
Output is correct |
22 |
Correct |
9 ms |
19288 KB |
Output is correct |
23 |
Correct |
13 ms |
19292 KB |
Output is correct |
24 |
Correct |
14 ms |
19880 KB |
Output is correct |
25 |
Correct |
12 ms |
19800 KB |
Output is correct |
26 |
Correct |
12 ms |
20060 KB |
Output is correct |
27 |
Correct |
13 ms |
20056 KB |
Output is correct |
28 |
Correct |
9 ms |
19732 KB |
Output is correct |
29 |
Correct |
10 ms |
19804 KB |
Output is correct |
30 |
Correct |
9 ms |
19804 KB |
Output is correct |
31 |
Correct |
10 ms |
19804 KB |
Output is correct |
32 |
Correct |
10 ms |
19816 KB |
Output is correct |
33 |
Correct |
7 ms |
19036 KB |
Output is correct |
34 |
Correct |
7 ms |
19036 KB |
Output is correct |
35 |
Correct |
11 ms |
19804 KB |
Output is correct |
36 |
Correct |
11 ms |
19700 KB |
Output is correct |
37 |
Correct |
11 ms |
19788 KB |
Output is correct |
38 |
Correct |
11 ms |
19804 KB |
Output is correct |
39 |
Correct |
11 ms |
19800 KB |
Output is correct |
40 |
Correct |
574 ms |
79256 KB |
Output is correct |
41 |
Correct |
341 ms |
60076 KB |
Output is correct |
42 |
Correct |
424 ms |
84236 KB |
Output is correct |
43 |
Correct |
432 ms |
84248 KB |
Output is correct |
44 |
Correct |
208 ms |
55220 KB |
Output is correct |
45 |
Correct |
255 ms |
61888 KB |
Output is correct |
46 |
Correct |
119 ms |
35260 KB |
Output is correct |
47 |
Correct |
381 ms |
62000 KB |
Output is correct |
48 |
Correct |
310 ms |
70180 KB |
Output is correct |