Submission #1228902

#TimeUsernameProblemLanguageResultExecution timeMemory
1228902rxlfd314Fountain Parks (IOI21_parks)C++20
30 / 100
446 ms36928 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);
  if (*max_element(all(X)) <= 6) {
    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;
    }
    #ifdef DEBUG
    cout << "wtf: " << size(edges) << '\n';
    #endif
    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});
      #ifdef DEBUG
      cout << "bench for " << X[i] << ' ' << Y[i] << ' ' << X[j] << ' ' << Y[j] << " at " << 4 + add << ' ' << (Y[i] + Y[j] >> 1) << '\n';
      #endif
      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);
          #ifdef DEBUG
          cout << "bench for " << X[i] << ' ' << Y[i] << ' ' << X[j] << ' ' << Y[j] << " at " << x << ' ' << Y[i] - 1 << '\n';
          #endif
        } else {
          have.insert({x, Y[i] + 1});
          D.push_back(Y[i] + 1);
          #ifdef DEBUG
          cout << "bench for " << X[i] << ' ' << Y[i] << ' ' << X[j] << ' ' << Y[j] << " at " << x << ' ' << Y[i] + 1 << '\n';
          #endif
        }
      }
    }
    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;
  }
}

Compilation message (stderr)

parks.cpp: In function 'int construct_roads(vt<int>, vt<int>)':
parks.cpp:119:1: warning: control reaches end of non-void function [-Wreturn-type]
  119 | }
      | ^
#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...