#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))
#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[N1]; int iq[N1 + 1], pq[N1], 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 |
Correct |
328 ms |
199848 KB |
Output is correct |
2 |
Correct |
307 ms |
198804 KB |
Output is correct |
3 |
Correct |
371 ms |
199392 KB |
Output is correct |
4 |
Correct |
352 ms |
199260 KB |
Output is correct |
5 |
Correct |
291 ms |
199144 KB |
Output is correct |
6 |
Correct |
297 ms |
200240 KB |
Output is correct |
7 |
Correct |
277 ms |
201676 KB |
Output is correct |
8 |
Correct |
206 ms |
191968 KB |
Output is correct |
9 |
Correct |
220 ms |
192908 KB |
Output is correct |
10 |
Correct |
220 ms |
194248 KB |
Output is correct |
11 |
Correct |
316 ms |
198920 KB |
Output is correct |
12 |
Correct |
332 ms |
200140 KB |
Output is correct |
13 |
Correct |
374 ms |
202884 KB |
Output is correct |
14 |
Correct |
374 ms |
202892 KB |
Output is correct |
15 |
Correct |
366 ms |
202572 KB |
Output is correct |
16 |
Correct |
384 ms |
202680 KB |
Output is correct |
17 |
Correct |
352 ms |
201836 KB |
Output is correct |
18 |
Correct |
303 ms |
197516 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 |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 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 |
1 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 |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 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 |
1 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 |
16 ms |
8248 KB |
Output is correct |
21 |
Correct |
16 ms |
8208 KB |
Output is correct |
22 |
Correct |
15 ms |
8276 KB |
Output is correct |
23 |
Correct |
13 ms |
8276 KB |
Output is correct |
24 |
Correct |
15 ms |
8212 KB |
Output is correct |
25 |
Correct |
20 ms |
8352 KB |
Output is correct |
26 |
Correct |
16 ms |
8320 KB |
Output is correct |
27 |
Correct |
11 ms |
8352 KB |
Output is correct |
28 |
Correct |
16 ms |
8304 KB |
Output is correct |
29 |
Correct |
16 ms |
8264 KB |
Output is correct |
30 |
Correct |
10 ms |
8032 KB |
Output is correct |
31 |
Correct |
10 ms |
7932 KB |
Output is correct |
32 |
Correct |
13 ms |
8276 KB |
Output is correct |
33 |
Correct |
14 ms |
8140 KB |
Output is correct |
34 |
Correct |
15 ms |
8276 KB |
Output is correct |
35 |
Correct |
11 ms |
8148 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 |
Correct |
328 ms |
199848 KB |
Output is correct |
2 |
Correct |
307 ms |
198804 KB |
Output is correct |
3 |
Correct |
371 ms |
199392 KB |
Output is correct |
4 |
Correct |
352 ms |
199260 KB |
Output is correct |
5 |
Correct |
291 ms |
199144 KB |
Output is correct |
6 |
Correct |
297 ms |
200240 KB |
Output is correct |
7 |
Correct |
277 ms |
201676 KB |
Output is correct |
8 |
Correct |
206 ms |
191968 KB |
Output is correct |
9 |
Correct |
220 ms |
192908 KB |
Output is correct |
10 |
Correct |
220 ms |
194248 KB |
Output is correct |
11 |
Correct |
316 ms |
198920 KB |
Output is correct |
12 |
Correct |
332 ms |
200140 KB |
Output is correct |
13 |
Correct |
374 ms |
202884 KB |
Output is correct |
14 |
Correct |
374 ms |
202892 KB |
Output is correct |
15 |
Correct |
366 ms |
202572 KB |
Output is correct |
16 |
Correct |
384 ms |
202680 KB |
Output is correct |
17 |
Correct |
352 ms |
201836 KB |
Output is correct |
18 |
Correct |
303 ms |
197516 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
16 ms |
8248 KB |
Output is correct |
39 |
Correct |
16 ms |
8208 KB |
Output is correct |
40 |
Correct |
15 ms |
8276 KB |
Output is correct |
41 |
Correct |
13 ms |
8276 KB |
Output is correct |
42 |
Correct |
15 ms |
8212 KB |
Output is correct |
43 |
Correct |
20 ms |
8352 KB |
Output is correct |
44 |
Correct |
16 ms |
8320 KB |
Output is correct |
45 |
Correct |
11 ms |
8352 KB |
Output is correct |
46 |
Correct |
16 ms |
8304 KB |
Output is correct |
47 |
Correct |
16 ms |
8264 KB |
Output is correct |
48 |
Correct |
10 ms |
8032 KB |
Output is correct |
49 |
Correct |
10 ms |
7932 KB |
Output is correct |
50 |
Correct |
13 ms |
8276 KB |
Output is correct |
51 |
Correct |
14 ms |
8140 KB |
Output is correct |
52 |
Correct |
15 ms |
8276 KB |
Output is correct |
53 |
Correct |
11 ms |
8148 KB |
Output is correct |
54 |
Correct |
12 ms |
8276 KB |
Output is correct |
55 |
Correct |
13 ms |
8276 KB |
Output is correct |
56 |
Correct |
485 ms |
199272 KB |
Output is correct |
57 |
Correct |
445 ms |
200244 KB |
Output is correct |
58 |
Correct |
508 ms |
200396 KB |
Output is correct |
59 |
Correct |
490 ms |
200604 KB |
Output is correct |
60 |
Correct |
396 ms |
199764 KB |
Output is correct |
61 |
Correct |
524 ms |
200232 KB |
Output is correct |
62 |
Correct |
488 ms |
199144 KB |
Output is correct |
63 |
Correct |
322 ms |
197276 KB |
Output is correct |
64 |
Correct |
309 ms |
197344 KB |
Output is correct |
65 |
Correct |
325 ms |
203288 KB |
Output is correct |
66 |
Correct |
356 ms |
199756 KB |
Output is correct |
67 |
Correct |
779 ms |
203128 KB |
Output is correct |
68 |
Correct |
579 ms |
203164 KB |
Output is correct |
69 |
Correct |
477 ms |
203488 KB |
Output is correct |
70 |
Correct |
743 ms |
203052 KB |
Output is correct |
71 |
Correct |
612 ms |
203036 KB |
Output is correct |
72 |
Correct |
438 ms |
203184 KB |
Output is correct |
73 |
Correct |
696 ms |
202724 KB |
Output is correct |
74 |
Correct |
271 ms |
195280 KB |
Output is correct |
75 |
Correct |
247 ms |
195008 KB |
Output is correct |
76 |
Correct |
337 ms |
200016 KB |
Output is correct |
77 |
Correct |
347 ms |
200364 KB |
Output is correct |
78 |
Correct |
425 ms |
202920 KB |
Output is correct |
79 |
Correct |
742 ms |
202644 KB |
Output is correct |
80 |
Correct |
430 ms |
202592 KB |
Output is correct |
81 |
Correct |
303 ms |
195092 KB |
Output is correct |
82 |
Correct |
426 ms |
201784 KB |
Output is correct |
83 |
Correct |
525 ms |
201768 KB |
Output is correct |
84 |
Correct |
640 ms |
201936 KB |
Output is correct |
85 |
Correct |
389 ms |
202856 KB |
Output is correct |
86 |
Correct |
442 ms |
200780 KB |
Output is correct |
87 |
Correct |
412 ms |
200872 KB |
Output is correct |
88 |
Correct |
381 ms |
202928 KB |
Output is correct |
89 |
Correct |
429 ms |
202572 KB |
Output is correct |
90 |
Correct |
426 ms |
202924 KB |
Output is correct |
91 |
Correct |
298 ms |
195020 KB |
Output is correct |
92 |
Correct |
518 ms |
200708 KB |
Output is correct |
93 |
Correct |
629 ms |
202200 KB |
Output is correct |
94 |
Correct |
460 ms |
201596 KB |
Output is correct |
95 |
Correct |
510 ms |
200812 KB |
Output is correct |
96 |
Correct |
355 ms |
202268 KB |
Output is correct |
97 |
Correct |
458 ms |
203356 KB |
Output is correct |
98 |
Correct |
470 ms |
203444 KB |
Output is correct |
99 |
Correct |
428 ms |
202052 KB |
Output is correct |
100 |
Correct |
360 ms |
199452 KB |
Output is correct |
101 |
Correct |
627 ms |
203592 KB |
Output is correct |
102 |
Correct |
405 ms |
201744 KB |
Output is correct |
103 |
Correct |
486 ms |
198796 KB |
Output is correct |