#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
using namespace std;
const int N = 2e5 + 5, NODE = 8e5 + 5, INF = 2e9;
int n, q, l[N], r[N], ans[N], idn[N];
vector<array<int, 2>> adj[N];
void add_edge(int x, int y, int w){
adj[x].push_back({y, w});
}
void build(int id = 1, int l = 1, int r = n){
if(l == r){
idn[l] = id; return;
}
int mid = (l + r) / 2;
build(id * 2, l, mid), build(id * 2 + 1, mid + 1, r);
adj[id * 2].push_back({id, 0}), adj[id * 2 + 1].push_back({id, 0});
}
void add(int id, int tl, int tr, int l, int r, int p){
if(r < tl || tr < l) return;
if(l <= tl && tr <= r) {
adj[id].push_back({idn[p], 1});
return;
}
int mid = (tl + tr) / 2;
add(id * 2, tl, mid, l, r, p), add(id * 2 + 1, mid + 1, tr, l, r, p);
}
void dijkstra(vector<int> &d){
priority_queue<array<int, 2>> pq;
for(int i = 1; i <= 4 * n; i++){
if(d[i] != INF) pq.push({-d[i], i});
}
while(!pq.empty()){
auto [c, i] = pq.top(); pq.pop();
c = -c;
if(c != d[i]) continue;
for(auto [x, w] : adj[i]){
if(d[x] > d[i] + w){
d[x] = d[i] + w;
pq.push({-d[x], x});
}
}
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
build(1, 1, n);
for(int i = 1; i <= n; i++){
cin >> l[i] >> r[i];
add(1, 1, n, l[i], r[i], i);
}
vector<int> d1(4 * n + 1, INF); d1[idn[1]] = 0; dijkstra(d1);
vector<int> d2(4 * n + 1, INF); d2[idn[n]] = 0; dijkstra(d2);
vector<int> d3(4 * n + 1, INF);
for(int i = 1; i <= n; i++){
int x = idn[i];
d3[x] = min(d3[x], d1[x] + d2[x]);
}
dijkstra(d3);
cin >> q;
while(q--){
int x; cin >> x;
int ans = d3[idn[x]];
if(ans > INF) cout << -1;
else cout << ans;
cout << '\n';
}
return 0;
}
Compilation message
passport.cpp: In function 'void dijkstra(std::vector<long long int>&)':
passport.cpp:41:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
41 | auto [c, i] = pq.top(); pq.pop();
| ^
passport.cpp:44:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
44 | for(auto [x, w] : adj[i]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Incorrect |
2 ms |
4960 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Incorrect |
2 ms |
4960 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Incorrect |
2 ms |
4960 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |