Submission #940850

#TimeUsernameProblemLanguageResultExecution timeMemory
940850rainboy사회적 불평등 (TOKI14_social)C11
100 / 100
39 ms5980 KiB
#include <stdio.h>

#define N	100000

unsigned int X = 12345;

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

int *ww;

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

void update(long long *ft, int i, int n, long long x) {
	while (i < n) {
		ft[i] += x;
		i |= i + 1;
	}
}

long long query(long long *ft, int i) {
	long long x = 0;

	while (i >= 0) {
		x += ft[i];
		i &= i + 1, i--;
	}
	return x;
}

int main() {
	static int xx[N], yy[N], yy_[N], ii[N];
	static long long ftk[N], ftx[N], fty[N], ftz[N];
	int n, h, i, k, k_;
	long long x, x_, y, y_, z, z_, ans;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d%d", &xx[i], &yy[i]);
	for (i = 0; i < n; i++)
		ii[i] = i;
	ww = yy, sort(ii, 0, n);
	for (i = 0; i < n; i++)
		yy_[ii[i]] = i;
	ww = xx, sort(ii, 0, n);
	ans = 0;
	k_ = x_ = y_ = z_ = 0;
	for (h = 0; h < n; h++) {
		i = ii[h];
		k = query(ftk, yy_[i]);
		x = query(ftx, yy_[i]);
		y = query(fty, yy_[i]);
		z = query(ftz, yy_[i]);
		ans += (long long) xx[i] * yy[i] * k - xx[i] * y - x * yy[i] + z;
		k = k_ - k, x = x_ - x, y = y_ - y, z = z_ - z;
		ans -= (long long) xx[i] * yy[i] * k - xx[i] * y - x * yy[i] + z;
		k_++, x_ += xx[i], y_ += yy[i], z_ += (long long) xx[i] * yy[i];
		update(ftk, yy_[i], n, 1);
		update(ftx, yy_[i], n, xx[i]);
		update(fty, yy_[i], n, yy[i]);
		update(ftz, yy_[i], n, (long long) xx[i] * yy[i]);
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message (stderr)

social.c: In function 'main':
social.c:55:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
social.c:57:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |   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...
#Verdict Execution timeMemoryGrader output
Fetching results...