Submission #670862

# Submission time Handle Problem Language Result Execution time Memory
670862 2022-12-10T22:16:14 Z rainboy Treatment Project (JOI20_treatment) C
0 / 100
3000 ms 18632 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

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 (tt[i] >= tt[j] && (rr[j] < x_ && ll[i] <= rr[j] - (tt[i] - tt[j]) && rr[j] - (tt[i] - tt[j]) <= rr[i] || ll[j] > 0 && ll[i] <= ll[j] + (tt[i] - tt[j]) && ll[j] + (tt[i] - tt[j]) <= rr[i]))
					append(i << 1 | 1, j << 1 | 0, 0), append(j << 1 | 1, i << 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:67:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   67 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
treatment.c: In function 'main':
treatment.c:171:75: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  171 |     if (tt[i] >= tt[j] && (rr[j] < x_ && ll[i] <= rr[j] - (tt[i] - tt[j]) && rr[j] - (tt[i] - tt[j]) <= rr[i] || ll[j] > 0 && ll[i] <= ll[j] + (tt[i] - tt[j]) && ll[j] + (tt[i] - tt[j]) <= rr[i]))
      |                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treatment.c:158:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  158 |  scanf("%d%d", &x_, &n);
      |  ^~~~~~~~~~~~~~~~~~~~~~
treatment.c:160:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  160 |   scanf("%d%d%d%d", &tt[i], &ll[i], &rr[i], &cc[i]), ll[i]--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Execution timed out 3077 ms 18632 KB Time limit exceeded
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 Incorrect 0 ms 340 KB Output isn't correct
5 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 Incorrect 0 ms 340 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3077 ms 18632 KB Time limit exceeded
2 Halted 0 ms 0 KB -