답안 #283853

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
283853 2020-08-26T08:10:05 Z rama_pang Golf (JOI17_golf) C++14
30 / 100
10000 ms 67460 KB
#include <bits/stdc++.h>
using namespace std;

class Boundary {
 public:
  int sz;
  vector<vector<int>> tree;

  Boundary() {}
  Boundary(int sz) : sz(sz), tree(2 * sz) {}

  void AddWall(int l, int r, int v) {
    for (l += sz, r += sz + 1; l < r; l /= 2, r /= 2) {
      if (l & 1) tree[l++].emplace_back(v);
      if (r & 1) tree[--r].emplace_back(v);
    }
  }

  void Build() {
    for (int i = 0; i < 2 * sz; i++) {
      sort(begin(tree[i]), end(tree[i]));
    }
  }

  int FindMax(int p, int x) { // Find maximum <= x
    int res = 0;
    for (p += sz; p > 0; p /= 2) {
      if (!tree[p].empty()) {
        auto it = upper_bound(begin(tree[p]), end(tree[p]), x);
        if (it != begin(tree[p])) {
          res = max(res, *prev(it));
        }
      }
    }
    return res;
  }

  int FindMin(int p, int x) { // Find minimum >= x
    int res = sz;
    for (p += sz; p > 0; p /= 2) {
      if (!tree[p].empty()) {
        auto it = lower_bound(begin(tree[p]), end(tree[p]), x);
        if (it != end(tree[p])) {
          res = min(res, *it);
        }        
      }
    }
    return res;
  }
};

void Read(int &S, int &T, int &U, int &V, int &N, 
          vector<int> &A, vector<int> &B, vector<int> &C, vector<int> &D) {

  cin >> S >> T >> U >> V >> N;
  A.resize(N), B.resize(N), C.resize(N), D.resize(N);

  vector<int> xcoord = {S, U};
  vector<int> ycoord = {T, V};
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i] >> C[i] >> D[i];
    xcoord.emplace_back(A[i]);
    xcoord.emplace_back(B[i]);
    ycoord.emplace_back(C[i]);
    ycoord.emplace_back(D[i]);
  }

  sort(begin(xcoord), end(xcoord));
  sort(begin(ycoord), end(ycoord));
  xcoord.resize(unique(begin(xcoord), end(xcoord)) - begin(xcoord));
  ycoord.resize(unique(begin(ycoord), end(ycoord)) - begin(ycoord));

  const auto GetPosition = [&](const vector<int> &a, int x) {
    return lower_bound(begin(a), end(a), x) - begin(a);
  };

  S = GetPosition(xcoord, S);
  T = GetPosition(ycoord, T);
  U = GetPosition(xcoord, U);
  V = GetPosition(ycoord, V);

  for (int i = 0; i < N; i++) {
    A[i] = GetPosition(xcoord, A[i]);
    B[i] = GetPosition(xcoord, B[i]);
    C[i] = GetPosition(ycoord, C[i]);
    D[i] = GetPosition(ycoord, D[i]);
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int S, T, U, V, N;
  vector<int> A, B, C, D;
  Read(S, T, U, V, N, A, B, C, D);

  vector<Boundary> boundary(2, Boundary(2 * N + 2));
  vector<array<int, 5>> lines; // (type 0 = vertical, 1 = horizontal)
  for (int i = 0; i < N; i++) {
    boundary[0].AddWall(C[i] + 1, D[i] - 1, A[i]);
    boundary[0].AddWall(C[i] + 1, D[i] - 1, B[i]);
    boundary[1].AddWall(A[i] + 1, B[i] - 1, C[i]);
    boundary[1].AddWall(A[i] + 1, B[i] - 1, D[i]);
    
    lines.push_back({A[i], C[i], D[i], 0, i * 4 + 0});
    lines.push_back({B[i], C[i], D[i], 0, i * 4 + 1});
    lines.push_back({C[i], A[i], B[i], 1, i * 4 + 2});
    lines.push_back({D[i], A[i], B[i], 1, i * 4 + 3});
  }
  lines.push_back({S, T, T, 0, N * 4 + 0});
  lines.push_back({T, S, S, 1, N * 4 + 0});
  lines.push_back({U, V, V, 0, N * 4 + 1});
  lines.push_back({V, U, U, 1, N * 4 + 1});

  vector<vector<int>> adj(4 * N + 6);
  auto AddEdge = [&](int u, int v) {
    adj[u].emplace_back(v);
  };

  boundary[0].Build();
  boundary[1].Build();
  for (auto &l : lines) {
    l[1] = boundary[l[3] ^ 1].FindMax(l[0], l[1] - 1);
    l[2] = boundary[l[3] ^ 1].FindMin(l[0], l[2] + 1);
  }

  for (int i = 0; i < (int) lines.size(); i++) {
    for (int j = i + 1; j < (int) lines.size(); j++) if (i != j) {
      if (lines[i][3] != lines[j][3]) {
        bool c1 = lines[i][1] <= lines[j][0] && lines[j][0] <= lines[i][2];
        bool c2 = lines[j][1] <= lines[i][0] && lines[i][0] <= lines[j][2];
        if (c1 && c2) {
          AddEdge(i, j);
          AddEdge(j, i);
        }
      }
    }
  }

  const int SRC = N * 4 + 4;
  const int TGT = N * 4 + 5;

  for (int i = 0; i < (int) lines.size(); i++) {
    if (lines[i][3] == 0) { // vertical
      if (lines[i][0] == S && lines[i][1] <= T && T <= lines[i][2]) {
        AddEdge(SRC, i);
      }
      if (lines[i][0] == U && lines[i][1] <= V && V <= lines[i][2]) {
        AddEdge(i, TGT);
      }
    } else { // horizontal
      if (lines[i][0] == T && lines[i][1] <= S && S <= lines[i][2]) {
        AddEdge(SRC, i);
      }
      if (lines[i][0] == V && lines[i][1] <= U && U <= lines[i][2]) {
        AddEdge(i, TGT);
      }
    }
  }

  queue<int> q;
  vector<int> dist(4 * N + 6, -1);
  q.emplace(SRC);
  dist[SRC] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto v : adj[u]) {
      if (dist[v] == -1) {
        dist[v] = dist[u] + 1;
        q.emplace(v);
      }
    }
  }
  cout << dist[TGT] - 1 << "\n";
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 48 ms 1400 KB Output is correct
6 Correct 45 ms 1400 KB Output is correct
7 Correct 42 ms 1400 KB Output is correct
8 Correct 42 ms 1400 KB Output is correct
9 Correct 42 ms 1404 KB Output is correct
10 Correct 43 ms 1400 KB Output is correct
11 Correct 44 ms 1400 KB Output is correct
12 Correct 42 ms 1400 KB Output is correct
13 Correct 42 ms 1404 KB Output is correct
14 Correct 47 ms 1528 KB Output is correct
15 Correct 6 ms 640 KB Output is correct
16 Correct 19 ms 1044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 48 ms 1400 KB Output is correct
6 Correct 45 ms 1400 KB Output is correct
7 Correct 42 ms 1400 KB Output is correct
8 Correct 42 ms 1400 KB Output is correct
9 Correct 42 ms 1404 KB Output is correct
10 Correct 43 ms 1400 KB Output is correct
11 Correct 44 ms 1400 KB Output is correct
12 Correct 42 ms 1400 KB Output is correct
13 Correct 42 ms 1404 KB Output is correct
14 Correct 47 ms 1528 KB Output is correct
15 Correct 6 ms 640 KB Output is correct
16 Correct 19 ms 1044 KB Output is correct
17 Correct 43 ms 1364 KB Output is correct
18 Correct 41 ms 1400 KB Output is correct
19 Correct 42 ms 1400 KB Output is correct
20 Correct 43 ms 1400 KB Output is correct
21 Correct 43 ms 1400 KB Output is correct
22 Correct 45 ms 1400 KB Output is correct
23 Correct 42 ms 1404 KB Output is correct
24 Correct 44 ms 1408 KB Output is correct
25 Correct 43 ms 1404 KB Output is correct
26 Correct 54 ms 1400 KB Output is correct
27 Correct 9 ms 768 KB Output is correct
28 Correct 22 ms 1024 KB Output is correct
29 Correct 23 ms 1056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 48 ms 1400 KB Output is correct
6 Correct 45 ms 1400 KB Output is correct
7 Correct 42 ms 1400 KB Output is correct
8 Correct 42 ms 1400 KB Output is correct
9 Correct 42 ms 1404 KB Output is correct
10 Correct 43 ms 1400 KB Output is correct
11 Correct 44 ms 1400 KB Output is correct
12 Correct 42 ms 1400 KB Output is correct
13 Correct 42 ms 1404 KB Output is correct
14 Correct 47 ms 1528 KB Output is correct
15 Correct 6 ms 640 KB Output is correct
16 Correct 19 ms 1044 KB Output is correct
17 Correct 43 ms 1364 KB Output is correct
18 Correct 41 ms 1400 KB Output is correct
19 Correct 42 ms 1400 KB Output is correct
20 Correct 43 ms 1400 KB Output is correct
21 Correct 43 ms 1400 KB Output is correct
22 Correct 45 ms 1400 KB Output is correct
23 Correct 42 ms 1404 KB Output is correct
24 Correct 44 ms 1408 KB Output is correct
25 Correct 43 ms 1404 KB Output is correct
26 Correct 54 ms 1400 KB Output is correct
27 Correct 9 ms 768 KB Output is correct
28 Correct 22 ms 1024 KB Output is correct
29 Correct 23 ms 1056 KB Output is correct
30 Execution timed out 10077 ms 67460 KB Time limit exceeded
31 Halted 0 ms 0 KB -