#include "walk.h"
#include <stdlib.h>
#include <string.h>
using namespace std;
typedef vector<int> vi;
const int N = 100000, M = 100000, N_ = 4000000, M_ = 4000000;
const long long INF = 0x3f3f3f3f3f3f3f3f;
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int xx[N], yy[N], ii[N], ll[M], rr[M], zz[M], hh[M];
int *aa;
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (aa[ii[j]] == aa[i_])
j++;
else if (aa[ii[j]] < aa[i_]) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
int ij[M_], ww[M_], m_;
int *eh[N_], eo[N_], n_;
void append(int i, int h) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
eh[i][o] = h;
}
int newnode() {
eh[n_] = (int *) malloc(2 * sizeof *eh[n_]);
return n_++;
}
void add(int i, int j, int w) {
int h = m_++;
ij[h] = i ^ j, ww[h] = w;
append(i, h), append(j, h);
}
int pp[N], qq[N], top[N], yy1[N];
void push(int i, int y) {
int t = newnode();
add(top[i], t, y - yy1[i]);
top[i] = t, yy1[i] = y;
}
long long dd[N_]; int pq[N_], iq[1 + N_], cnt;
int lt(int i, int j) {
return dd[i] < dd[j];
}
int p2(int p) {
return (p *= 2) > cnt ? 0 : (p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p);
}
void pq_up(int i) {
int p, q, j;
for (p = pq[i]; (q = p / 2) && lt(i, j = iq[q]); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_dn(int i) {
int p, q, j;
for (p = pq[i]; (q = p2(p)) && lt(j = iq[q], i); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_add_last(int i) {
iq[pq[i] = ++cnt] = i;
}
int pq_remove_first() {
int i = iq[1], j = iq[cnt--];
if (j != i)
pq[j] = 1, pq_dn(j);
pq[i] = 0;
return i;
}
long long dijkstra(int s, int t) {
memset(dd, 0x3f, n_ * sizeof *dd);
dd[s] = 0, pq_add_last(s);
while (cnt) {
int i = pq_remove_first(), o;
if (i == t)
return dd[i];
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = i ^ ij[h];
long long d = dd[i] + ww[h];
if (dd[j] > d) {
if (dd[j] == INF)
pq_add_last(j);
dd[j] = d, pq_up(j);
}
}
}
return -1;
}
long long min_distance(vi xx_, vi yy_, vi ll_, vi rr_, vi zz_, int s, int t) {
int n = xx_.size(), m = zz_.size(), h, i;
for (i = 0; i < n; i++) {
xx[i] = xx_[i], yy[i] = yy_[i];
ii[i] = i;
}
aa = yy, sort(ii, 0, n);
for (h = 0; h < m; h++) {
ll[h] = ll_[h], rr[h] = rr_[h], zz[h] = zz_[h];
hh[h] = h;
}
aa = zz, sort(hh, 0, m);
for (i = 0; i < n; i++) {
pp[i] = i - 1, qq[i] = i + 1;
top[i] = newnode(), yy1[i] = 0;
}
for (h = 0, i = 0; h < m; h++) {
int h_ = hh[h], i_;
while (i < n && yy[i_ = ii[i]] < zz[h_]) {
if (pp[i_] != -1)
qq[pp[i_]] = qq[i_];
if (qq[i_] != n)
pp[qq[i_]] = pp[i_];
i++;
}
for (i_ = ll[h_]; i_ != rr[h_]; i_ = qq[i_])
push(i_, zz[h_]);
push(i_, zz[h_]);
for (i_ = ll[h_]; i_ != rr[h_]; i_ = qq[i_])
add(top[i_], top[qq[i_]], xx[qq[i_]] - xx[i_]);
}
for (i = 0; i < n; i++)
push(i, yy[i]);
return dijkstra(s, t);
}
Compilation message
walk.cpp: In function 'void append(int, int)':
walk.cpp:47:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
47 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
147 ms |
52328 KB |
Output is correct |
4 |
Correct |
436 ms |
72772 KB |
Output is correct |
5 |
Correct |
181 ms |
61252 KB |
Output is correct |
6 |
Correct |
177 ms |
56160 KB |
Output is correct |
7 |
Correct |
184 ms |
61588 KB |
Output is correct |
8 |
Correct |
198 ms |
69904 KB |
Output is correct |
9 |
Correct |
263 ms |
61728 KB |
Output is correct |
10 |
Correct |
488 ms |
93856 KB |
Output is correct |
11 |
Correct |
166 ms |
37104 KB |
Output is correct |
12 |
Correct |
243 ms |
37496 KB |
Output is correct |
13 |
Correct |
516 ms |
84508 KB |
Output is correct |
14 |
Correct |
132 ms |
34848 KB |
Output is correct |
15 |
Correct |
156 ms |
36872 KB |
Output is correct |
16 |
Correct |
162 ms |
35684 KB |
Output is correct |
17 |
Correct |
185 ms |
34708 KB |
Output is correct |
18 |
Correct |
141 ms |
35652 KB |
Output is correct |
19 |
Correct |
6 ms |
2124 KB |
Output is correct |
20 |
Correct |
57 ms |
18240 KB |
Output is correct |
21 |
Correct |
173 ms |
32296 KB |
Output is correct |
22 |
Correct |
109 ms |
35884 KB |
Output is correct |
23 |
Correct |
243 ms |
40352 KB |
Output is correct |
24 |
Correct |
199 ms |
35652 KB |
Output is correct |
25 |
Correct |
166 ms |
34620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
11136 KB |
Output is correct |
2 |
Runtime error |
435 ms |
301456 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
11136 KB |
Output is correct |
2 |
Runtime error |
435 ms |
301456 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
147 ms |
52328 KB |
Output is correct |
21 |
Correct |
436 ms |
72772 KB |
Output is correct |
22 |
Correct |
181 ms |
61252 KB |
Output is correct |
23 |
Correct |
177 ms |
56160 KB |
Output is correct |
24 |
Correct |
184 ms |
61588 KB |
Output is correct |
25 |
Correct |
198 ms |
69904 KB |
Output is correct |
26 |
Correct |
263 ms |
61728 KB |
Output is correct |
27 |
Correct |
488 ms |
93856 KB |
Output is correct |
28 |
Correct |
166 ms |
37104 KB |
Output is correct |
29 |
Correct |
243 ms |
37496 KB |
Output is correct |
30 |
Correct |
516 ms |
84508 KB |
Output is correct |
31 |
Correct |
132 ms |
34848 KB |
Output is correct |
32 |
Correct |
156 ms |
36872 KB |
Output is correct |
33 |
Correct |
162 ms |
35684 KB |
Output is correct |
34 |
Correct |
185 ms |
34708 KB |
Output is correct |
35 |
Correct |
141 ms |
35652 KB |
Output is correct |
36 |
Correct |
6 ms |
2124 KB |
Output is correct |
37 |
Correct |
57 ms |
18240 KB |
Output is correct |
38 |
Correct |
173 ms |
32296 KB |
Output is correct |
39 |
Correct |
109 ms |
35884 KB |
Output is correct |
40 |
Correct |
243 ms |
40352 KB |
Output is correct |
41 |
Correct |
199 ms |
35652 KB |
Output is correct |
42 |
Correct |
166 ms |
34620 KB |
Output is correct |
43 |
Correct |
49 ms |
11136 KB |
Output is correct |
44 |
Runtime error |
435 ms |
301456 KB |
Execution killed with signal 11 |
45 |
Halted |
0 ms |
0 KB |
- |