#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;
}
}
int dist(int i, int j) {
return abs_(xx[j] - xx[i]) + abs_(yy[j] - yy[i]);
}
int main() {
static int ii[N * 4], qu[2][N * 4], cnt[2];
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) {
if (d > 0)
ans += cnt[t] ? dist(i_, qu[t][--cnt[t]]) : 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) {
if (d < 0)
ans += cnt[t] ? dist(i_, qu[t][--cnt[t]]) : dist(i_, qu[t ^ 1][--cnt[t ^ 1]]);
else
qu[t][cnt[t]++] = i_;
d++;
}
}
}
}
printf("%lld\n", ans);
return 0;
}
Compilation message
joi2019_ho_t4.c: In function 'main':
joi2019_ho_t4.c:43:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
joi2019_ho_t4.c:45:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf("%d%d", &xx[i], &yy[i]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
292 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
292 KB |
Output is correct |
5 |
Correct |
0 ms |
296 KB |
Output is correct |
6 |
Correct |
0 ms |
296 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Incorrect |
0 ms |
296 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
292 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
292 KB |
Output is correct |
5 |
Correct |
0 ms |
296 KB |
Output is correct |
6 |
Correct |
0 ms |
296 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Incorrect |
0 ms |
296 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
292 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
292 KB |
Output is correct |
5 |
Correct |
0 ms |
296 KB |
Output is correct |
6 |
Correct |
0 ms |
296 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Incorrect |
0 ms |
296 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |