#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0);
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define int long long
#define lowbit(x) x&-x
const int maxn = 2e5 + 5;
const int INF = 1e9 + 7;
int l[maxn], r[maxn], ans[maxn];
int d0[maxn], d1[maxn];
vector<pll> adj[maxn * 4];
int idx[maxn];
int n;
void build(int v = 1, int l = 0, int r = n - 1){
if(l == r){
idx[l] = v;
return;
}
int m = (l + r) / 2;
adj[v * 2].eb(v, 0);
adj[v * 2 + 1].eb(v, 0);
build(v * 2, l, m);
build(v * 2 + 1, m + 1, r);
}
void add(int id, int l, int r, int v = 1, int L = 0, int R = n - 1){
if(l > R || r < L) return;
if(l <= L && r >= R){
if(idx[id] != v) adj[v].eb(idx[id], 1);
return;
}
int m = (L + R) / 2;
add(id, l, r, v * 2, L, m);
add(id, l, r, v * 2 + 1, m + 1, R);
}
vector<int> dijkstra(int st){
vector<int> dist(n * 4, INF);
dist[st] = 0;
priority_queue<pll, vector<pll>, greater<pll>> q;
q.push({0, st});
while(!q.empty()){
auto [d, pos] = q.top(); q.pop();
if(dist[pos] != d) continue;
for(auto [x, w] : adj[pos]){
if(dist[x] > dist[pos] + w){
dist[x] = dist[pos] + w;
q.push({dist[x], x});
}
}
}
dist[st] = 1;
return dist;
}
signed main(void){
fastio;
cin>>n;
build();
for(int i = 0; i < n; i++){
cin>>l[i]>>r[i];
l[i] --, r[i] --;
add(i, l[i], r[i]);
}
auto dist = dijkstra(idx[0]), d1 = dijkstra(idx[n - 1]);
for(int i = 0; i < n * 4; i++){
dist[i] += d1[i];
}
for(int i = 0; i < n; i++) dist[idx[i]] --;
priority_queue<pll, vector<pll>, greater<pll>> q;
for(int i = 0; i < n; i++) q.push({dist[idx[i]], idx[i]});
while(!q.empty()){
auto [d, pos] = q.top(); q.pop();
if(dist[pos] != d) continue;
for(auto [x, w] : adj[pos]){
if(dist[x] > dist[pos] + w){
dist[x] = dist[pos] + w;
q.push({dist[x], x});
}
}
}
int Q;
cin>>Q;
for(int i = 0; i < Q; i++){
int x;
cin>>x;
x = idx[x - 1];
cout<<(dist[x] <= n ? dist[x] : -1)<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
22876 KB |
Output is correct |
2 |
Correct |
5 ms |
22992 KB |
Output is correct |
3 |
Correct |
5 ms |
23128 KB |
Output is correct |
4 |
Correct |
743 ms |
116988 KB |
Output is correct |
5 |
Correct |
323 ms |
73932 KB |
Output is correct |
6 |
Correct |
153 ms |
63084 KB |
Output is correct |
7 |
Correct |
180 ms |
60740 KB |
Output is correct |
8 |
Correct |
149 ms |
75032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
22876 KB |
Output is correct |
2 |
Correct |
4 ms |
22876 KB |
Output is correct |
3 |
Correct |
4 ms |
22876 KB |
Output is correct |
4 |
Correct |
4 ms |
22876 KB |
Output is correct |
5 |
Correct |
4 ms |
22876 KB |
Output is correct |
6 |
Correct |
5 ms |
22876 KB |
Output is correct |
7 |
Correct |
5 ms |
22876 KB |
Output is correct |
8 |
Correct |
4 ms |
22876 KB |
Output is correct |
9 |
Correct |
4 ms |
22876 KB |
Output is correct |
10 |
Correct |
4 ms |
22976 KB |
Output is correct |
11 |
Correct |
5 ms |
22876 KB |
Output is correct |
12 |
Correct |
6 ms |
23132 KB |
Output is correct |
13 |
Correct |
5 ms |
23132 KB |
Output is correct |
14 |
Correct |
5 ms |
23132 KB |
Output is correct |
15 |
Correct |
5 ms |
22888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
22876 KB |
Output is correct |
2 |
Correct |
4 ms |
22876 KB |
Output is correct |
3 |
Correct |
4 ms |
22876 KB |
Output is correct |
4 |
Correct |
4 ms |
22876 KB |
Output is correct |
5 |
Correct |
4 ms |
22876 KB |
Output is correct |
6 |
Correct |
5 ms |
22876 KB |
Output is correct |
7 |
Correct |
5 ms |
22876 KB |
Output is correct |
8 |
Correct |
4 ms |
22876 KB |
Output is correct |
9 |
Correct |
4 ms |
22876 KB |
Output is correct |
10 |
Correct |
4 ms |
22976 KB |
Output is correct |
11 |
Correct |
5 ms |
22876 KB |
Output is correct |
12 |
Correct |
6 ms |
23132 KB |
Output is correct |
13 |
Correct |
5 ms |
23132 KB |
Output is correct |
14 |
Correct |
5 ms |
23132 KB |
Output is correct |
15 |
Correct |
5 ms |
22888 KB |
Output is correct |
16 |
Correct |
8 ms |
23644 KB |
Output is correct |
17 |
Correct |
9 ms |
23644 KB |
Output is correct |
18 |
Correct |
9 ms |
24152 KB |
Output is correct |
19 |
Correct |
8 ms |
23924 KB |
Output is correct |
20 |
Correct |
6 ms |
23388 KB |
Output is correct |
21 |
Correct |
7 ms |
23644 KB |
Output is correct |
22 |
Correct |
6 ms |
23388 KB |
Output is correct |
23 |
Correct |
8 ms |
23644 KB |
Output is correct |
24 |
Correct |
7 ms |
23820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
22876 KB |
Output is correct |
2 |
Correct |
4 ms |
22876 KB |
Output is correct |
3 |
Correct |
4 ms |
22876 KB |
Output is correct |
4 |
Correct |
4 ms |
22876 KB |
Output is correct |
5 |
Correct |
4 ms |
22876 KB |
Output is correct |
6 |
Correct |
5 ms |
22876 KB |
Output is correct |
7 |
Correct |
5 ms |
22876 KB |
Output is correct |
8 |
Correct |
4 ms |
22876 KB |
Output is correct |
9 |
Correct |
4 ms |
22876 KB |
Output is correct |
10 |
Correct |
4 ms |
22976 KB |
Output is correct |
11 |
Correct |
5 ms |
22876 KB |
Output is correct |
12 |
Correct |
6 ms |
23132 KB |
Output is correct |
13 |
Correct |
5 ms |
23132 KB |
Output is correct |
14 |
Correct |
5 ms |
23132 KB |
Output is correct |
15 |
Correct |
5 ms |
22888 KB |
Output is correct |
16 |
Correct |
8 ms |
23644 KB |
Output is correct |
17 |
Correct |
9 ms |
23644 KB |
Output is correct |
18 |
Correct |
9 ms |
24152 KB |
Output is correct |
19 |
Correct |
8 ms |
23924 KB |
Output is correct |
20 |
Correct |
6 ms |
23388 KB |
Output is correct |
21 |
Correct |
7 ms |
23644 KB |
Output is correct |
22 |
Correct |
6 ms |
23388 KB |
Output is correct |
23 |
Correct |
8 ms |
23644 KB |
Output is correct |
24 |
Correct |
7 ms |
23820 KB |
Output is correct |
25 |
Correct |
5 ms |
22876 KB |
Output is correct |
26 |
Correct |
5 ms |
22876 KB |
Output is correct |
27 |
Correct |
9 ms |
23736 KB |
Output is correct |
28 |
Correct |
8 ms |
23644 KB |
Output is correct |
29 |
Correct |
7 ms |
23388 KB |
Output is correct |
30 |
Correct |
7 ms |
23644 KB |
Output is correct |
31 |
Correct |
7 ms |
23640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
22876 KB |
Output is correct |
2 |
Correct |
5 ms |
22992 KB |
Output is correct |
3 |
Correct |
5 ms |
23128 KB |
Output is correct |
4 |
Correct |
743 ms |
116988 KB |
Output is correct |
5 |
Correct |
323 ms |
73932 KB |
Output is correct |
6 |
Correct |
153 ms |
63084 KB |
Output is correct |
7 |
Correct |
180 ms |
60740 KB |
Output is correct |
8 |
Correct |
149 ms |
75032 KB |
Output is correct |
9 |
Correct |
5 ms |
22876 KB |
Output is correct |
10 |
Correct |
4 ms |
22876 KB |
Output is correct |
11 |
Correct |
4 ms |
22876 KB |
Output is correct |
12 |
Correct |
4 ms |
22876 KB |
Output is correct |
13 |
Correct |
4 ms |
22876 KB |
Output is correct |
14 |
Correct |
5 ms |
22876 KB |
Output is correct |
15 |
Correct |
5 ms |
22876 KB |
Output is correct |
16 |
Correct |
4 ms |
22876 KB |
Output is correct |
17 |
Correct |
4 ms |
22876 KB |
Output is correct |
18 |
Correct |
4 ms |
22976 KB |
Output is correct |
19 |
Correct |
5 ms |
22876 KB |
Output is correct |
20 |
Correct |
6 ms |
23132 KB |
Output is correct |
21 |
Correct |
5 ms |
23132 KB |
Output is correct |
22 |
Correct |
5 ms |
23132 KB |
Output is correct |
23 |
Correct |
5 ms |
22888 KB |
Output is correct |
24 |
Correct |
8 ms |
23644 KB |
Output is correct |
25 |
Correct |
9 ms |
23644 KB |
Output is correct |
26 |
Correct |
9 ms |
24152 KB |
Output is correct |
27 |
Correct |
8 ms |
23924 KB |
Output is correct |
28 |
Correct |
6 ms |
23388 KB |
Output is correct |
29 |
Correct |
7 ms |
23644 KB |
Output is correct |
30 |
Correct |
6 ms |
23388 KB |
Output is correct |
31 |
Correct |
8 ms |
23644 KB |
Output is correct |
32 |
Correct |
7 ms |
23820 KB |
Output is correct |
33 |
Correct |
5 ms |
22876 KB |
Output is correct |
34 |
Correct |
5 ms |
22876 KB |
Output is correct |
35 |
Correct |
9 ms |
23736 KB |
Output is correct |
36 |
Correct |
8 ms |
23644 KB |
Output is correct |
37 |
Correct |
7 ms |
23388 KB |
Output is correct |
38 |
Correct |
7 ms |
23644 KB |
Output is correct |
39 |
Correct |
7 ms |
23640 KB |
Output is correct |
40 |
Correct |
725 ms |
123316 KB |
Output is correct |
41 |
Correct |
308 ms |
76464 KB |
Output is correct |
42 |
Correct |
443 ms |
127924 KB |
Output is correct |
43 |
Correct |
450 ms |
128780 KB |
Output is correct |
44 |
Correct |
186 ms |
63684 KB |
Output is correct |
45 |
Correct |
249 ms |
78120 KB |
Output is correct |
46 |
Correct |
129 ms |
42488 KB |
Output is correct |
47 |
Correct |
432 ms |
84160 KB |
Output is correct |
48 |
Correct |
341 ms |
96888 KB |
Output is correct |