結果

問題 No.1332 Range Nearest Query
ユーザー 👑 hitonanodehitonanode
提出日時 2021-01-08 22:32:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,541 ms / 2,500 ms
コード長 1,237 bytes
コンパイル時間 2,365 ms
コンパイル使用メモリ 208,684 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-28 03:59:08
合計ジャッジ時間 38,304 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1,396 ms
5,632 KB
testcase_04 AC 1,346 ms
5,632 KB
testcase_05 AC 1,364 ms
6,940 KB
testcase_06 AC 241 ms
6,944 KB
testcase_07 AC 240 ms
6,940 KB
testcase_08 AC 245 ms
6,944 KB
testcase_09 AC 240 ms
6,944 KB
testcase_10 AC 241 ms
6,944 KB
testcase_11 AC 240 ms
6,940 KB
testcase_12 AC 240 ms
6,940 KB
testcase_13 AC 242 ms
6,940 KB
testcase_14 AC 241 ms
6,944 KB
testcase_15 AC 244 ms
6,940 KB
testcase_16 AC 1,534 ms
6,940 KB
testcase_17 AC 1,532 ms
6,944 KB
testcase_18 AC 1,539 ms
5,888 KB
testcase_19 AC 1,529 ms
5,888 KB
testcase_20 AC 1,541 ms
5,760 KB
testcase_21 AC 1,527 ms
5,888 KB
testcase_22 AC 1,535 ms
5,888 KB
testcase_23 AC 1,535 ms
5,888 KB
testcase_24 AC 1,536 ms
5,888 KB
testcase_25 AC 1,535 ms
5,888 KB
testcase_26 AC 1,017 ms
5,888 KB
testcase_27 AC 787 ms
5,888 KB
testcase_28 AC 27 ms
5,376 KB
testcase_29 AC 27 ms
5,376 KB
testcase_30 AC 27 ms
5,376 KB
testcase_31 AC 25 ms
5,376 KB
testcase_32 AC 28 ms
5,376 KB
testcase_33 AC 28 ms
5,376 KB
testcase_34 AC 26 ms
5,376 KB
testcase_35 AC 28 ms
5,376 KB
testcase_36 AC 27 ms
5,376 KB
testcase_37 AC 28 ms
5,376 KB
testcase_38 AC 845 ms
5,376 KB
testcase_39 AC 171 ms
5,376 KB
testcase_40 AC 1,498 ms
5,888 KB
testcase_41 AC 392 ms
5,376 KB
testcase_42 AC 801 ms
5,376 KB
testcase_43 AC 563 ms
5,376 KB
testcase_44 AC 1,170 ms
5,376 KB
testcase_45 AC 1,060 ms
5,376 KB
testcase_46 AC 733 ms
5,376 KB
testcase_47 AC 955 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }

constexpr int B = 400;

int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vector<int> X(N);
    for (auto &x : X) cin >> x;
    int K = (N + B - 1) / B;
    vector<vector<int>> segs(K);
    REP(i, N) segs[i / B].push_back(X[i]);

    for (auto &vec : segs) sort(vec.begin(), vec.end());

    int Q;
    cin >> Q;
    while (Q--) {
        int l, r, x;
        cin >> l >> r >> x;
        l--;
        int ret = 1 << 30;
        while (l % B and l < r) {
            chmin(ret, abs(x - X[l]));
            l++;
        }
        while (r % B and r > l) {
            r--;
            chmin(ret, abs(x - X[r]));
        }
        for (int ib = l / B, ie = r / B; ib < ie; ib++) {
            auto itr = lower_bound(segs[ib].begin(), segs[ib].end(), x);
            if (itr != segs[ib].end()) chmin(ret, abs(x - *itr));
            if (itr != segs[ib].begin()) chmin(ret, abs(x - *(--itr)));
        }
        cout << ret << '\n';
    }
}
0