結果

問題 No.1332 Range Nearest Query
ユーザー KoDKoD
提出日時 2021-01-08 21:29:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 409 ms / 2,500 ms
コード長 3,128 bytes
コンパイル時間 847 ms
コンパイル使用メモリ 87,700 KB
実行使用メモリ 55,944 KB
最終ジャッジ日時 2023-08-10 08:34:38
合計ジャッジ時間 19,937 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 359 ms
48,924 KB
testcase_04 AC 355 ms
49,084 KB
testcase_05 AC 371 ms
49,036 KB
testcase_06 AC 191 ms
55,832 KB
testcase_07 AC 196 ms
55,856 KB
testcase_08 AC 195 ms
55,700 KB
testcase_09 AC 193 ms
55,944 KB
testcase_10 AC 190 ms
55,792 KB
testcase_11 AC 190 ms
55,672 KB
testcase_12 AC 193 ms
55,792 KB
testcase_13 AC 193 ms
55,860 KB
testcase_14 AC 191 ms
55,692 KB
testcase_15 AC 189 ms
55,688 KB
testcase_16 AC 387 ms
55,852 KB
testcase_17 AC 393 ms
55,808 KB
testcase_18 AC 389 ms
55,668 KB
testcase_19 AC 398 ms
55,656 KB
testcase_20 AC 397 ms
55,840 KB
testcase_21 AC 404 ms
55,696 KB
testcase_22 AC 409 ms
55,736 KB
testcase_23 AC 406 ms
55,868 KB
testcase_24 AC 403 ms
55,652 KB
testcase_25 AC 396 ms
55,672 KB
testcase_26 AC 163 ms
55,704 KB
testcase_27 AC 150 ms
55,748 KB
testcase_28 AC 24 ms
4,380 KB
testcase_29 AC 25 ms
4,380 KB
testcase_30 AC 25 ms
4,376 KB
testcase_31 AC 22 ms
4,376 KB
testcase_32 AC 26 ms
4,376 KB
testcase_33 AC 27 ms
4,376 KB
testcase_34 AC 23 ms
4,380 KB
testcase_35 AC 23 ms
4,376 KB
testcase_36 AC 23 ms
4,376 KB
testcase_37 AC 24 ms
4,376 KB
testcase_38 AC 272 ms
29,392 KB
testcase_39 AC 104 ms
4,812 KB
testcase_40 AC 385 ms
54,268 KB
testcase_41 AC 175 ms
12,224 KB
testcase_42 AC 270 ms
27,984 KB
testcase_43 AC 222 ms
18,500 KB
testcase_44 AC 345 ms
42,096 KB
testcase_45 AC 331 ms
37,968 KB
testcase_46 AC 269 ms
25,388 KB
testcase_47 AC 321 ms
33,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "main.cpp"

/**
 * @title Template
 */

#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
#include <cassert>

#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"

#line 4 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"

class range {
  struct iter {
    std::size_t itr;
    constexpr iter(std::size_t pos) noexcept: itr(pos) { }
    constexpr void operator ++ () noexcept { ++itr; }
    constexpr bool operator != (iter other) const noexcept { return itr != other.itr; }
    constexpr std::size_t operator * () const noexcept { return itr; }
  };

  struct reviter {
    std::size_t itr;
    constexpr reviter(std::size_t pos) noexcept: itr(pos) { }
    constexpr void operator ++ () noexcept { --itr; }
    constexpr bool operator != (reviter other) const noexcept { return itr != other.itr; }
    constexpr std::size_t operator * () const noexcept { return itr; }
  };

  const iter first, last;

public:
  constexpr range(std::size_t first, std::size_t last) noexcept: first(first), last(std::max(first, last)) { }
  constexpr iter begin() const noexcept { return first; }
  constexpr iter end() const noexcept { return last; }
  constexpr reviter rbegin() const noexcept { return reviter(*last - 1); } 
  constexpr reviter rend() const noexcept { return reviter(*first - 1); } 
};

/**
 * @title Range
 */
#line 15 "main.cpp"

using i32 = std::int32_t;
using i64 = std::int64_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

constexpr i32 inf32 = (u32) ~0 >> 2;
constexpr i64 inf64 = (u64) ~0 >> 2;

template <class T>
using Vec = std::vector<T>;

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  usize N;
  std::cin >> N;
  Vec<u32> X(N);
  for (auto &x: X) {
    std::cin >> x;
  }
  Vec<Vec<u32>> seg(2 * N);
  for (auto i: range(0, N)) {
    seg[N + i].push_back(X[i]);
  }
  for (usize i = N; --i > 0;) {
    seg[i].reserve(seg[i * 2].size() + seg[i * 2 + 1].size());
    std::copy(seg[i * 2].begin(), seg[i * 2].end(), std::back_inserter(seg[i]));
    std::copy(seg[i * 2 + 1].begin(), seg[i * 2 + 1].end(), std::back_inserter(seg[i]));
    std::inplace_merge(seg[i].begin(), seg[i].begin() + seg[i * 2].size(), seg[i].end());
  }
  usize Q;
  std::cin >> Q;
  const auto calc = [&](const std::vector<u32> &vec, const u32 val) {
    const auto itr = std::lower_bound(vec.cbegin(), vec.cend(), val);
    if (itr == vec.end()) {
      return val - *std::prev(itr);
    }
    if (itr == vec.begin()) {
      return *itr - val;
    }
    return std::min(val - *std::prev(itr), *itr - val);
  };
  while (Q--) {
    usize l, r;
    u32 x;
    std::cin >> l >> r >> x;
    l -= 1;
    l += N;
    r += N;
    u32 ans = inf32;
    while (l < r) {
      if (l & 1) {
        ans = std::min(ans, calc(seg[l], x));
        l += 1;
      }
      l >>= 1;
      if (r & 1) {
        r -= 1;
        ans = std::min(ans, calc(seg[r], x));
      }
      r >>= 1;
    } 
    std::cout << ans << '\n';
  }
  return 0;
}
0