Submission #1037554

#TimeUsernameProblemLanguageResultExecution timeMemory
1037554juicyRoad Construction (JOI21_road_construction)C++17
100 / 100
2619 ms17380 KiB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 25e4 + 5;

int n, k;
int s[N], pos[N];
array<long long, 2> pt[N];
vector<long long> comp;

void upd(int i, int x) {
  for (; i <= comp.size(); i += i & -i) {
    s[i] += x;
  }
}

long long qry(int i) {
  int res = 0;
  for (; i; i -= i & -i) {
    res += s[i];
  }
  return res;
}

void clear() {
  fill(s + 1, s + comp.size() + 1, 0);
}

long long count(long long d) {
  clear();
  long long cnt = 0;
  for (int i = 1, j = 1; i <= n; ++i) {
    while (pt[i][0] - pt[j][0] > d) {
      upd(pos[j], -1);
      ++j;
    }
    int l = lower_bound(comp.begin(), comp.end(), pt[i][1] - d) - comp.begin();
    int r = upper_bound(comp.begin(), comp.end(), pt[i][1] + d) - comp.begin();
    cnt += qry(r) - qry(l);
    upd(pos[i], 1);
  }
  return cnt;
}

vector<long long> get(long long d) {
  vector<long long> res;
  set<array<long long, 2>> st;
  for (int i = 1, j = 1; i <= n; ++i) {
    while (pt[i][0] - pt[j][0] > d) {
      st.erase({pt[j][1], pt[j][0]});
      ++j;
    }
    for (auto it = st.lower_bound({pt[i][1] - d, pt[i][0] - d}); it != st.end() && (*it)[0] <= pt[i][1] + d; ++it) {
      res.push_back(max(abs(pt[i][1] - (*it)[0]), pt[i][0] - (*it)[1]));
    }
    st.insert({pt[i][1], pt[i][0]});
  }
  sort(res.begin(), res.end());
  return res;
}

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

  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    int x, y; cin >> x >> y;
    pt[i] = {x + y, x - y};
    comp.push_back(pt[i][1]);
  } 
  sort(comp.begin(), comp.end());
  comp.erase(unique(comp.begin(), comp.end()), comp.end());
  sort(pt + 1, pt + n + 1);
  for (int i = 1; i <= n; ++i) {
    pos[i] = lower_bound(comp.begin(), comp.end(), pt[i][1]) - comp.begin() + 1;
  }
  long long L = 1, R = 4e9, p = R;
  while (L <= R) {
    long long md = (L + R) / 2;
    if (count(md) >= k) {
      p = md;
      R = md - 1;
    } else {
      L = md + 1;
    }
  }
  auto res = get(p - 1);
  while (res.size() < k) {
    res.push_back(p);
  }
  for (auto x : res) {
    cout << x << "\n";
  }
  return 0;
}

Compilation message (stderr)

road_construction.cpp: In function 'void upd(int, int)':
road_construction.cpp:19:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |   for (; i <= comp.size(); i += i & -i) {
      |          ~~^~~~~~~~~~~~~~
road_construction.cpp: In function 'int main()':
road_construction.cpp:95:21: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   95 |   while (res.size() < k) {
      |          ~~~~~~~~~~~^~~
#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...