// 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
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 |
1 |
Correct |
35 ms |
56600 KB |
Output is correct |
2 |
Correct |
30 ms |
56684 KB |
Output is correct |
3 |
Correct |
34 ms |
56612 KB |
Output is correct |
4 |
Correct |
1085 ms |
192264 KB |
Output is correct |
5 |
Correct |
517 ms |
151972 KB |
Output is correct |
6 |
Correct |
338 ms |
143704 KB |
Output is correct |
7 |
Correct |
307 ms |
140932 KB |
Output is correct |
8 |
Correct |
261 ms |
142968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
56684 KB |
Output is correct |
2 |
Correct |
33 ms |
56660 KB |
Output is correct |
3 |
Correct |
38 ms |
56672 KB |
Output is correct |
4 |
Correct |
31 ms |
56676 KB |
Output is correct |
5 |
Correct |
31 ms |
56676 KB |
Output is correct |
6 |
Correct |
32 ms |
56588 KB |
Output is correct |
7 |
Correct |
29 ms |
56708 KB |
Output is correct |
8 |
Correct |
30 ms |
56700 KB |
Output is correct |
9 |
Correct |
31 ms |
56660 KB |
Output is correct |
10 |
Correct |
31 ms |
56652 KB |
Output is correct |
11 |
Correct |
37 ms |
56744 KB |
Output is correct |
12 |
Correct |
31 ms |
56744 KB |
Output is correct |
13 |
Correct |
34 ms |
56860 KB |
Output is correct |
14 |
Correct |
32 ms |
56752 KB |
Output is correct |
15 |
Correct |
29 ms |
56720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
56684 KB |
Output is correct |
2 |
Correct |
33 ms |
56660 KB |
Output is correct |
3 |
Correct |
38 ms |
56672 KB |
Output is correct |
4 |
Correct |
31 ms |
56676 KB |
Output is correct |
5 |
Correct |
31 ms |
56676 KB |
Output is correct |
6 |
Correct |
32 ms |
56588 KB |
Output is correct |
7 |
Correct |
29 ms |
56708 KB |
Output is correct |
8 |
Correct |
30 ms |
56700 KB |
Output is correct |
9 |
Correct |
31 ms |
56660 KB |
Output is correct |
10 |
Correct |
31 ms |
56652 KB |
Output is correct |
11 |
Correct |
37 ms |
56744 KB |
Output is correct |
12 |
Correct |
31 ms |
56744 KB |
Output is correct |
13 |
Correct |
34 ms |
56860 KB |
Output is correct |
14 |
Correct |
32 ms |
56752 KB |
Output is correct |
15 |
Correct |
29 ms |
56720 KB |
Output is correct |
16 |
Correct |
35 ms |
57940 KB |
Output is correct |
17 |
Correct |
36 ms |
57804 KB |
Output is correct |
18 |
Correct |
42 ms |
58244 KB |
Output is correct |
19 |
Correct |
34 ms |
58188 KB |
Output is correct |
20 |
Correct |
32 ms |
57684 KB |
Output is correct |
21 |
Correct |
93 ms |
57872 KB |
Output is correct |
22 |
Correct |
32 ms |
57684 KB |
Output is correct |
23 |
Correct |
36 ms |
57904 KB |
Output is correct |
24 |
Correct |
40 ms |
57992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
56684 KB |
Output is correct |
2 |
Correct |
33 ms |
56660 KB |
Output is correct |
3 |
Correct |
38 ms |
56672 KB |
Output is correct |
4 |
Correct |
31 ms |
56676 KB |
Output is correct |
5 |
Correct |
31 ms |
56676 KB |
Output is correct |
6 |
Correct |
32 ms |
56588 KB |
Output is correct |
7 |
Correct |
29 ms |
56708 KB |
Output is correct |
8 |
Correct |
30 ms |
56700 KB |
Output is correct |
9 |
Correct |
31 ms |
56660 KB |
Output is correct |
10 |
Correct |
31 ms |
56652 KB |
Output is correct |
11 |
Correct |
37 ms |
56744 KB |
Output is correct |
12 |
Correct |
31 ms |
56744 KB |
Output is correct |
13 |
Correct |
34 ms |
56860 KB |
Output is correct |
14 |
Correct |
32 ms |
56752 KB |
Output is correct |
15 |
Correct |
29 ms |
56720 KB |
Output is correct |
16 |
Correct |
35 ms |
57940 KB |
Output is correct |
17 |
Correct |
36 ms |
57804 KB |
Output is correct |
18 |
Correct |
42 ms |
58244 KB |
Output is correct |
19 |
Correct |
34 ms |
58188 KB |
Output is correct |
20 |
Correct |
32 ms |
57684 KB |
Output is correct |
21 |
Correct |
93 ms |
57872 KB |
Output is correct |
22 |
Correct |
32 ms |
57684 KB |
Output is correct |
23 |
Correct |
36 ms |
57904 KB |
Output is correct |
24 |
Correct |
40 ms |
57992 KB |
Output is correct |
25 |
Correct |
30 ms |
56700 KB |
Output is correct |
26 |
Correct |
28 ms |
56660 KB |
Output is correct |
27 |
Correct |
32 ms |
57940 KB |
Output is correct |
28 |
Correct |
33 ms |
57876 KB |
Output is correct |
29 |
Correct |
36 ms |
57792 KB |
Output is correct |
30 |
Correct |
93 ms |
57868 KB |
Output is correct |
31 |
Correct |
32 ms |
57804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
56600 KB |
Output is correct |
2 |
Correct |
30 ms |
56684 KB |
Output is correct |
3 |
Correct |
34 ms |
56612 KB |
Output is correct |
4 |
Correct |
1085 ms |
192264 KB |
Output is correct |
5 |
Correct |
517 ms |
151972 KB |
Output is correct |
6 |
Correct |
338 ms |
143704 KB |
Output is correct |
7 |
Correct |
307 ms |
140932 KB |
Output is correct |
8 |
Correct |
261 ms |
142968 KB |
Output is correct |
9 |
Correct |
32 ms |
56684 KB |
Output is correct |
10 |
Correct |
33 ms |
56660 KB |
Output is correct |
11 |
Correct |
38 ms |
56672 KB |
Output is correct |
12 |
Correct |
31 ms |
56676 KB |
Output is correct |
13 |
Correct |
31 ms |
56676 KB |
Output is correct |
14 |
Correct |
32 ms |
56588 KB |
Output is correct |
15 |
Correct |
29 ms |
56708 KB |
Output is correct |
16 |
Correct |
30 ms |
56700 KB |
Output is correct |
17 |
Correct |
31 ms |
56660 KB |
Output is correct |
18 |
Correct |
31 ms |
56652 KB |
Output is correct |
19 |
Correct |
37 ms |
56744 KB |
Output is correct |
20 |
Correct |
31 ms |
56744 KB |
Output is correct |
21 |
Correct |
34 ms |
56860 KB |
Output is correct |
22 |
Correct |
32 ms |
56752 KB |
Output is correct |
23 |
Correct |
29 ms |
56720 KB |
Output is correct |
24 |
Correct |
35 ms |
57940 KB |
Output is correct |
25 |
Correct |
36 ms |
57804 KB |
Output is correct |
26 |
Correct |
42 ms |
58244 KB |
Output is correct |
27 |
Correct |
34 ms |
58188 KB |
Output is correct |
28 |
Correct |
32 ms |
57684 KB |
Output is correct |
29 |
Correct |
93 ms |
57872 KB |
Output is correct |
30 |
Correct |
32 ms |
57684 KB |
Output is correct |
31 |
Correct |
36 ms |
57904 KB |
Output is correct |
32 |
Correct |
40 ms |
57992 KB |
Output is correct |
33 |
Correct |
30 ms |
56700 KB |
Output is correct |
34 |
Correct |
28 ms |
56660 KB |
Output is correct |
35 |
Correct |
32 ms |
57940 KB |
Output is correct |
36 |
Correct |
33 ms |
57876 KB |
Output is correct |
37 |
Correct |
36 ms |
57792 KB |
Output is correct |
38 |
Correct |
93 ms |
57868 KB |
Output is correct |
39 |
Correct |
32 ms |
57804 KB |
Output is correct |
40 |
Correct |
1194 ms |
192972 KB |
Output is correct |
41 |
Correct |
533 ms |
154028 KB |
Output is correct |
42 |
Execution timed out |
2083 ms |
206800 KB |
Time limit exceeded |
43 |
Halted |
0 ms |
0 KB |
- |