답안 #670865

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
670865 2022-12-10T22:31:12 Z rainboy 치료 계획 (JOI20_treatment) C
35 / 100
1078 ms 524288 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	100000
#define LN	18	/* LN = ceil(log2(N * 2)) */
#define N_	(1 << LN)
#define N1	(N * 2 + N * (LN + 1) * 4)
#define INF	0x7fffffff
#define LINF	0x3f3f3f3f3f3f3f3fLL

int abs_(int a) { return a > 0 ? a : -a; }

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

int tt[N], ll[N], rr[N], cc[N], xx[N * 2];

int compare_t(int i, int j) {
	return tt[i] - tt[j];
}

int compare_x(int i, int j) {
	return xx[i] != xx[j] ? (xx[i] < xx[j] ? -1 : 1) : (i & 1) - (j & 1);
}

int (*compare)(int, int);

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 = compare(ii[j], i_);

			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 st0[N_ * 2], st1[N_ * 2], n_;

void pul(int i) {
	int l = i << 1, r = l | 1;

	st0[i] = newnode();
	if (st0[l] != -1)
		append(st0[i], st0[l], 0);
	if (st0[r] != -1)
		append(st0[i], st0[r], 0);
	st1[i] = newnode();
	if (st1[l] != -1)
		append(st1[l], st1[i], 0);
	if (st1[r] != -1)
		append(st1[r], st1[i], 0);
}

void update(int i, int u) {
	i += n_;
	st0[i] = u << 1 | 0, st1[i] = u << 1 | 1;
	while (i > 1)
		pul(i >>= 1);
}

void add_edges(int i, int l, int r) {
	for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1) {
			if (st0[l] != -1)
				append(i << 1 | 1, st0[l], 0);
			if (st1[l] != -1)
				append(st1[l], i << 1 | 0, 0);
			l++;
		}
		if ((r & 1) == 0) {
			if (st0[r] != -1)
				append(i << 1 | 1, st0[r], 0);
			if (st1[r] != -1)
				append(st1[r], i << 1 | 0, 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];
	int n, x_, i, j, 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]--;
		ii[i] = i;
	}
	compare = compare_t, sort(ii, 0, n);
	for (i = 0; i < n; i++) {
		newnode(), newnode();
		append(i << 1 | 0, i << 1 | 1, cc[i]);
	}
	for (i = 0; i < n; i++)
		for (j = 0; j < n; j++)
			if (i != j)
				if (rr[i] == x_ || ll[j] == 0 || rr[i] - ll[j] >= abs_(tt[i] - tt[j]))
					append(i << 1 | 1, j << 1 | 0, 0);
#if 0
	n_ = 1;
	while (n_ < n * 2)
		n_ <<= 1;
	for (g = 0; g < 2; g++) {
		for (i = 0; i < n; i++) {
			xx[i << 1 | 0] = ll[i] == 0 ? -INF : ll[i] + tt[i];
			xx[i << 1 | 1] = rr[i] == x_ ? INF : rr[i] + tt[i];
		}
		for (i = 0; i < n * 2; i++)
			ii_[i] = i;
		compare = compare_x, sort(ii_, 0, n * 2);
		for (i = 0; i < n * 2; i++)
			xx[ii_[i]] = i;
		memset(st0, -1, n_ * 2 * sizeof *st0), memset(st1, -1, n_ * 2 * sizeof *st1);
		for (h = 0; h < n; h++) {
			i = ii[h];
			add_edges(i, xx[i << 1 | 0], xx[i << 1 | 1]);
			update(xx[i << 1 | 1], i);
		}
		for (i = 0; i < n; i++)
			tmp = x_ - ll[i], ll[i] = x_ - rr[i], rr[i] = tmp;
	}
#endif
	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:69:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   69 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
treatment.c: In function 'main':
treatment.c:160:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  160 |  scanf("%d%d", &x_, &n);
      |  ^~~~~~~~~~~~~~~~~~~~~~
treatment.c:162:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  162 |   scanf("%d%d%d%d", &tt[i], &ll[i], &rr[i], &cc[i]), ll[i]--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1078 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 0 ms 340 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 296 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
# 결과 실행 시간 메모리 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 0 ms 340 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 296 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 290 ms 166948 KB Output is correct
21 Correct 283 ms 167344 KB Output is correct
22 Correct 47 ms 4112 KB Output is correct
23 Correct 48 ms 4148 KB Output is correct
24 Correct 171 ms 128092 KB Output is correct
25 Correct 128 ms 73992 KB Output is correct
26 Correct 111 ms 67964 KB Output is correct
27 Correct 123 ms 81528 KB Output is correct
28 Correct 171 ms 127788 KB Output is correct
29 Correct 123 ms 73568 KB Output is correct
30 Correct 116 ms 78324 KB Output is correct
31 Correct 113 ms 83352 KB Output is correct
32 Correct 234 ms 171596 KB Output is correct
33 Correct 247 ms 202868 KB Output is correct
34 Correct 243 ms 129476 KB Output is correct
35 Correct 236 ms 172932 KB Output is correct
36 Correct 263 ms 202628 KB Output is correct
37 Correct 240 ms 128916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1078 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -