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 M 100000
#define N_ (M * 4)
#define M_ (M + N_)
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int xx[N_], xx_[N_], ii[M_], jj[M_], ww[M_];
int compare_x(int h1, int h2) {
return xx[h1] != xx[h2] ? xx[h1] - xx[h2] : (h1 & 1) - (h2 & 1);
}
int compare_w(int h1, int h2) {
return ww[h2] - ww[h1];
}
int (*compare)(int, int);
void sort(int *hh, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, h = hh[l + rand_() % (r - l)], tmp;
while (j < k) {
int c = compare(hh[j], h);
if (c == 0)
j++;
else if (c < 0) {
tmp = hh[i], hh[i] = hh[j], hh[j] = tmp;
i++, j++;
} else {
k--;
tmp = hh[j], hh[j] = hh[k], hh[k] = tmp;
}
}
sort(hh, l, i);
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;
}
int pq[M * 2], iq[1 + M * 2], cnt;
int lt(int i, int j) { return ww[i >> 1] > ww[j >> 1]; }
int p2(int p) {
return (p *= 2) > cnt ? 0 : (p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p);
}
void pq_up(int i) {
int p, q, j;
for (p = pq[i]; (q = p / 2) && lt(i, j = iq[q]); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_dn(int i) {
int p, q, j;
for (p = pq[i]; (q = p2(p)) && lt(j = iq[q], i); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_add(int i) {
pq[i] = ++cnt, pq_up(i);
}
void pq_remove(int i) {
int j = iq[cnt--];
if (j != i)
pq[j] = pq[i], pq_up(j), pq_dn(j);
pq[i] = 0;
}
int pq_first() {
return iq[1];
}
int main() {
static int hh[M_], ww_[N_];
int n, n1, n2, n_, m, m_, h, h_, i, c;
long long w;
scanf("%d%d%*d%d", &n1, &n2, &m), n = n1 + n2;
for (h = 0; h < m; h++) {
scanf("%d%d%d%d%d", &xx[h << 2 | 0], &xx[h << 2 | 1], &xx[h << 2 | 2], &xx[h << 2 | 3], &ww[h]);
xx[h << 2 | 0]--, xx[h << 2 | 1]--, xx[h << 2 | 2]--, xx[h << 2 | 3]--;
xx[h << 2 | 2] += n1, xx[h << 2 | 3] += n1;
}
for (h = 0; h < m * 4; h++)
hh[h] = h;
compare = compare_x, sort(hh, 0, m * 4);
n_ = 0;
for (h = 0; h < m * 4; h++) {
int x;
h_ = hh[h], x = xx[h_];
if ((h_ & 1) == 0)
pq_add(h_ >> 1);
else
pq_remove(h_ >> 1);
xx[h_] = n_;
if (h + 1 == m * 4 || xx[hh[h + 1]] != x)
ww_[n_] = cnt ? ww[pq_first() >> 1] : 0, xx_[n_] = x, n_++;
}
for (h = 0; h < m; h++)
ii[h] = xx[h << 2 | 0], jj[h] = xx[h << 2 | 2];
w = 0, c = n, m_ = m;
for (i = 0; i + 1 < n_; i++)
if (ww_[i]) {
w += (long long) (xx_[i + 1] - xx_[i] - 1) * ww_[i];
c -= xx_[i + 1] - xx_[i] - 1;
ii[m_] = i, jj[m_] = i + 1, ww[m_] = ww_[i], m_++;
}
memset(ds, -1, n_ * sizeof *ds);
for (h = 0; h < m_; h++)
hh[h] = h;
compare = compare_w, sort(hh, 0, m_);
for (h = 0; h < m_; h++) {
h_ = hh[h];
if (join(ii[h_], jj[h_]))
w += ww[h_], c--;
}
if (c != 1)
w = -1;
printf("%lld\n", w);
return 0;
}
Compilation message (stderr)
invitation.c: In function 'main':
invitation.c:114:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
114 | scanf("%d%d%*d%d", &n1, &n2, &m), n = n1 + n2;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
invitation.c:116:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | scanf("%d%d%d%d%d", &xx[h << 2 | 0], &xx[h << 2 | 1], &xx[h << 2 | 2], &xx[h << 2 | 3], &ww[h]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |