This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 200005;
const int MAXV = MAXN * 6;
#define OFFSET n * 4
int n;
vii adj[MAXV], radj[MAXV];
int q;
void addEdge(int u, int v, int p) {
adj[u].pb({v, p});
radj[v].pb({u, p});
}
#define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
void init(int u = 1, int lo = 0, int hi = n - 1) {
assert(u < OFFSET);
if (lo == hi) {
addEdge(u, OFFSET + n + lo, 0);
return;
}
MLR;
addEdge(u, lc, 0);
addEdge(u, rc, 0);
init(lc, lo, mid);
init(rc, mid + 1, hi);
}
void upd(int s, int e, int c, int u = 1, int lo = 0, int hi = n - 1) {
if (lo >= s && hi <= e) {
addEdge(OFFSET + c, u, 0);
return;
}
MLR;
if (s <= mid) {
upd(s, e, c, lc, lo, mid);
}
if (e > mid) {
upd(s, e, c, rc, mid + 1, hi);
}
}
ll d1[MAXV], dn[MAXV], ans[MAXV];
void dijkstra(int src, ll *d, vii *adj) {
deque<pll> pq;
if (src != -1) {
REP (i, 0, OFFSET + n + n) {
d[i] = LINF;
}
d[src] = 0;
pq.pb({0, src});
} else {
REP (i, OFFSET, OFFSET + n + n) {
pq.pb({d[i], i});
}
}
while (!pq.empty()) {
auto [ud, u] = pq.back(); pq.pop_back();
if (ud != d[u]) {
continue;
}
for (auto [v, w] : adj[u]) {
if (mnto(d[v], d[u] + w)) {
if (w == 0) {
pq.pb({d[v], v});
} else {
pq.push_front({d[v], v});
}
}
}
}
}
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> n;
init();
REP (i, 0, n) {
int a, b; cin >> a >> b;
a--; b--;
addEdge(OFFSET + n + i, OFFSET + i, 1);
upd(a, b, i);
}
dijkstra(OFFSET + n, d1, radj);
dijkstra(OFFSET + n + n - 1, dn, radj);
/*
REP (i, 0, OFFSET + 1) {
ans[i] = LINF;
}
*/
REP (i, 0, OFFSET + n + n) {
ans[i] = d1[i] + dn[i];
}
dijkstra(-1, ans, radj);
cin >> q;
while (q--) {
int x; cin >> x;
x--;
if (ans[OFFSET + n + x] >= LINF) {
ans[OFFSET + n + x] = -1;
}
cout << ans[OFFSET + n + x] << '\n';
}
/*
REP (i, 0, n) {
if (ans[OFFSET + n + i] >= LINF) {
ans[OFFSET + n + i] = -1;
}
cout << ans[OFFSET + n + i] << '\n';
}
*/
return 0;
}
Compilation message (stderr)
passport.cpp: In function 'void init(int, int, int)':
passport.cpp:49:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
49 | #define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
| ~~~^~~~
passport.cpp:56:5: note: in expansion of macro 'MLR'
56 | MLR;
| ^~~
passport.cpp: In function 'void upd(int, int, int, int, int, int)':
passport.cpp:49:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
49 | #define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
| ~~~^~~~
passport.cpp:67:5: note: in expansion of macro 'MLR'
67 | MLR;
| ^~~
# | 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... |