QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#132722 | #5463. Range Closest Pair of Points Query | ClHg2 | TL | 1243ms | 57844kb | C++14 | 3.7kb | 2023-07-31 09:00:31 | 2023-07-31 09:00:33 |
Judging History
answer
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
namespace {
using std::cin;
using std::cout;
using std::int64_t;
using std::size_t;
namespace base {
template <typename T, size_t... sizes>
struct NestedArray {};
template <typename T, size_t size, size_t... sizes>
struct NestedArray<T, size, sizes...> {
using Type = std::array<typename NestedArray<T, sizes...>::Type, size>;
};
template <typename T>
struct NestedArray<T> {
using Type = T;
};
template <typename T, size_t... sizes>
using Array = typename NestedArray<T, sizes...>::Type;
void OptimizeIO() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
}
void OptimizeIO(const std::string &input_file, const std::string &output_file) {
static std::ifstream input_stream(input_file);
static std::ofstream output_stream(output_file);
cin.rdbuf(input_stream.rdbuf()), cout.rdbuf(output_stream.rdbuf());
cin.tie(nullptr), cout.tie(nullptr);
}
} // namespace base
using base::Array;
const int kMaxN = 2.5e5 + 5, kMaxLogV = 28;
const int64_t kBase = 1.0e9, kInf = 1.0e18 + 5;
int n, m;
Array<int, kMaxLogV> p;
Array<int64_t, kMaxN> ans;
Array<__gnu_pbds::cc_hash_table<int64_t, std::vector<int>>, kMaxLogV> cell;
__gnu_pbds::cc_hash_table<int64_t, int> vis;
struct Point {
int x, y;
};
Array<Point, kMaxN> a;
struct Query {
int l, id;
Query(int l, int id) : l(l), id(id) {}
};
Array<std::vector<Query>, kMaxN> query;
class Decomposition {
public:
void Init(int n) { std::fill_n(a_.begin() + 1, n, kInf); }
int64_t Query(int l, int r) {
return *std::min_element(a_.begin() + l, a_.begin() + r + 1);
}
void Update(int x, int64_t val) { a_[x] = std::min(a_[x], val); }
private:
Array<int64_t, kMaxN> a_;
};
Decomposition decomposition;
inline int64_t Sqr(int x) { return int64_t{x} * x; }
inline int64_t Distance(const Point &a, const Point &b) {
return Sqr(a.x - b.x) + Sqr(a.y - b.y);
}
void Insert(int i) {
int &last = vis[kBase * a[i].x + a[i].y];
if (last) decomposition.Update(last, 0);
last = i;
for (int k = 0; k < kMaxLogV; ++k) {
int x = a[i].x >> (k + 1), y = a[i].y >> (k + 1);
auto it = cell[k].find(kBase * x + y);
if (it != cell[k].end()) {
int q = p[k];
for (int j : it->second) {
if (Distance(a[i], a[j]) < (int64_t{1} << (k << 1))) q = std::max(q, j);
}
for (int j = p[k] + 1; j <= q; ++j) {
int x = a[j].x >> (k + 1), y = a[j].y >> (k + 1);
auto &v = cell[k][x * kBase + y];
v.erase(std::find(v.begin(), v.end(), j));
}
p[k] = q;
}
for (int X = x - 1; X <= x + 1; ++X) {
for (int Y = y - 1; Y <= y + 1; ++Y) {
it = cell[k].find(kBase * X + Y);
if (it != cell[k].end()) {
for (int j : it->second) {
decomposition.Update(j, Distance(a[i], a[j]));
}
}
}
}
cell[k][kBase * x + y].emplace_back(i);
}
}
int Main() {
base::OptimizeIO();
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> a[i].x >> a[i].y;
for (int i = 1; i <= m; ++i) {
int l, r;
cin >> l >> r;
query[r].emplace_back(l, i);
}
decomposition.Init(n);
for (int i = 1; i <= n; ++i) {
Insert(i);
for (const Query &q : query[i]) ans[q.id] = decomposition.Query(q.l, i);
}
for (int i = 1; i <= m; ++i) cout << ans[i] << "\n";
return 0;
}
} // namespace
int main() { return Main(); }
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 13836kb
input:
5 5 2 4 1 1 3 3 5 1 4 2 1 5 2 3 2 4 3 5 1 3
output:
2 8 8 2 2
result:
ok 5 number(s): "2 8 8 2 2"
Test #2:
score: 0
Accepted
time: 1ms
memory: 13800kb
input:
2 1 1 1 1 1 1 2
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 2ms
memory: 13716kb
input:
2 1 1 100000000 100000000 1 1 2
output:
19999999600000002
result:
ok 1 number(s): "19999999600000002"
Test #4:
score: 0
Accepted
time: 1016ms
memory: 18484kb
input:
20000 250000 3 10 5 4 4 7 1 5 2 1 10 6 2 3 8 4 2 1 8 5 9 8 7 7 4 5 2 7 9 4 9 10 3 2 9 5 10 2 9 2 3 1 9 9 6 5 9 5 9 10 9 1 1 2 8 8 3 4 7 6 6 2 6 8 6 6 8 4 10 2 1 1 10 2 8 3 4 4 5 5 5 1 4 9 7 6 6 8 6 4 1 6 10 3 3 2 4 10 6 8 9 7 2 10 7 8 10 7 3 2 5 1 6 4 7 9 1 3 4 9 4 8 9 4 5 2 2 2 9 2 9 2 9 6 6 9 8 7 ...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 250000 numbers
Test #5:
score: 0
Accepted
time: 1046ms
memory: 17856kb
input:
20000 250000 72 45 72 34 34 10 20 96 79 39 43 5 72 49 56 85 1 72 44 70 73 89 69 76 49 89 57 38 39 9 33 47 22 3 96 41 90 82 25 6 72 92 73 38 53 21 16 88 59 9 54 2 14 6 7 94 99 68 27 82 70 50 81 81 60 81 2 98 33 19 98 9 35 36 49 66 86 7 3 95 32 89 62 42 68 88 65 16 94 6 85 10 51 69 90 36 70 87 13 79 4...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
result:
ok 250000 numbers
Test #6:
score: 0
Accepted
time: 1068ms
memory: 24448kb
input:
20000 250000 116 165 150 677 951 676 552 324 267 222 739 755 705 663 235 142 152 714 735 641 414 201 313 663 683 300 403 739 37 521 42 833 553 733 886 449 86 913 55 637 731 932 143 161 500 891 719 79 916 237 431 992 804 210 643 332 165 362 178 332 821 510 762 34 188 83 283 357 743 407 750 487 19 658...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 45 1 0 1 0 0 0 2 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 52 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 5 0 0 0 0 0 1 0 0 0 1 1 0 0 0 2 0 0 0 0 0 0 ...
result:
ok 250000 numbers
Test #7:
score: 0
Accepted
time: 1195ms
memory: 43956kb
input:
20000 250000 193144 901630 894860 3728 802840 155641 625273 792794 433205 942335 223506 874548 425235 550629 341169 950916 49296 305542 709463 512381 9742 994078 106664 845553 38691 373010 184728 331946 344567 438182 854583 291040 94405 555036 56330 494420 928479 123077 796593 798314 300374 490072 2...
output:
3576676 1219565 93925 2336788 8872 68 137 842657 137 8560936 13914025 28521 88362 8872 8872 52996 12869 86297 68 8137625 93925 12869 86297 8872 8872 8872 47888 8872 12869 107860 12869 59536 59536 425540 12869 59536 8872 93925 117225 137 137 52996 68 52996 137 8872 68 12869 137 68 86297 1514 159700 6...
result:
ok 250000 numbers
Test #8:
score: 0
Accepted
time: 1243ms
memory: 57844kb
input:
20000 250000 65468917 46637638 46041830 58072288 95596278 49154795 57837493 40861050 21328886 69542502 7729300 7126264 2317013 48080367 75669670 20165373 93996778 88884044 8523082 62327896 123901 11925128 71901024 73104267 94991893 98591670 53591520 11543761 76785613 86286274 64694742 89591932 75687...
output:
185588005 3282196826 141969393 25769005 141969393 185588005 576346153849 141969393 141969393 185588005 141969393 141969393 141969393 141969393 135584833 141969393 141969393 485404589 1182793 1182793 35224007589 135584833 185588005 931246420 185588005 25769005 375240717 141969393 2245310308 239709665...
result:
ok 250000 numbers
Test #9:
score: -100
Time Limit Exceeded
input:
250000 250000 1 2 1 1 3 2 3 3 1 2 3 2 1 2 1 3 3 1 2 1 1 3 2 3 3 3 1 3 3 1 3 1 1 3 2 1 1 2 1 1 1 2 2 2 1 2 1 2 1 3 3 3 1 1 3 2 1 2 2 2 1 1 2 3 3 2 2 1 3 3 2 1 2 3 3 1 2 3 3 2 2 1 1 1 3 2 1 2 1 3 3 1 2 3 2 1 1 2 1 1 1 2 3 3 1 2 2 2 3 1 3 1 3 1 1 2 2 2 1 1 3 3 1 3 1 3 1 2 1 2 3 1 3 2 1 2 2 2 1 2 1 1 2 ...