結果

問題 No.1332 Range Nearest Query
ユーザー satashunsatashun
提出日時 2021-01-08 21:47:43
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 517 ms / 2,500 ms
コード長 3,174 bytes
コンパイル時間 2,751 ms
コンパイル使用メモリ 205,844 KB
実行使用メモリ 93,504 KB
最終ジャッジ日時 2023-08-10 09:14:24
合計ジャッジ時間 20,378 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
52,172 KB
testcase_01 AC 31 ms
52,160 KB
testcase_02 AC 31 ms
52,096 KB
testcase_03 AC 449 ms
88,076 KB
testcase_04 AC 460 ms
88,088 KB
testcase_05 AC 441 ms
88,084 KB
testcase_06 AC 233 ms
93,344 KB
testcase_07 AC 235 ms
93,388 KB
testcase_08 AC 234 ms
93,344 KB
testcase_09 AC 232 ms
93,308 KB
testcase_10 AC 240 ms
93,300 KB
testcase_11 AC 229 ms
93,364 KB
testcase_12 AC 229 ms
93,264 KB
testcase_13 AC 224 ms
93,368 KB
testcase_14 AC 228 ms
93,316 KB
testcase_15 AC 227 ms
93,304 KB
testcase_16 AC 473 ms
93,280 KB
testcase_17 AC 475 ms
93,504 KB
testcase_18 AC 463 ms
93,316 KB
testcase_19 AC 472 ms
93,316 KB
testcase_20 AC 479 ms
93,320 KB
testcase_21 AC 464 ms
93,264 KB
testcase_22 AC 467 ms
93,300 KB
testcase_23 AC 517 ms
93,268 KB
testcase_24 AC 474 ms
93,316 KB
testcase_25 AC 483 ms
93,280 KB
testcase_26 AC 167 ms
93,416 KB
testcase_27 AC 159 ms
93,388 KB
testcase_28 AC 70 ms
52,316 KB
testcase_29 AC 69 ms
52,132 KB
testcase_30 AC 74 ms
52,100 KB
testcase_31 AC 71 ms
52,080 KB
testcase_32 AC 74 ms
52,156 KB
testcase_33 AC 73 ms
52,128 KB
testcase_34 AC 71 ms
52,132 KB
testcase_35 AC 72 ms
52,100 KB
testcase_36 AC 73 ms
52,208 KB
testcase_37 AC 74 ms
52,204 KB
testcase_38 AC 349 ms
73,288 KB
testcase_39 AC 164 ms
53,740 KB
testcase_40 AC 463 ms
91,980 KB
testcase_41 AC 245 ms
59,752 KB
testcase_42 AC 352 ms
71,972 KB
testcase_43 AC 303 ms
64,860 KB
testcase_44 AC 420 ms
83,100 KB
testcase_45 AC 393 ms
79,556 KB
testcase_46 AC 344 ms
69,788 KB
testcase_47 AC 370 ms
76,416 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