QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#132721#5463. Range Closest Pair of Points QueryClHg2WA 1025ms17252kbC++143.7kb2023-07-31 08:58:472023-07-31 08:58:48

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-31 08:58:48]
  • 评测
  • 测评结果:WA
  • 用时:1025ms
  • 内存:17252kb
  • [2023-07-31 08:58:47]
  • 提交

answer

#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <ext/pb_ds/assoc_container.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);
  } else {
    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: 0ms
memory: 11696kb

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: 0ms
memory: 13732kb

input:

2 1
1 1
1 1
1 2

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 0ms
memory: 11664kb

input:

2 1
1 100000000
100000000 1
1 2

output:

19999999600000002

result:

ok 1 number(s): "19999999600000002"

Test #4:

score: -100
Wrong Answer
time: 1025ms
memory: 17252kb

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:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

wrong answer 1st numbers differ - expected: '0', found: '1'