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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
#define g4(a) get<4>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
using iiiii = tuple<int, int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
int N, Q, _idx, L[400005], R[400005], distL[400005], distR[400005], distC[400005], mpp[400005];
vector<ii> adj[400005];
priority_queue<ii, vector<ii>, greater<ii> > pq;
void dijk(int dist[]) {
for (int i = 1; i <= _idx; i++) {
pq.emplace(dist[i], i);
}
while (!pq.empty()) {
auto [d, v] = pq.top();
pq.pop();
if (d != dist[v]) continue;
//~ cout << "@ " << v << ' ' << dist[v] << '\n';
for (auto [u, w] : adj[v]) {
if (d + w < dist[u]) {
dist[u] = d + w;
pq.emplace(dist[u], u);
}
}
}
}
struct node {
node *left, *right;
int S, E, idx;
node(int _s, int _e) : S(_s), E(_e), idx(++_idx) {
if (S == E) {
mpp[S] = idx;
return;
}
int M = (S + E) >> 1;
left = new node(S, M);
right = new node(M + 1, E);
adj[left->idx].eb(idx, 0);
adj[right->idx].eb(idx, 0);
}
void add(int l, int r, int v) {
if (l > E || r < S) return;
if (l <= S && E <= r) {
//~ cout << "ADD " << idx << ' ' << v << '\n';
adj[idx].eb(v, 1);
return;
}
left->add(l, r, v);
right->add(l, r, v);
}
} *root;
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
root = new node(1, N);
for (int i = 1; i <= N; i++) {
cin >> L[i] >> R[i];
root->add(L[i], R[i], mpp[i]);
}
for (int i = 1; i <= _idx; i++) {
distL[i] = distR[i] = distC[i] = (int)1e9;
}
for (int i = 1; i <= N; i++) {
if (L[i] == 1) distL[mpp[i]] = 0;
if (R[i] == N) distR[mpp[i]] = 0;
}
dijk(distL);
dijk(distR);
for (int i = 1; i <= N; i++) {
//~ cout << distL[mpp[i]] << ' ' << distR[mpp[i]] << '\n';
distC[mpp[i]] = distL[mpp[i]] + distR[mpp[i]];
}
dijk(distC);
cin >> Q;
for (int x; Q--; ) {
cin >> x;
if (distC[mpp[x]] >= (int)1e9) cout << "-1\n";
else cout << distC[mpp[x]] + 1 << '\n';
}
}
Compilation message (stderr)
passport.cpp:76:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
76 | main() {
| ^~~~
# | 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... |