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>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define isz(x) ((int)(x).size())
#define sumof(x) accumulate(all(x), 0ll)
const int N=2e5+1;
int n, pos[N], dist[3][N*4];
vector<pair<int, int>> g[N*4];
queue<int> q[N*4];
void build(int k, int l, int r){
if (l==r){
pos[l]=k;
return;
}
int mid=(l+r)>>1;
build(k<<1, l, mid);
build(k<<1|1, mid+1, r);
g[k<<1].emplace_back(k, 0);
g[k<<1|1].emplace_back(k, 0);
}
void update(int k, int l, int r, int L, int R, int u){
if (r<L || R<l) return;
if (L<=l && r<=R){
g[k].emplace_back(pos[u], 1);
return;
}
int mid=(l+r)>>1;
update(k<<1, l, mid, L, R, u);
update(k<<1|1, mid+1, r, L, R, u);
}
void bfs(int t){
for (int i=1; i<=n*4; ++i) if (dist[t][i]<1e9) q[dist[t][i]].push(i);
for (int i=0; i<=n*4; ++i){
while (q[i].size()){
int u=q[i].front(); q[i].pop();
if (dist[t][u]!=i) continue;
for (auto &e:g[u]){
int v=e.first, w=e.second;
if (dist[t][v]>dist[t][u]+w) q[dist[t][v]=dist[t][u]+w].push(v);
}
}
}
}
void solve(){
cin >> n;
build(1, 1, n);
for (int i=1; i<=n; ++i){
int l, r; cin >> l >> r;
update(1, 1, n, l, r, i);
}
memset(dist, 0x3f, sizeof dist);
dist[0][pos[1]]=0;
dist[1][pos[n]]=0;
bfs(0);
bfs(1);
for (int i=1; i<=n*4; ++i) dist[2][i]=dist[0][i]+dist[1][i];
bfs(2);
int tc; cin >> tc;
while (tc--){
int x; cin >> x; x=pos[x];
cout << (dist[2][x]<1e9?dist[2][x]:-1) << '\n';
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int ntests=1;
// cin >> ntests;
for (int i=1; i<=ntests; ++i) solve();
return 0;
}
# | 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... |