This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <string.h>
#define N 100001
#define N_ (1 << 18) /* N_ = pow2(ceil(log2(N * 2))) */
int min(int a, int b) { return a < b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int xx[N], yy[N * 2], yy_[N * 2], hh[N * 2];
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;
}
}
int st[N_ * 2], n_;
int min_(int i, int j) { return j == -1 || i != -1 && xx[i] < xx[j] ? i : j; }
void update(int l, int r, int x) {
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1)
st[l] = min_(st[l], x), l++;
if ((r & 1) == 0)
st[r] = min_(st[r], x), r--;
}
}
int query(int i) {
int x = -1;
for (i += n_; i > 0; i >>= 1)
x = min_(x, st[i]);
return x;
}
int main() {
static int ii[N * 2], jj[N * 2], dp[N * 2];
static char used[N * 2], used_[N * 2];
int n, m, x, y, y_, h, i, i0, i1, j, j0, j1, k;
scanf("%d%d%d", &n, &y_, &x), n++;
xx[0] = 0, yy[0 << 1 | 0] = y_, yy[0 << 1 | 1] = y_;
for (i = 1; i < n; i++)
scanf("%d%d%d", &xx[i], &yy[i << 1 | 0], &yy[i << 1 | 1]);
for (i = 0; i < n * 2; i++)
ii[i] = i;
ww = yy, sort(ii, 0, n * 2);
m = 0;
for (i = 0; i < n * 2; i = j) {
j = i, y = yy[ii[i]];
while (j < n * 2 && yy[ii[j]] == y)
hh[ii[j++]] = m;
yy_[m++] = y;
}
n_ = 1;
while (n_ < n * 2)
n_ <<= 1;
memset(st, -1, n_ * 2 * sizeof *st);
for (i = 0; i < n; i++)
ii[i] = i;
ww = xx, sort(ii, 0, n);
for (h = n - 1; h >= 0; h--) {
i = ii[h], i0 = i << 1 | 0, i1 = i << 1 | 1;
j = jj[i0] = query(hh[i0]);
if (j == -1)
dp[i0] = 0;
else {
j0 = j << 1 | 0, j1 = j << 1 | 1;
dp[i0] = min(dp[j0] + yy[i0] - yy[j0], dp[j1] + yy[j1] - yy[i0]);
}
j = jj[i1] = query(hh[i1]);
if (j == -1)
dp[i1] = 0;
else {
j0 = j << 1 | 0, j1 = j << 1 | 1;
dp[i1] = min(dp[j0] + yy[i1] - yy[j0], dp[j1] + yy[j1] - yy[i1]);
}
update(hh[i0] + 1, hh[i1] - 1, i);
}
printf("%d\n", x + dp[0]);
used[0 << 1 | 0] = used[0 << 1 | 1] = 1;
for (h = 0; h < n; h++) {
i = ii[h], i0 = i << 1 | 0, i1 = i << 1 | 1;
i = ii[h];
if (used[i0]) {
j = jj[i0];
if (j == -1)
used_[hh[i0]] = 1;
else {
j0 = j << 1 | 0, j1 = j << 1 | 1;
if (dp[i0] == dp[j0] + yy[i0] - yy[j0])
used[j0] = 1;
if (dp[i0] == dp[j1] + yy[j1] - yy[i0])
used[j1] = 1;
}
}
if (used[i1]) {
j = jj[i1];
if (j == -1)
used_[hh[i1]] = 1;
else {
j0 = j << 1 | 0, j1 = j << 1 | 1;
if (dp[i1] == dp[j0] + yy[i1] - yy[j0])
used[j0] = 1;
if (dp[i1] == dp[j1] + yy[j1] - yy[i1])
used[j1] = 1;
}
}
}
k = 0;
for (h = 0; h < m; h++)
if (used_[h])
k++;
printf("%d", k);
for (h = 0; h < m; h++)
if (used_[h])
printf(" %d", yy_[h]);
printf("\n");
return 0;
}
Compilation message (stderr)
dd.c: In function 'min_':
dd.c:39:52: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
39 | int min_(int i, int j) { return j == -1 || i != -1 && xx[i] < xx[j] ? i : j; }
| ~~~~~~~~^~~~~~~~~~~~~~~~
dd.c: In function 'main':
dd.c:63:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | scanf("%d%d%d", &n, &y_, &x), n++;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
dd.c:66:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | scanf("%d%d%d", &xx[i], &yy[i << 1 | 0], &yy[i << 1 | 1]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |