Submission #670866

#TimeUsernameProblemLanguageResultExecution timeMemory
670866rainboyTreatment Project (JOI20_treatment)C11
35 / 100
404 ms357384 KiB
#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 * (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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...