답안 #741224

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
741224 2023-05-13T21:55:14 Z rainboy Count Squares (CEOI19_countsquares) C
100 / 100
278 ms 9104 KB
#include <stdio.h>

#define N	1500
#define N2	(N * (N - 1) / 2)

unsigned int X = 12345;

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

void sort(int *aa, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, a = aa[l + rand_() % (r - l)], tmp;

		while (j < k)
			if (aa[j] == a)
				j++;
			else if (aa[j] < a) {
				tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = aa[j], aa[j] = aa[k], aa[k] = tmp;
			}
		sort(aa, l, i);
		l = k;
	}
}

int main() {
	static int aa[N], bb[N], aa_[N2], bb_[N2];
	int n, n_, m, m_, i, i_, i1, i2, j, j_, j1, j2;
	long long ans;

	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	for (j = 0; j < m; j++)
		scanf("%d", &bb[j]);
	n_ = 0;
	for (i1 = 0; i1 < n; i1++)
		for (i2 = i1 + 1; i2 < n; i2++)
			aa_[n_++] = aa[i2] - aa[i1];
	sort(aa_, 0, n_);
	m_ = 0;
	for (j1 = 0; j1 < m; j1++)
		for (j2 = j1 + 1; j2 < m; j2++)
			bb_[m_++] = bb[j2] - bb[j1];
	sort(bb_, 0, m_);
	i = 0, j = 0, ans = 0;
	while (i < n_ && j < m_)
		if (aa_[i] < bb_[j])
			i++;
		else if (aa_[i] > bb_[j])
			j++;
		else {
			i_ = i + 1;
			while (i_ < n_ && aa_[i_] == aa_[i])
				i_++;
			j_ = j + 1;
			while (j_ < m_ && bb_[j_] == bb_[j])
				j_++;
			ans += (long long) (i_ - i) * (j_ - j);
			i = i_, j = j_;
		}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

countsquares.c: In function 'main':
countsquares.c:36:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
countsquares.c:38:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
countsquares.c:40:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |   scanf("%d", &bb[j]);
      |   ^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 38 ms 1656 KB Output is correct
7 Correct 40 ms 1704 KB Output is correct
8 Correct 44 ms 1712 KB Output is correct
9 Correct 39 ms 1664 KB Output is correct
10 Correct 12 ms 1708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 38 ms 1656 KB Output is correct
7 Correct 40 ms 1704 KB Output is correct
8 Correct 44 ms 1712 KB Output is correct
9 Correct 39 ms 1664 KB Output is correct
10 Correct 12 ms 1708 KB Output is correct
11 Correct 257 ms 9104 KB Output is correct
12 Correct 261 ms 9080 KB Output is correct
13 Correct 259 ms 9068 KB Output is correct
14 Correct 278 ms 8900 KB Output is correct
15 Correct 64 ms 9072 KB Output is correct