This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll,ll>
#define ld long double
#define el cout << '\n'
#define f1(i,n) for(ll i=1;i<=n;i++)
#define __file_name ""
using namespace std;
const ll maxn = 1e6+5, inf=1e18;
ll n, rpos[maxn], l, r, lf[maxn], rt[maxn], q, x, d[maxn], d1[maxn],dn[maxn];
vector<pair<ll,ll>> G[maxn];
bool vis[maxn];
priority_queue<pll, vector<pll>, greater<pll>> pq;
vector<int> rr;
void addEdge(ll u, ll v, ll w){
// cout << u << " -> " << v << " w = " << w;el;
G[u].push_back({v, w});
}
void build(ll idx, ll l, ll r){
if(l == r){
rpos[l] = idx;
return;
}
// addEdge(idx, idx*2, 0);
// addEdge(idx, idx*2+1, 0);
addEdge(idx*2, idx, 0);
addEdge(idx*2+1, idx, 0);
ll mid = (l+r)/2;
build(idx*2, l, mid);
build(idx*2+1, mid+1, r);
}
void addSeg(ll idx, ll l, ll r, ll u, ll v, ll tar, ll w){
// connects tar -> [u,v] if not rev
if(r < u || v < l) return;
if(u <= l && r <= v){
addEdge(idx, tar, w);
return;
}
ll mid = (l+r)/2;
addSeg(idx*2, l, mid, u, v, tar, w);
addSeg(idx*2+1, mid+1, r, u, v, tar, w);
}
void calc(){
while(!pq.empty()){
pair<ll,ll> item = pq.top();
pq.pop();
ll dist = item.first, u = item.second;
if(dist != d[u]) continue;
vis[u] = true;
for(pair<ll,ll> e: G[u]){
ll c = e.first, w = e.second;
if(d[u] + w < d[c]){
// cout << u << " -> " << c << " changed: " << d[u] + w;el;
d[c] = d[u] + w;
pq.push({d[c], c});
}
}
}
}
void dijkstra(ll s){
d[0] = inf;
f1(i,4*n) d[i] = inf;
d[s] = 0ll;
pq.push({d[s], s});
calc();
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
if(fopen(__file_name ".inp", "r")){
freopen(__file_name ".inp","r",stdin);
freopen(__file_name ".out","w",stdout);
}
// code here
cin >> n;
build(1, 1, n);
f1(i,n){
cin >> l >> r;
lf[i] = l; rt[i] = r;
// if(l != i){
// addSeg(1,1,n,l,i-1,rpos[i],1);
// }
// if(i != r){
// addSeg(1,1,n,i+1,r,rpos[i],1);
// }
addSeg(1,1,n,l,r,rpos[i],1);
}
dijkstra(rpos[1]);
f1(i,4*n) {
d1[i] = d[i];
}
dijkstra(rpos[n]);
f1(i,4*n) {
dn[i] = d[i];
}
f1(i,n) rr.push_back(rpos[i]);
sort(rr.begin(), rr.end());
f1(i,4*n) d[i] = inf;
f1(i,4*n) {
// cout << i << " = " << d1[i] << ' ' << dn[i];el;
d[i] = d1[i] + dn[i] - binary_search(rr.begin(), rr.end(), i) + (i == rpos[1] || i == rpos[n]);
pq.push({d[i], i});
}
// f1(i, 4*n) pq.push({min(d[i], d1[i] + dn[i]), i});
calc();
cin >> q;
while(q--){
cin >> x;
// cout << x << " = " << rpos[x];el;
cout << (d[rpos[x]] < 1e9 ? d[rpos[x]] : -1);el;
}
return 0;
}
Compilation message (stderr)
passport.cpp: In function 'int main()':
passport.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen(__file_name ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | freopen(__file_name ".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |