#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REV(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}
const int N = 2e5+5;
struct Edge{
int u,v,w;
Edge(int u = 0, int v = 0, int w = 0): u(u), v(v), w(w) {}
};
int n, dist[3][N + (N << 2)];
vector<int> adj[(N << 2) + N];
vector<Edge> E;
void addEdge(int u, int v, int c){
adj[u].push_back(sz(E));
E.push_back(Edge(u, v, c));
}
void update(int id, int l, int r, int u, int v, int x){
if(l >= u && r <= v){
addEdge(id + n, x, 1);
return;
}
int m = l+r>>1;
if(u <= m) update(id << 1, l, m, u, v, x);
if(v > m) update(id << 1|1, m+1, r, u, v, x);
}
void build(int id, int l, int r){
if(l == r){
addEdge(l, id + n, 0);
return;
}
int m = l+r>>1;
build(id << 1, l, m);
build(id << 1|1, m+1, r);
addEdge(id * 2 + n, id + n, 0);
addEdge(id * 2 + n + 1, id + n, 0);
}
void bfs01(int id, int source){
FOR(i, 1, n + (n << 2)){
dist[id][i] = 1e9;
}
dist[id][source] = 0;
deque<int> q; q.push_front(source);
while(!q.empty()){
int x = q.front(); q.pop_front();
for(int ide : adj[x]){
int v = E[ide].u ^ E[ide].v ^ x;
if(dist[id][v] > dist[id][x] + E[ide].w){
dist[id][v] = dist[id][x] + E[ide].w;
if(!E[ide].w){
q.push_front(v);
}
else q.push_back(v);
}
}
}
}
void solve()
{
cin >> n;
FOR(i, 1, n){
int l,r; cin >> l >> r;
update(1, 1, n, l, r, i);
}
build(1, 1, n);
bfs01(0, 1), bfs01(1, n);
priority_queue<pair<int,int>> pq;
FOR(i, 1, n + (n << 2)){
dist[2][i] = 1e9;
}
FOR(i, 1, n){
if(max(dist[0][i], dist[1][i]) < 1e9){
dist[2][i] = dist[0][i] + dist[1][i] - (i != 1 && i != n); // L[i] <= i <= R[i] so it's easy here
pq.push({-dist[2][i], i});
}
}
while(!pq.empty()){
pair<int,int> eq = pq.top(); pq.pop();
int x = eq.se;
if(-eq.fi > dist[2][x]) continue;
for(int ide : adj[x]){
int v = E[ide].u ^ E[ide].v ^ x;
if(ckmn(dist[2][v], dist[2][x] + E[ide].w)){
pq.push({-dist[2][v], v});
}
}
}
int q; cin >> q;
while(q--){
int u; cin >> u;
cout << (dist[2][u] == 1e9 ? -1 : dist[2][u]) << "\n";
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP","r",stdin);
freopen(name".OUT","w",stdout);
}
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
Compilation message (stderr)
passport.cpp: In function 'int main()':
passport.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
141 | freopen(name".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
passport.cpp:142:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
142 | freopen(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... |