結果

問題 No.1332 Range Nearest Query
ユーザー lorent_kyoprolorent_kyopro
提出日時 2021-01-09 13:42:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 274 ms / 2,500 ms
コード長 4,686 bytes
コンパイル時間 2,896 ms
コンパイル使用メモリ 218,436 KB
実行使用メモリ 13,428 KB
最終ジャッジ日時 2024-04-28 21:07:56
合計ジャッジ時間 15,037 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 232 ms
12,784 KB
testcase_04 AC 234 ms
12,936 KB
testcase_05 AC 235 ms
12,836 KB
testcase_06 AC 165 ms
13,120 KB
testcase_07 AC 165 ms
13,308 KB
testcase_08 AC 168 ms
13,272 KB
testcase_09 AC 165 ms
13,292 KB
testcase_10 AC 163 ms
13,300 KB
testcase_11 AC 163 ms
13,272 KB
testcase_12 AC 163 ms
13,236 KB
testcase_13 AC 167 ms
13,296 KB
testcase_14 AC 167 ms
13,244 KB
testcase_15 AC 164 ms
13,304 KB
testcase_16 AC 252 ms
13,304 KB
testcase_17 AC 260 ms
13,304 KB
testcase_18 AC 266 ms
13,340 KB
testcase_19 AC 257 ms
13,304 KB
testcase_20 AC 250 ms
13,308 KB
testcase_21 AC 257 ms
13,428 KB
testcase_22 AC 269 ms
13,176 KB
testcase_23 AC 274 ms
13,304 KB
testcase_24 AC 265 ms
13,308 KB
testcase_25 AC 262 ms
13,304 KB
testcase_26 AC 154 ms
12,920 KB
testcase_27 AC 153 ms
13,144 KB
testcase_28 AC 51 ms
5,760 KB
testcase_29 AC 51 ms
5,764 KB
testcase_30 AC 52 ms
5,636 KB
testcase_31 AC 50 ms
5,764 KB
testcase_32 AC 53 ms
5,764 KB
testcase_33 AC 53 ms
5,764 KB
testcase_34 AC 50 ms
5,760 KB
testcase_35 AC 51 ms
5,892 KB
testcase_36 AC 50 ms
5,764 KB
testcase_37 AC 52 ms
5,892 KB
testcase_38 AC 176 ms
9,384 KB
testcase_39 AC 109 ms
6,948 KB
testcase_40 AC 253 ms
13,256 KB
testcase_41 AC 126 ms
8,412 KB
testcase_42 AC 173 ms
9,632 KB
testcase_43 AC 145 ms
8,804 KB
testcase_44 AC 214 ms
12,508 KB
testcase_45 AC 201 ms
12,136 KB
testcase_46 AC 163 ms
9,344 KB
testcase_47 AC 188 ms
11,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <vector>

namespace atcoder {

template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  public:
    segtree() : segtree(0) {}
    segtree(int n) : segtree(std::vector<S>(n, e())) {}
    segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;

        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    template <bool (*f)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) {
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

}  // namespace atcoder


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

__attribute__((constructor))
void fast_io() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

using S = int;
S op(S a, S b) { return max(a, b); }
S e() { return -1; }

int main() {
    int n;
    cin >> n;
    vector<int> xs(n);
    vector<int> cx;
    for (auto& xi : xs) {
        cin >> xi;
        cx.push_back(xi);
    }
    int q;
    cin >> q;
    vector<tuple<int, int, int, int>> qs(q);
    for (int i = 0; i < q; ++i) {
        auto& [r, l, y, id] = qs[i];
        cin >> l >> r >> y;
        l--;
        cx.push_back(y);
        id = i;
    }
    sort(qs.begin(), qs.end());

    sort(cx.begin(), cx.end());
    cx.erase(unique(cx.begin(), cx.end()), cx.end());
    auto xi = [&](int x) { return distance(cx.begin(), lower_bound(cx.begin(), cx.end(), x)); };

    int m = cx.size();
    segtree<S, op, e> seg(m);

    vector<int> ans(q, INT_MAX);
    int nr = 0;
    for (auto [r, l, y, i] : qs) {
        while (nr < r) {
            seg.set(xi(xs[nr]), nr);
            nr++;
        }
        auto f = [&](int x) { return x < l; };
        {
            int j = seg.max_right(xi(y), f);
            if (j != m) ans[i] = min(ans[i], cx[j] - y);
        }
        {
            int j = seg.min_left(xi(y), f);
            if (j != 0) ans[i] = min(ans[i], y - cx[j - 1]);
        }
    }
    for (auto ansi : ans) cout << ansi << '\n';
}
0