Submission #387571

# Submission time Handle Problem Language Result Execution time Memory
387571 2021-04-09T02:11:53 Z rama_pang Road Construction (JOI21_road_construction) C++17
100 / 100
7742 ms 19464 KB
#include <bits/stdc++.h>
using namespace std;

using lint = long long;
const lint inf = 1e18;

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

  Fenwick(int sz) : sz(sz), tree(sz, 0) {}

  void Update(int p, int x) {
    for (int i = p; i < sz; i |= i + 1) {
      tree[i] += x;
    }
  }

  int Query(int p) {
    int res = 0;
    for (int i = p; i > 0; i &= i - 1) {
      res += tree[i - 1];
    }
    return res;
  }
};

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

  int N, K;
  cin >> N >> K;

  vector<lint> X(N), Y(N);
  vector<lint> coordX = {-inf, inf}, coordY = {-inf, inf};

  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i];
    tie(X[i], Y[i]) = make_pair(X[i] + Y[i], X[i] - Y[i]);
    coordX.emplace_back(X[i]);
    coordY.emplace_back(Y[i]);
  }

  sort(begin(coordX), end(coordX));
  sort(begin(coordY), end(coordY));
  coordX.resize(unique(begin(coordX), end(coordX)) - begin(coordX));
  coordY.resize(unique(begin(coordY), end(coordY)) - begin(coordY));

  vector<int> ord(N);
  iota(begin(ord), end(ord), 0);
  sort(begin(ord), end(ord), [&](int i, int j) { return X[i] < X[j]; });

  const auto Count = [&](lint dist) {
    Fenwick F(coordY.size());
    const auto Modify = [&](int i, int x) {
      int yid = lower_bound(begin(coordY), end(coordY), Y[i]) - begin(coordY);
      F.Update(yid, x);
    };

    lint res = 0;
    for (int i = 0, j = 0; i < N; i++) {
      while (abs(X[ord[i]] - X[ord[j]]) > dist) Modify(ord[j++], -1);
      int ylo = lower_bound(begin(coordY), end(coordY), Y[ord[i]] - dist) - begin(coordY);
      int yhi = upper_bound(begin(coordY), end(coordY), Y[ord[i]] + dist) - begin(coordY);
      res += F.Query(yhi) - F.Query(ylo);
      Modify(ord[i], 1);
    }

    return res;
  };

  const auto Generate = [&](lint dist) -> vector<lint> {
    set<pair<lint, int>> F;
    const auto Modify = [&](int i, int x) {
      if (x == 1) {
        F.emplace(Y[i], i);
      } else if (x == -1) {
        F.erase({Y[i], i});
      }
    };

    vector<pair<int, int>> pairs;
    for (int i = 0, j = 0; i < N; i++) {
      while (abs(X[ord[i]] - X[ord[j]]) > dist) Modify(ord[j++], -1);
      int ylo = lower_bound(begin(coordY), end(coordY), Y[ord[i]] - dist) - begin(coordY);
      int yhi = upper_bound(begin(coordY), end(coordY), Y[ord[i]] + dist) - begin(coordY);
      auto it = F.lower_bound({Y[ord[i]] - dist, -1});
      while (it != end(F) && abs(it->first - Y[ord[i]]) <= dist) {
        pairs.emplace_back(ord[i], it->second);
        it++;
      }
      Modify(ord[i], 1);
    }

    vector<lint> res;
    for (auto [i, j] : pairs) {
      if (i != j) {
        res.emplace_back(max(abs(X[i] - X[j]), abs(Y[i] - Y[j])));
      }
    }

    return res;
  };

  lint lo = 0, hi = 1e10;
  while (lo < hi) {
    lint md = (lo + hi) / 2;
    if (Count(md) >= K) {
      hi = md;
    } else {
      lo = md + 1;
    }
  }

  vector<lint> ans = Generate(lo - 1);
  while (ans.size() < K) {
    ans.emplace_back(lo);
  }

  sort(begin(ans), end(ans));
  assert(ans.size() == K);
  for (int i = 0; i < K; i++) {
    cout << ans[i] << '\n';
  }
  return 0;
}

Compilation message

road_construction.cpp: In lambda function:
road_construction.cpp:87:11: warning: unused variable 'ylo' [-Wunused-variable]
   87 |       int ylo = lower_bound(begin(coordY), end(coordY), Y[ord[i]] - dist) - begin(coordY);
      |           ^~~
road_construction.cpp:88:11: warning: unused variable 'yhi' [-Wunused-variable]
   88 |       int yhi = upper_bound(begin(coordY), end(coordY), Y[ord[i]] + dist) - begin(coordY);
      |           ^~~
road_construction.cpp: In function 'int main()':
road_construction.cpp:118:21: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  118 |   while (ans.size() < K) {
      |          ~~~~~~~~~~~^~~
In file included from /usr/include/c++/9/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:33,
                 from road_construction.cpp:1:
road_construction.cpp:123:21: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  123 |   assert(ans.size() == K);
      |          ~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 62 ms 5436 KB Output is correct
2 Correct 68 ms 5336 KB Output is correct
3 Correct 56 ms 5452 KB Output is correct
4 Correct 56 ms 5348 KB Output is correct
5 Correct 57 ms 5308 KB Output is correct
6 Correct 5 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3075 ms 14124 KB Output is correct
2 Correct 2959 ms 17108 KB Output is correct
3 Correct 52 ms 5452 KB Output is correct
4 Correct 2982 ms 17192 KB Output is correct
5 Correct 2916 ms 17032 KB Output is correct
6 Correct 2898 ms 17156 KB Output is correct
7 Correct 2902 ms 17232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7528 ms 10108 KB Output is correct
2 Correct 7742 ms 15204 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 2953 ms 13108 KB Output is correct
5 Correct 3078 ms 14468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7528 ms 10108 KB Output is correct
2 Correct 7742 ms 15204 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 2953 ms 13108 KB Output is correct
5 Correct 3078 ms 14468 KB Output is correct
6 Correct 6962 ms 15120 KB Output is correct
7 Correct 6831 ms 15196 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 6615 ms 15124 KB Output is correct
11 Correct 3024 ms 13052 KB Output is correct
12 Correct 3045 ms 14476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 5436 KB Output is correct
2 Correct 68 ms 5336 KB Output is correct
3 Correct 56 ms 5452 KB Output is correct
4 Correct 56 ms 5348 KB Output is correct
5 Correct 57 ms 5308 KB Output is correct
6 Correct 5 ms 332 KB Output is correct
7 Correct 2549 ms 8868 KB Output is correct
8 Correct 2555 ms 10880 KB Output is correct
9 Correct 56 ms 5456 KB Output is correct
10 Correct 2236 ms 10852 KB Output is correct
11 Correct 2065 ms 10948 KB Output is correct
12 Correct 964 ms 10088 KB Output is correct
13 Correct 1086 ms 11032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 5436 KB Output is correct
2 Correct 68 ms 5336 KB Output is correct
3 Correct 56 ms 5452 KB Output is correct
4 Correct 56 ms 5348 KB Output is correct
5 Correct 57 ms 5308 KB Output is correct
6 Correct 5 ms 332 KB Output is correct
7 Correct 3075 ms 14124 KB Output is correct
8 Correct 2959 ms 17108 KB Output is correct
9 Correct 52 ms 5452 KB Output is correct
10 Correct 2982 ms 17192 KB Output is correct
11 Correct 2916 ms 17032 KB Output is correct
12 Correct 2898 ms 17156 KB Output is correct
13 Correct 2902 ms 17232 KB Output is correct
14 Correct 7528 ms 10108 KB Output is correct
15 Correct 7742 ms 15204 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 2953 ms 13108 KB Output is correct
18 Correct 3078 ms 14468 KB Output is correct
19 Correct 6962 ms 15120 KB Output is correct
20 Correct 6831 ms 15196 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 6615 ms 15124 KB Output is correct
24 Correct 3024 ms 13052 KB Output is correct
25 Correct 3045 ms 14476 KB Output is correct
26 Correct 2549 ms 8868 KB Output is correct
27 Correct 2555 ms 10880 KB Output is correct
28 Correct 56 ms 5456 KB Output is correct
29 Correct 2236 ms 10852 KB Output is correct
30 Correct 2065 ms 10948 KB Output is correct
31 Correct 964 ms 10088 KB Output is correct
32 Correct 1086 ms 11032 KB Output is correct
33 Correct 7387 ms 19332 KB Output is correct
34 Correct 7378 ms 19220 KB Output is correct
35 Correct 6553 ms 19224 KB Output is correct
36 Correct 3195 ms 18400 KB Output is correct
37 Correct 3358 ms 18328 KB Output is correct
38 Correct 3259 ms 19464 KB Output is correct