// 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) {
priority_queue<pll, vector<pll>, greater<pll>> pq;
if (src != -1) {
REP (i, 0, OFFSET + n + n) {
d[i] = LINF;
}
d[src] = 0;
pq.push({0, src});
} else {
REP (i, OFFSET, OFFSET + n + n) {
pq.push({d[i], i});
}
}
queue<pll> qu;
while (!qu.empty() || !pq.empty()) {
ll ud, u;
if (!qu.empty()) {
tie(ud, u) = qu.front(); qu.pop();
} else {
tie(ud, u) = pq.top(); pq.pop();
}
if (ud != d[u]) {
continue;
}
for (auto [v, w] : adj[u]) {
if (mnto(d[v], d[u] + w)) {
if (w == 0) {
qu.push({d[v], v});
} else {
pq.push({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
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;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
56652 KB |
Output is correct |
2 |
Correct |
32 ms |
56592 KB |
Output is correct |
3 |
Correct |
30 ms |
56660 KB |
Output is correct |
4 |
Correct |
1132 ms |
199364 KB |
Output is correct |
5 |
Correct |
600 ms |
155188 KB |
Output is correct |
6 |
Correct |
386 ms |
146360 KB |
Output is correct |
7 |
Correct |
369 ms |
150976 KB |
Output is correct |
8 |
Correct |
290 ms |
151064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
56692 KB |
Output is correct |
2 |
Correct |
31 ms |
56660 KB |
Output is correct |
3 |
Correct |
28 ms |
56676 KB |
Output is correct |
4 |
Correct |
29 ms |
56616 KB |
Output is correct |
5 |
Correct |
29 ms |
56652 KB |
Output is correct |
6 |
Correct |
29 ms |
56620 KB |
Output is correct |
7 |
Correct |
30 ms |
56684 KB |
Output is correct |
8 |
Correct |
27 ms |
56588 KB |
Output is correct |
9 |
Correct |
28 ms |
56696 KB |
Output is correct |
10 |
Correct |
29 ms |
56652 KB |
Output is correct |
11 |
Correct |
29 ms |
56836 KB |
Output is correct |
12 |
Correct |
30 ms |
56780 KB |
Output is correct |
13 |
Correct |
29 ms |
56780 KB |
Output is correct |
14 |
Correct |
36 ms |
56864 KB |
Output is correct |
15 |
Correct |
28 ms |
56788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
56692 KB |
Output is correct |
2 |
Correct |
31 ms |
56660 KB |
Output is correct |
3 |
Correct |
28 ms |
56676 KB |
Output is correct |
4 |
Correct |
29 ms |
56616 KB |
Output is correct |
5 |
Correct |
29 ms |
56652 KB |
Output is correct |
6 |
Correct |
29 ms |
56620 KB |
Output is correct |
7 |
Correct |
30 ms |
56684 KB |
Output is correct |
8 |
Correct |
27 ms |
56588 KB |
Output is correct |
9 |
Correct |
28 ms |
56696 KB |
Output is correct |
10 |
Correct |
29 ms |
56652 KB |
Output is correct |
11 |
Correct |
29 ms |
56836 KB |
Output is correct |
12 |
Correct |
30 ms |
56780 KB |
Output is correct |
13 |
Correct |
29 ms |
56780 KB |
Output is correct |
14 |
Correct |
36 ms |
56864 KB |
Output is correct |
15 |
Correct |
28 ms |
56788 KB |
Output is correct |
16 |
Correct |
36 ms |
58052 KB |
Output is correct |
17 |
Correct |
34 ms |
57960 KB |
Output is correct |
18 |
Correct |
34 ms |
58296 KB |
Output is correct |
19 |
Correct |
35 ms |
58364 KB |
Output is correct |
20 |
Correct |
33 ms |
57936 KB |
Output is correct |
21 |
Correct |
33 ms |
57960 KB |
Output is correct |
22 |
Correct |
31 ms |
57932 KB |
Output is correct |
23 |
Correct |
33 ms |
57968 KB |
Output is correct |
24 |
Correct |
31 ms |
58116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
56692 KB |
Output is correct |
2 |
Correct |
31 ms |
56660 KB |
Output is correct |
3 |
Correct |
28 ms |
56676 KB |
Output is correct |
4 |
Correct |
29 ms |
56616 KB |
Output is correct |
5 |
Correct |
29 ms |
56652 KB |
Output is correct |
6 |
Correct |
29 ms |
56620 KB |
Output is correct |
7 |
Correct |
30 ms |
56684 KB |
Output is correct |
8 |
Correct |
27 ms |
56588 KB |
Output is correct |
9 |
Correct |
28 ms |
56696 KB |
Output is correct |
10 |
Correct |
29 ms |
56652 KB |
Output is correct |
11 |
Correct |
29 ms |
56836 KB |
Output is correct |
12 |
Correct |
30 ms |
56780 KB |
Output is correct |
13 |
Correct |
29 ms |
56780 KB |
Output is correct |
14 |
Correct |
36 ms |
56864 KB |
Output is correct |
15 |
Correct |
28 ms |
56788 KB |
Output is correct |
16 |
Correct |
36 ms |
58052 KB |
Output is correct |
17 |
Correct |
34 ms |
57960 KB |
Output is correct |
18 |
Correct |
34 ms |
58296 KB |
Output is correct |
19 |
Correct |
35 ms |
58364 KB |
Output is correct |
20 |
Correct |
33 ms |
57936 KB |
Output is correct |
21 |
Correct |
33 ms |
57960 KB |
Output is correct |
22 |
Correct |
31 ms |
57932 KB |
Output is correct |
23 |
Correct |
33 ms |
57968 KB |
Output is correct |
24 |
Correct |
31 ms |
58116 KB |
Output is correct |
25 |
Correct |
28 ms |
56660 KB |
Output is correct |
26 |
Correct |
33 ms |
56588 KB |
Output is correct |
27 |
Correct |
33 ms |
58188 KB |
Output is correct |
28 |
Correct |
37 ms |
57936 KB |
Output is correct |
29 |
Correct |
38 ms |
57872 KB |
Output is correct |
30 |
Correct |
34 ms |
57972 KB |
Output is correct |
31 |
Correct |
34 ms |
57904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
56652 KB |
Output is correct |
2 |
Correct |
32 ms |
56592 KB |
Output is correct |
3 |
Correct |
30 ms |
56660 KB |
Output is correct |
4 |
Correct |
1132 ms |
199364 KB |
Output is correct |
5 |
Correct |
600 ms |
155188 KB |
Output is correct |
6 |
Correct |
386 ms |
146360 KB |
Output is correct |
7 |
Correct |
369 ms |
150976 KB |
Output is correct |
8 |
Correct |
290 ms |
151064 KB |
Output is correct |
9 |
Correct |
31 ms |
56692 KB |
Output is correct |
10 |
Correct |
31 ms |
56660 KB |
Output is correct |
11 |
Correct |
28 ms |
56676 KB |
Output is correct |
12 |
Correct |
29 ms |
56616 KB |
Output is correct |
13 |
Correct |
29 ms |
56652 KB |
Output is correct |
14 |
Correct |
29 ms |
56620 KB |
Output is correct |
15 |
Correct |
30 ms |
56684 KB |
Output is correct |
16 |
Correct |
27 ms |
56588 KB |
Output is correct |
17 |
Correct |
28 ms |
56696 KB |
Output is correct |
18 |
Correct |
29 ms |
56652 KB |
Output is correct |
19 |
Correct |
29 ms |
56836 KB |
Output is correct |
20 |
Correct |
30 ms |
56780 KB |
Output is correct |
21 |
Correct |
29 ms |
56780 KB |
Output is correct |
22 |
Correct |
36 ms |
56864 KB |
Output is correct |
23 |
Correct |
28 ms |
56788 KB |
Output is correct |
24 |
Correct |
36 ms |
58052 KB |
Output is correct |
25 |
Correct |
34 ms |
57960 KB |
Output is correct |
26 |
Correct |
34 ms |
58296 KB |
Output is correct |
27 |
Correct |
35 ms |
58364 KB |
Output is correct |
28 |
Correct |
33 ms |
57936 KB |
Output is correct |
29 |
Correct |
33 ms |
57960 KB |
Output is correct |
30 |
Correct |
31 ms |
57932 KB |
Output is correct |
31 |
Correct |
33 ms |
57968 KB |
Output is correct |
32 |
Correct |
31 ms |
58116 KB |
Output is correct |
33 |
Correct |
28 ms |
56660 KB |
Output is correct |
34 |
Correct |
33 ms |
56588 KB |
Output is correct |
35 |
Correct |
33 ms |
58188 KB |
Output is correct |
36 |
Correct |
37 ms |
57936 KB |
Output is correct |
37 |
Correct |
38 ms |
57872 KB |
Output is correct |
38 |
Correct |
34 ms |
57972 KB |
Output is correct |
39 |
Correct |
34 ms |
57904 KB |
Output is correct |
40 |
Correct |
1206 ms |
202536 KB |
Output is correct |
41 |
Correct |
622 ms |
155916 KB |
Output is correct |
42 |
Correct |
796 ms |
209748 KB |
Output is correct |
43 |
Correct |
843 ms |
209892 KB |
Output is correct |
44 |
Correct |
430 ms |
146616 KB |
Output is correct |
45 |
Correct |
556 ms |
160620 KB |
Output is correct |
46 |
Correct |
289 ms |
95892 KB |
Output is correct |
47 |
Correct |
814 ms |
166988 KB |
Output is correct |
48 |
Correct |
617 ms |
185448 KB |
Output is correct |