Submission #1229070

#TimeUsernameProblemLanguageResultExecution timeMemory
1229070rxlfd314Fountain Parks (IOI21_parks)C++20
30 / 100
514 ms39240 KiB
#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ari2 = array<int, 2>;
using ari3 = array<int, 3>;
using arl2 = array<ll, 2>;
using arl3 = array<ll, 3>;
template <class T> using vt = vector<T>;
#define all(x) begin(x), end(x)
#define size(x) (int((x).size()))
#define REP(a,b,c,d) for(int a=(b);(d)>0?a<=(c):a>=(c);a+=(d))
#define FOR(a,b,c) REP(a,b,c,1)
#define ROF(a,b,c) REP(a,b,c,-1)

struct DSU {
  vt<int> uf;
  DSU(const int n) : uf(n, -1) {}
  int find(const int i) {
    return uf[i] < 0 ? i : uf[i] = find(uf[i]);
  }
  bool unite(int a, int b) {
    if ((a = find(a)) == (b = find(b)))
      return false;
    if (uf[a] > uf[b])
      swap(a, b);
    uf[a] += uf[b];
    uf[b] = a;
    return true;
  }
};

constexpr int dir[5] = {2, 0, -2, 0, 2};

int construct_roads(vt<int> X, vt<int> Y) {
  const int N = size(X);
  map<ari2, int> points;
  vt<ari2> edges;
  FOR(i, 0, N-1) {
    FOR(j, 0, 3) {
      const int x = X[i] + dir[j];
      const int y = Y[i] + dir[j+1];
      if (points.find({x, y}) != points.end()) {
        const int k = points[{x, y}];
        edges.push_back({i, k});
      }
    }
    points[{X[i], Y[i]}] = i;
  }
  if (*max_element(all(X)) <= 6) {
    DSU uf(N);
    vt<int> A, B, C, D;
    vt<ari3> fours;
    for (const auto &[i, j] : edges) {
      if (X[i] == X[j]) {
        uf.unite(i, j);
        if (X[i] == 2) {
          A.push_back(i);
          B.push_back(j);
          C.push_back(1);
          D.push_back(Y[i] + Y[j] >> 1);
        } else if (X[i] == 6) {
          A.push_back(i);
          B.push_back(j);
          C.push_back(7);
          D.push_back(Y[i] + Y[j] >> 1);
        } else {
          fours.push_back({Y[i] + Y[j] >> 1, i, j});
        }
      }
    }
    sort(all(fours));
    int add = 1;
    set<ari2> have;
    for (const auto &[_, i, j] : fours) {
      A.push_back(i);
      B.push_back(j);
      C.push_back(4 + add);
      D.push_back(Y[i] + Y[j] >> 1);
      have.insert({4 + add, Y[i] + Y[j] >> 1});
      add *= -1;
    }
    for (const auto &[i, j] : edges) {
      if (Y[i] == Y[j]) {
        if (!uf.unite(i, j))
          continue;
        A.push_back(i);
        B.push_back(j);
        const int x = X[i] + X[j] >> 1;
        C.push_back(x);
        if (have.count({x, Y[i] + 1})) {
          assert(!have.count({x, Y[i] - 1}));
          have.insert({x, Y[i] - 1});
          D.push_back(Y[i] - 1);
        } else {
          have.insert({x, Y[i] + 1});
          D.push_back(Y[i] + 1);
        }
      }
    }
    assert(size(A) == size(B) && size(B) == size(C) && size(C) == size(D));
    if (size(A) != N - 1)
      return 0;
    build(A, B, C, D);
    return 1;
  }
  DSU uf(N);
  vt<array<int, 4>> hori;
  for (const auto &[i, j] : edges) {
    if (Y[i] != Y[j] || !uf.unite(i, j))
      continue;
    hori.push_back({Y[i], X[i] + X[j] >> 1, i, j});
  }
  vt<int> A, B, C, D;
  sort(all(hori));
  int add = 1;
  set<ari2> have;
  for (const auto &[y, x, i, j] : hori) {
    assert(!have.count({x, y + add}));
    have.insert({x, y + add});
    A.push_back(i);
    B.push_back(j);
    D.push_back(y + add);
    C.push_back(x);
    add *= -1;
  }
  for (const auto &[i, j] : edges) {
    if (X[i] != X[j] || !uf.unite(i, j))
      continue;
    A.push_back(i);
    B.push_back(j);
    const int y = Y[i] + Y[j] >> 1;
    D.push_back(y);
    if (have.count({X[i] - 1, y})) {
      assert(!have.count({X[i] + 1, y}));
      have.insert({X[i] + 1, y});
      C.push_back(X[i] + 1);
    } else {
      have.insert({X[i] - 1, y});
      C.push_back(X[i] - 1);
    }
  }
  assert(size(A) == size(B) && size(B) == size(C) && size(C) == size(D));
  if (size(A) != N - 1)
    return 0;
  build(A, B, C, D);
  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...