#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100000
#define LN 18 /* LN = ceil(log2(N * 2 + 1)) */
#define N_ (1 << LN)
#define N1 (N * 2 + N * 2 * (LN + 1) * 2)
#define INF 0x7fffffff
#define LINF 0x3f3f3f3f3f3f3f3fLL
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int tt[N], ll[N], rr[N], cc[N], xx[N * 2], yy[N * 2];
int *zz;
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) {
int c = zz[ii[j]] != zz[i_] ? (zz[ii[j]] < zz[i_] ? -1 : 1) : (ii[j] & 1) - (i_ & 1);
if (c == 0)
j++;
else if (c < 0) {
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 *ej[N1], *ew[N1], eo[N1], n1;
int newnode() {
int i = n1++;
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
ew[i] = (int *) malloc(2 * sizeof *ew[i]);
return i;
}
void append(int i, int j, int w) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0) {
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ew[i] = (int *) realloc(ew[i], o * 2 * sizeof *ew[i]);
}
ej[i][o] = j, ew[i][o] = w;
}
int st[N_ * 2], n_;
void pul(int i) {
int l = i << 1, r = l | 1;
st[i] = newnode();
if (st[l] != -1)
append(st[i], st[l], 0);
if (st[r] != -1)
append(st[i], st[r], 0);
}
void update(int i, int u) {
i += n_;
st[i] = u << 1 | 0;
while (i > 1)
pul(i >>= 1);
}
void add_edges(int r, int i) {
int l = 0;
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1)
if ((r & 1) == 0) {
if (st[r] != -1)
append(i << 1 | 1, st[r], 0);
r--;
}
}
long long dd[N_]; int iq[N_ + 1], pq[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;
}
int main() {
static int ii[N * 2];
int n, x_, h, i, o;
scanf("%d%d", &x_, &n);
for (i = 0; i < n; i++) {
scanf("%d%d%d%d", &tt[i], &ll[i], &rr[i], &cc[i]), ll[i]--;
if (ll[i] == 0)
xx[i << 1 | 0] = -INF, yy[i << 1 | 0] = -INF;
else
xx[i << 1 | 0] = ll[i] - tt[i], yy[i << 1 | 0] = ll[i] + tt[i];
if (rr[i] == x_)
xx[i << 1 | 1] = INF, yy[i << 1 | 1] = INF;
else
xx[i << 1 | 1] = rr[i] - tt[i], yy[i << 1 | 1] = rr[i] + tt[i];
}
for (i = 0; i < n; i++) {
newnode(), newnode();
append(i << 1 | 0, i << 1 | 1, cc[i]);
}
for (i = 0; i < n * 2; i++)
ii[i] = i;
zz = xx, sort(ii, 0, n * 2);
for (i = 0; i < n * 2; i++)
xx[ii[i]] = i;
zz = yy, sort(ii, 0, n * 2);
n_ = 1;
while (n_ < n * 2 + 1)
n_ <<= 1;
memset(st, -1, n_ * 2 * sizeof *st);
for (h = 0; h < n * 2; h++) {
i = ii[h];
if ((i & 1) == 0)
update(xx[i], i >> 1);
else
add_edges(xx[i], i >> 1);
}
cnt = 0;
memset(dd, 0x3f, n1 * sizeof *dd);
for (i = 0; i < n; i++)
if (ll[i] == 0)
dd[i << 1 | 0] = 0, pq_add_last(i << 1 | 0);
while (cnt) {
i = pq_remove_first();
if (i < n * 2 && (i & 1) != 0 && rr[i >> 1] == x_) {
printf("%lld\n", dd[i]);
return 0;
}
for (o = eo[i]; o--; ) {
int j = ej[i][o];
long long w = ew[i][o], d = dd[i] + w;
if (dd[j] > d) {
if (dd[j] == LINF)
pq_add_last(j);
dd[j] = d, pq_up(j);
}
}
}
printf("-1\n");
return 0;
}
Compilation message
treatment.c: In function 'append':
treatment.c:57:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
57 | if (o >= 2 && (o & o - 1) == 0) {
| ~~^~~
treatment.c: In function 'main':
treatment.c:135:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | scanf("%d%d", &x_, &n);
| ^~~~~~~~~~~~~~~~~~~~~~
treatment.c:137:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
137 | scanf("%d%d%d%d", &tt[i], &ll[i], &rr[i], &cc[i]), ll[i]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
407 ms |
357416 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
15 ms |
8148 KB |
Output is correct |
21 |
Correct |
15 ms |
8192 KB |
Output is correct |
22 |
Correct |
14 ms |
8304 KB |
Output is correct |
23 |
Correct |
13 ms |
8276 KB |
Output is correct |
24 |
Correct |
14 ms |
8300 KB |
Output is correct |
25 |
Correct |
22 ms |
8276 KB |
Output is correct |
26 |
Correct |
17 ms |
8416 KB |
Output is correct |
27 |
Correct |
12 ms |
8308 KB |
Output is correct |
28 |
Correct |
16 ms |
8204 KB |
Output is correct |
29 |
Correct |
18 ms |
8204 KB |
Output is correct |
30 |
Correct |
11 ms |
8020 KB |
Output is correct |
31 |
Correct |
10 ms |
8020 KB |
Output is correct |
32 |
Correct |
13 ms |
8276 KB |
Output is correct |
33 |
Correct |
12 ms |
8148 KB |
Output is correct |
34 |
Correct |
14 ms |
8280 KB |
Output is correct |
35 |
Correct |
11 ms |
8132 KB |
Output is correct |
36 |
Correct |
12 ms |
8276 KB |
Output is correct |
37 |
Correct |
13 ms |
8276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
407 ms |
357416 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |