#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(abs(a), abs(b));}
ll lcm(ll a, ll b){return abs(a) / gcd(a, b) * abs(b);}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ull mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int N = 2e5 + 69, INF = 1e9 + 69;
struct P{
int i, w;
P(int i, int w): i(i), w(w){
}
};
struct compare{
bool operator () (P a, P b){
return a.w > b.w;
}
};
int n, q;
template<class T1, class T2>
ostream& operator << (ostream &out, pair<T1, T2> x){
return out << "(" << x.first << "," << x.second << ")";
}
vector<P> graph[N * 3];
void dijkstra(int dih[]){
priority_queue<P, vector<P>, compare> pq;
for(int j = 1; j <= n * 3; ++j) pq.push(P(j, dih[j]));
while(pq.size()){
P u = pq.top(); pq.pop();
if (u.w != dih[u.i]) continue;
for(P v: graph[u.i])
if (minimize(dih[v.i], dih[u.i] + v.w))
pq.push(P(v.i, dih[v.i]));
}
}
void add_edge(int u, int v, int w){
graph[u].push_back(P(v, w));
}
void solve(){
cin >> n;
for(int i = 2; i <= n * 2; ++i) {
add_edge(i, i/2, 0);
}
for(int i = 1; i <= n; ++i) add_edge(n*2+i, n+i, 1);
for(int i = 1; i <= n; ++i){
int l, r; cin >> l >> r;
l += n; r += n + 1;
while(l < r){
if (l & 1) add_edge(l++, i+n*2, 0);
if (r & 1) add_edge(--r, i+n*2, 0);
l >>= 1; r >>= 1;
}
}
int dis1[n*3+2], dis2[n*3+2];
memset(dis1, 63, sizeof dis1);
memset(dis2, 63, sizeof dis2);
dis1[n+1] = 0;
dis2[n*2] = 0;
dijkstra(dis1);
dijkstra(dis2);
int dis3[n*3+2];
for(int i = 1; i <= n*3; ++i) dis3[i] = dis1[i] + dis2[i];
priority_queue<P, vector<P>, compare> pq;
for(int i = 1; i <= n*3; ++i) pq.push(P(i, dis3[i]));
while(pq.size()){
P u = pq.top(); pq.pop();
if (u.w != dis3[u.i]) continue;
for(P v: graph[u.i]){
if (minimize(dis3[v.i], dis3[u.i] + v.w)){
pq.push(P(v.i, dis3[v.i]));
}
}
}
int ans[n+1]; memset(ans, -1, sizeof ans);
for(int i = 1; i <= n; ++i){
int u = i + n*2;
if (dis1[u] >= INF || dis2[u] >= INF) continue;
ans[i] = dis3[u] + 1;
}
cin >> q;
while(q--){
int x; cin >> x;
cout << ans[x] << "\n";
}
}
int main(void){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
clock_t start = clock();
// freopen("input.inp", "r", stdin);
solve();
cerr << "Time elapsed: " << clock() - start << "ms!\n";
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... |