#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_++] = aa[j2] - aa[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]);
| ^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |