結果

問題 No.1332 Range Nearest Query
ユーザー 👑 hitonanodehitonanode
提出日時 2021-01-08 22:32:34
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,402 ms / 2,500 ms
コード長 1,237 bytes
コンパイル時間 2,288 ms
コンパイル使用メモリ 207,204 KB
実行使用メモリ 5,940 KB
最終ジャッジ日時 2023-08-10 10:33:47
合計ジャッジ時間 35,338 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1,222 ms
5,488 KB
testcase_04 AC 1,205 ms
5,368 KB
testcase_05 AC 1,213 ms
5,448 KB
testcase_06 AC 218 ms
5,580 KB
testcase_07 AC 220 ms
5,636 KB
testcase_08 AC 219 ms
5,768 KB
testcase_09 AC 217 ms
5,940 KB
testcase_10 AC 219 ms
5,644 KB
testcase_11 AC 218 ms
5,604 KB
testcase_12 AC 219 ms
5,916 KB
testcase_13 AC 218 ms
5,632 KB
testcase_14 AC 219 ms
5,600 KB
testcase_15 AC 218 ms
5,692 KB
testcase_16 AC 1,386 ms
5,580 KB
testcase_17 AC 1,391 ms
5,680 KB
testcase_18 AC 1,401 ms
5,608 KB
testcase_19 AC 1,391 ms
5,644 KB
testcase_20 AC 1,402 ms
5,696 KB
testcase_21 AC 1,388 ms
5,772 KB
testcase_22 AC 1,387 ms
5,676 KB
testcase_23 AC 1,391 ms
5,636 KB
testcase_24 AC 1,385 ms
5,640 KB
testcase_25 AC 1,374 ms
5,852 KB
testcase_26 AC 1,007 ms
5,756 KB
testcase_27 AC 778 ms
5,676 KB
testcase_28 AC 25 ms
4,380 KB
testcase_29 AC 25 ms
4,376 KB
testcase_30 AC 26 ms
4,376 KB
testcase_31 AC 24 ms
4,376 KB
testcase_32 AC 26 ms
4,376 KB
testcase_33 AC 26 ms
4,376 KB
testcase_34 AC 25 ms
4,376 KB
testcase_35 AC 25 ms
4,380 KB
testcase_36 AC 26 ms
4,376 KB
testcase_37 AC 26 ms
4,376 KB
testcase_38 AC 752 ms
4,376 KB
testcase_39 AC 149 ms
4,380 KB
testcase_40 AC 1,334 ms
5,676 KB
testcase_41 AC 351 ms
4,376 KB
testcase_42 AC 719 ms
4,384 KB
testcase_43 AC 506 ms
4,380 KB
testcase_44 AC 1,048 ms
5,128 KB
testcase_45 AC 937 ms
4,848 KB
testcase_46 AC 658 ms
4,380 KB
testcase_47 AC 858 ms
4,608 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