結果

問題 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  
実行時間 256 ms / 2,500 ms
コード長 4,686 bytes
コンパイル時間 5,194 ms
コンパイル使用メモリ 217,664 KB
実行使用メモリ 13,376 KB
最終ジャッジ日時 2023-08-11 04:08:05
合計ジャッジ時間 17,891 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 229 ms
12,876 KB
testcase_04 AC 229 ms
12,876 KB
testcase_05 AC 229 ms
12,716 KB
testcase_06 AC 160 ms
13,000 KB
testcase_07 AC 160 ms
13,148 KB
testcase_08 AC 159 ms
13,320 KB
testcase_09 AC 159 ms
13,152 KB
testcase_10 AC 159 ms
13,152 KB
testcase_11 AC 158 ms
12,980 KB
testcase_12 AC 159 ms
13,120 KB
testcase_13 AC 160 ms
13,068 KB
testcase_14 AC 160 ms
13,376 KB
testcase_15 AC 161 ms
13,052 KB
testcase_16 AC 254 ms
13,120 KB
testcase_17 AC 256 ms
13,084 KB
testcase_18 AC 248 ms
13,100 KB
testcase_19 AC 251 ms
13,056 KB
testcase_20 AC 249 ms
13,184 KB
testcase_21 AC 252 ms
13,136 KB
testcase_22 AC 249 ms
13,052 KB
testcase_23 AC 247 ms
13,304 KB
testcase_24 AC 252 ms
13,208 KB
testcase_25 AC 252 ms
13,056 KB
testcase_26 AC 144 ms
12,968 KB
testcase_27 AC 146 ms
13,308 KB
testcase_28 AC 51 ms
5,548 KB
testcase_29 AC 51 ms
5,744 KB
testcase_30 AC 52 ms
5,636 KB
testcase_31 AC 50 ms
5,740 KB
testcase_32 AC 51 ms
5,656 KB
testcase_33 AC 51 ms
5,640 KB
testcase_34 AC 50 ms
5,844 KB
testcase_35 AC 51 ms
5,700 KB
testcase_36 AC 50 ms
5,664 KB
testcase_37 AC 52 ms
5,716 KB
testcase_38 AC 169 ms
9,488 KB
testcase_39 AC 104 ms
6,828 KB
testcase_40 AC 244 ms
13,064 KB
testcase_41 AC 124 ms
8,148 KB
testcase_42 AC 160 ms
9,220 KB
testcase_43 AC 137 ms
8,556 KB
testcase_44 AC 205 ms
12,228 KB
testcase_45 AC 191 ms
11,948 KB
testcase_46 AC 156 ms
8,944 KB
testcase_47 AC 183 ms
11,884 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