結果

問題 No.1332 Range Nearest Query
ユーザー satashunsatashun
提出日時 2021-01-08 21:47:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 433 ms / 2,500 ms
コード長 3,174 bytes
コンパイル時間 2,968 ms
コンパイル使用メモリ 210,320 KB
実行使用メモリ 93,512 KB
最終ジャッジ日時 2024-04-28 02:40:02
合計ジャッジ時間 17,524 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
52,264 KB
testcase_01 AC 27 ms
52,288 KB
testcase_02 AC 27 ms
52,416 KB
testcase_03 AC 404 ms
88,116 KB
testcase_04 AC 402 ms
88,128 KB
testcase_05 AC 399 ms
88,120 KB
testcase_06 AC 208 ms
93,368 KB
testcase_07 AC 209 ms
93,240 KB
testcase_08 AC 206 ms
93,348 KB
testcase_09 AC 207 ms
93,416 KB
testcase_10 AC 206 ms
93,476 KB
testcase_11 AC 207 ms
93,240 KB
testcase_12 AC 205 ms
93,364 KB
testcase_13 AC 213 ms
93,304 KB
testcase_14 AC 207 ms
93,380 KB
testcase_15 AC 215 ms
93,368 KB
testcase_16 AC 420 ms
93,304 KB
testcase_17 AC 426 ms
93,316 KB
testcase_18 AC 430 ms
93,348 KB
testcase_19 AC 419 ms
93,332 KB
testcase_20 AC 417 ms
93,352 KB
testcase_21 AC 433 ms
93,384 KB
testcase_22 AC 428 ms
93,512 KB
testcase_23 AC 428 ms
93,308 KB
testcase_24 AC 429 ms
93,300 KB
testcase_25 AC 428 ms
93,288 KB
testcase_26 AC 153 ms
93,400 KB
testcase_27 AC 148 ms
93,332 KB
testcase_28 AC 64 ms
52,384 KB
testcase_29 AC 63 ms
52,420 KB
testcase_30 AC 64 ms
52,260 KB
testcase_31 AC 64 ms
52,372 KB
testcase_32 AC 65 ms
52,404 KB
testcase_33 AC 65 ms
52,368 KB
testcase_34 AC 66 ms
52,380 KB
testcase_35 AC 65 ms
52,356 KB
testcase_36 AC 67 ms
52,376 KB
testcase_37 AC 64 ms
52,352 KB
testcase_38 AC 315 ms
73,192 KB
testcase_39 AC 151 ms
53,628 KB
testcase_40 AC 419 ms
92,044 KB
testcase_41 AC 223 ms
59,964 KB
testcase_42 AC 309 ms
72,164 KB
testcase_43 AC 270 ms
64,828 KB
testcase_44 AC 373 ms
83,000 KB
testcase_45 AC 361 ms
79,596 KB
testcase_46 AC 298 ms
70,048 KB
testcase_47 AC 337 ms
76,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define per(i, b) per2(i, 0, b)
#define per2(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define ALL(c) (c).begin(), (c).end()
#define SZ(x) ((int)(x).size())

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U>
void chmin(T& t, const U& u) {
    if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
    if (t < u) t = u;
}

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "{";
    rep(i, v.size()) {
        if (i) os << ",";
        os << v[i];
    }
    os << "}";
    return os;
}

#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << H;
    debug_out(T...);
}
#define debug(...) \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

const int INF = TEN(9) + 10;

const int sz = 1 << 20;

struct segtree {
    using Val = int;
    vector<vector<Val>> dat;
    segtree() {}

    void init(vector<Val> num) {
        dat.resize(sz * 2);

        rep(i, sz * 2) { dat[i].clear(); }

        rep(i, num.size()) { dat[sz - 1 + i].pb(num[i]); }

        for (int i = sz - 2; i >= 0; --i) {
            int lc = i * 2 + 1, rc = i * 2 + 2;
            dat[i].resize(dat[lc].size() + dat[rc].size());
            merge(dat[lc].begin(), dat[lc].end(), dat[rc].begin(),
                  dat[rc].end(), dat[i].begin());
        }
    }

    Val ask(int a, int b, int u, int k = 0, int l = 0, int r = sz) {
        if (b <= l || r <= a) return INF;
        if (a <= l && r <= b) {
            if (dat[k].size() == 0) return INF;
            int res = INF;
            if (u <= dat[k][0])
                res = dat[k][0] - u;
            else if (u >= dat[k].back())
                res = u - dat[k].back();
            else {
                auto it = lower_bound(ALL(dat[k]), u);
                res = min(abs((*it) - u), abs(*(prev(it)) - u));
            }
            return res;
        }

        return min(ask(a, b, u, k * 2 + 1, l, (l + r) / 2),
                   ask(a, b, u, k * 2 + 2, (l + r) / 2, r));
    }
} seg;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N;
    cin >> N;
    V<int> X(N);
    rep(i, N) cin >> X[i];

    segtree seg;
    seg.init(X);

    int Q;
    cin >> Q;
    while (Q--) {
        int l, r, y;
        cin >> l >> r >> y;
        --l;
        int ans = INF;
        cout << seg.ask(l, r, y) << '\n';
    }
    return 0;
}
0