結果
| 問題 |
No.2897 2集合間距離
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2024-09-20 21:56:02 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 168 ms / 3,500 ms |
| コード長 | 3,292 bytes |
| コンパイル時間 | 3,288 ms |
| コンパイル使用メモリ | 257,208 KB |
| 実行使用メモリ | 37,760 KB |
| 最終ジャッジ日時 | 2024-09-20 21:56:08 |
| 合計ジャッジ時間 | 6,335 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 24 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
IOSetup() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << fixed << setprecision(20);
}
} iosetup;
template <typename T>
struct CumulativeSum2D {
explicit CumulativeSum2D(const int h, const int w)
: CumulativeSum2D(std::vector<std::vector<T>>(h, std::vector<T>(w, 0))) {}
template <typename U>
explicit CumulativeSum2D(const std::vector<std::vector<U>>& a)
: is_built(false), h(a.size()), w(a.front().size()),
data(h + 1, std::vector<T>(w + 1, 0)) {
for (int i = 0; i < h; ++i) {
std::copy(a[i].begin(), a[i].end(), std::next(data[i + 1].begin()));
}
}
void add(const int y, const int x, const T val) {
assert(!is_built);
data[y + 1][x + 1] += val;
}
void build() {
assert(!is_built);
is_built = true;
for (int i = 0; i < h; ++i) {
std::partial_sum(data[i + 1].begin(), data[i + 1].end(),
data[i + 1].begin());
}
for (int j = 1; j <= w; ++j) {
for (int i = 1; i < h; ++i) {
data[i + 1][j] += data[i][j];
}
}
}
T query(const int y1, const int x1, const int y2, const int x2) const {
assert(is_built);
return y1 > y2 || x1 > x2 ? 0 : data[y2 + 1][x2 + 1] - data[y2 + 1][x1]
- data[y1][x2 + 1] + data[y1][x1];
}
private:
bool is_built;
const int h, w;
std::vector<std::vector<T>> data;
};
int main() {
int n; cin >> n;
vector<int> x(n), y(n);
REP(i, n) {
int x_i, y_i; cin >> x_i >> y_i;
x[i] = x_i - y_i;
y[i] = x_i + y_i;
}
int m; cin >> m;
vector<int> z(m), w(m);
REP(i, m) {
int z_i, w_i; cin >> z_i >> w_i;
z[i] = z_i - w_i;
w[i] = z_i + w_i;
}
{
const int x_min = min(ranges::min(x), ranges::min(z));
REP(i, n) x[i] -= x_min;
REP(i, m) z[i] -= x_min;
}
{
const int y_min = min(ranges::min(y), ranges::min(w));
REP(i, n) y[i] -= y_min;
REP(i, m) w[i] -= y_min;
}
const int xr = max(ranges::max(x), ranges::max(z));
const int yr = max(ranges::max(y), ranges::max(w));
CumulativeSum2D<int> cs(xr + 1, yr + 1);
REP(i, n) cs.add(x[i], y[i], 1);
cs.build();
int lb = -1, ub = abs(x.front() - z.front()) + abs(y.front() - w.front());
while (ub - lb > 1) {
const int l = midpoint(lb, ub);
bool exists = false;
REP(i, m) {
const int num = cs.query(max(z[i] - l, 0), max(w[i] - l, 0), min(z[i] + l, xr), min(w[i] + l, yr));
if (num > 0) {
exists = true;
break;
}
}
(exists ? ub : lb) = l;
}
cout << ub << '\n';
return 0;
}
emthrm