이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "railroad_c.h"
#include <string.h>
#define N 200001
#define INF 0x3f3f3f3f
long long min(long long a, long long b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int xx[N * 2], uu[N * 2 - 1], vv[N * 2 - 1], ww[N * 2 - 1];
int compare_x(int i, int j) {
return xx[i] != xx[j] ? xx[i] - xx[j] : (i & 1) - (j & 1);
}
int compare_w(int h1, int h2) {
return ww[h1] - ww[h2];
}
void sort(int *ii, int l, int r, int (*compare)(int, int)) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k) {
int c = compare(ii[j], i_);
if (c == 0)
j++;
else if (c < 0) {
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, compare);
l = k;
}
}
int ds[N];
int find(int i) {
return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}
int join(int i, int j) {
i = find(i);
j = find(j);
if (i == j)
return 0;
if (ds[i] > ds[j])
ds[i] = j;
else {
if (ds[i] == ds[j])
ds[i]--;
ds[j] = i;
}
return 1;
}
long long plan_roller_coaster(int n, int *aa, int *bb) {
static int hh[N * 2 - 1], ii[N * 2];
int m, h, h_, i, j, d;
long long ans;
for (i = 0; i < n; i++)
xx[i << 1 | 0] = bb[i], xx[i << 1 | 1] = aa[i];
xx[n << 1 | 0] = 0, xx[n << 1 | 1] = INF, n++;
for (i = 0; i < n * 2; i++)
ii[i] = i;
sort(ii, 0, n * 2, compare_x);
memset(ds, -1, n * sizeof *ds);
ans = 0;
for (h = 0, d = 0; h < n * 2; h++) {
i = ii[h];
d += (i & 1) == 0 ? 1 : -1;
if (h + 1 < n * 2) {
j = ii[h + 1];
if (d < 0)
ans += (long long) -d * (xx[j] - xx[i]);
if (d != 0 && h + 1 < n * 2)
join(i >> 1, j >> 1);
}
}
m = 0;
for (h = 1; h < n * 2; h++) {
i = ii[h], j = ii[h - 1];
if (find(i >> 1) != find(j >> 1))
uu[m] = i >> 1, vv[m] = j >> 1, ww[m] = xx[i] - xx[j], m++;
}
for (h = 0; h < m; h++)
hh[h] = h;
sort(hh, 0, m, compare_w);
for (h = 0; h < m; h++) {
h_ = hh[h];
if (join(uu[h_], vv[h_]))
ans += ww[h_];
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |