Submission #706495

#TimeUsernameProblemLanguageResultExecution timeMemory
706495rainboyCoin Collecting (JOI19_ho_t4)C11
100 / 100
98 ms12124 KiB
#include <stdio.h>

#define N	100000

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

unsigned int X = 12345;

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

int xx[N * 4], yy[N * 4], tt[N * 4];

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)
			if (xx[ii[j]] == xx[i_])
				j++;
			else if (xx[ii[j]] < xx[i_]) {
				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;
	}
}

long long dist(int i, int j) {
	return (long long) abs_(xx[j] - xx[i]) + abs_(yy[j] - yy[i]);
}

int main() {
	static int ii[N * 4], qu[2][N * 4], cnt[2];
	static char used[N * 4];
	int n, h, i, i_, j, d, t;
	long long ans;

	scanf("%d", &n);
	for (i = 0; i < n * 2; i++)
		scanf("%d%d", &xx[i], &yy[i]);
	for (i = 0; i < n * 2; i++)
		xx[n * 2 + i] = i / 2 + 1, yy[n * 2 + i] = i % 2 + 1;
	for (i = 0; i < n * 4; i++)
		tt[i] = yy[i] <= 1 ? 0 : 1;
	for (i = 0; i < n * 4; i++)
		ii[i] = i;
	sort(ii, 0, n * 4);
	d = 0, ans = 0;
	for (i = 0; i < n * 4; i = j) {
		j = i + 1;
		while (j < n * 4 && xx[ii[j]] == xx[ii[i]])
			j++;
		if (d > 0) {
			for (h = i; h < j; h++) {
				i_ = ii[h], t = tt[i_];
				if (i_ < n * 2)
					qu[t][cnt[t]++] = i_, d++;
			}
			for (h = i; h < j; h++) {
				i_ = ii[h], t = tt[i_];
				if (i_ >= n * 2 && cnt[t])
					ans += dist(i_, qu[t][--cnt[t]]), d--, used[i_] = 1;
			}
			for (h = i; h < j; h++) {
				i_ = ii[h], t = tt[i_];
				if (i_ >= n * 2 && !used[i_]) {
					if (d > 0)
						ans += dist(i_, qu[t ^ 1][--cnt[t ^ 1]]);
					else
						qu[t][cnt[t]++] = i_;
					d--;
				}
			}
		} else {
			for (h = i; h < j; h++) {
				i_ = ii[h], t = tt[i_];
				if (i_ >= n * 2)
					qu[t][cnt[t]++] = i_, d--;
			}
			for (h = i; h < j; h++) {
				i_ = ii[h], t = tt[i_];
				if (i_ < n * 2 && cnt[t])
					ans += dist(i_, qu[t][--cnt[t]]), d++, used[i_] = 1;
			}
			for (h = i; h < j; h++) {
				i_ = ii[h], t = tt[i_];
				if (i_ < n * 2 && !used[i_]) {
					if (d < 0)
						ans += dist(i_, qu[t ^ 1][--cnt[t ^ 1]]);
					else
						qu[t][cnt[t]++] = i_;
					d++;
				}
			}
		}
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message (stderr)

joi2019_ho_t4.c: In function 'main':
joi2019_ho_t4.c:44:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
joi2019_ho_t4.c:46:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |   scanf("%d%d", &xx[i], &yy[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...