Submission #437670

#TimeUsernameProblemLanguageResultExecution timeMemory
437670rainboyFountain Parks (IOI21_parks)C++17
100 / 100
229 ms19324 KiB
/* https://codeforces.com/blog/entry/92083#comment-807698 (frodakcin) */ #include "parks.h" #include <string.h> using namespace std; typedef vector<int> vi; const int N = 200000; unsigned int X = 12345; int rand_() { return (X *= 3) >> 1; } int xx[N], yy[N]; 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) { int c = xx[ii[j]] != xx[i_] ? xx[ii[j]] - xx[i_] : yy[ii[j]] - yy[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); 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 ii[N], jj[N][4]; int construct_roads(vi xx_, vi yy_) { int n = xx_.size(), m, i, j, tmp; vi uu(n - 1), vv(n - 1), aa(n - 1), bb(n - 1); for (i = 0; i < n; i++) xx[i] = xx_[i] / 2, yy[i] = yy_[i] / 2, ii[i] = i; sort(ii, 0, n); for (i = 0; i < n; i++) jj[i][0] = jj[i][1] = jj[i][2] = jj[i][3] = -1; for (i = 1; i < n; i++) if (xx[ii[i - 1]] == xx[ii[i]] && yy[ii[i - 1]] + 1 == yy[ii[i]]) jj[ii[i - 1]][0] = ii[i], jj[ii[i]][2] = ii[i - 1]; for (i = 0; i < n; i++) tmp = xx[i], xx[i] = yy[i], yy[i] = tmp; sort(ii, 0, n); for (i = 1; i < n; i++) if (xx[ii[i - 1]] == xx[ii[i]] && yy[ii[i - 1]] + 1 == yy[ii[i]]) jj[ii[i - 1]][1] = ii[i], jj[ii[i]][3] = ii[i - 1]; for (i = 0; i < n; i++) tmp = xx[i], xx[i] = yy[i], yy[i] = tmp; memset(ds, -1, n * sizeof *ds); m = 0; for (i = 0; i < n; i++) { if ((j = jj[i][0]) != -1) { if ((xx[i] + yy[i]) % 2 == 0) { if (join(i, j)) uu[m] = i, vv[m] = j, aa[m] = xx[i] + xx[j] + 1, bb[m] = yy[i] + yy[j], m++; } else { if ((jj[i][3] == -1 || jj[j][3] == -1) && join(i, j)) uu[m] = i, vv[m] = j, aa[m] = xx[i] + xx[j] - 1, bb[m] = yy[i] + yy[j], m++; } } if ((j = jj[i][1]) != -1) { if ((xx[i] + yy[i]) % 2 == 0) { if (join(i, j)) uu[m] = i, vv[m] = j, aa[m] = xx[i] + xx[j], bb[m] = yy[i] + yy[j] - 1, m++; } else { if ((jj[i][0] == -1 || jj[j][0] == -1) && join(i, j)) uu[m] = i, vv[m] = j, aa[m] = xx[i] + xx[j], bb[m] = yy[i] + yy[j] + 1, m++; } } } for (i = 0; i < n; i++) if (find(i) != find(0)) return 0; build(uu, vv, aa, bb); return 1; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...