結果

問題 No.1332 Range Nearest Query
ユーザー kcvlexkcvlex
提出日時 2021-01-08 23:21:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 11,206 bytes
コンパイル時間 2,156 ms
コンパイル使用メモリ 158,004 KB
実行使用メモリ 107,356 KB
最終ジャッジ日時 2024-11-16 18:08:42
合計ジャッジ時間 94,952 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,248 KB
testcase_02 AC 3 ms
5,248 KB
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 AC 1,521 ms
100,736 KB
testcase_07 AC 1,652 ms
100,608 KB
testcase_08 AC 1,622 ms
100,480 KB
testcase_09 AC 1,488 ms
100,480 KB
testcase_10 AC 1,462 ms
100,608 KB
testcase_11 AC 1,423 ms
100,608 KB
testcase_12 AC 1,398 ms
100,480 KB
testcase_13 AC 1,449 ms
100,608 KB
testcase_14 AC 1,466 ms
100,480 KB
testcase_15 AC 1,632 ms
100,480 KB
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 AC 1,583 ms
100,480 KB
testcase_27 AC 981 ms
100,608 KB
testcase_28 AC 1,067 ms
5,248 KB
testcase_29 AC 1,068 ms
5,248 KB
testcase_30 AC 1,085 ms
5,248 KB
testcase_31 AC 828 ms
5,248 KB
testcase_32 AC 1,123 ms
5,248 KB
testcase_33 AC 1,087 ms
5,248 KB
testcase_34 AC 958 ms
5,248 KB
testcase_35 AC 1,015 ms
5,248 KB
testcase_36 AC 976 ms
5,248 KB
testcase_37 AC 1,069 ms
5,248 KB
testcase_38 TLE -
testcase_39 AC 1,355 ms
6,656 KB
testcase_40 TLE -
testcase_41 AC 1,747 ms
21,376 KB
testcase_42 AC 2,248 ms
50,048 KB
testcase_43 AC 1,966 ms
33,024 KB
testcase_44 TLE -
testcase_45 AC 2,471 ms
68,352 KB
testcase_46 AC 2,156 ms
45,312 KB
testcase_47 AC 2,372 ms
60,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <limits>
#include <initializer_list>
#include <utility>
#include <bitset>
#include <tuple>
#include <type_traits>
#include <functional>
#include <string>
#include <array>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <iterator>
#include <algorithm>
#include <complex>
#include <random>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <regex>
#include <cassert>
#include <cstddef>
#include <variant>

#define endl codeforces

#define ALL(v) std::begin(v), std::end(v)
#define ALLR(v) std::rbegin(v), std::rend(v)

using ll = std::int64_t;
using ull = std::uint64_t;
using pii = std::pair<int, int>;
using tii = std::tuple<int, int, int>;
using pll = std::pair<ll, ll>;
using tll = std::tuple<ll, ll, ll>;
using size_type = ssize_t;
template <typename T> using vec = std::vector<T>;
template <typename T> using vvec = vec<vec<T>>;

template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }

template <typename T, std::size_t Head, std::size_t... Tail> 
struct multi_dim_array { using type = std::array<typename multi_dim_array<T, Tail...>::type, Head>; };

template <typename T, std::size_t Head> 
struct multi_dim_array<T, Head> { using type = std::array<T, Head>; };

template <typename T, std::size_t... Args> using mdarray = typename multi_dim_array<T, Args...>::type;

template <typename T, typename F, typename... Args> 
void fill_seq(T &t, F f, Args... args) { 
    if constexpr (std::is_invocable<F, Args...>::value) { 
        t = f(args...); 
    } else { 
        for (size_type i = 0; i < t.size(); i++) fill_seq(t[i], f, args..., i); 
    } 
}

template <typename T> vec<T> make_v(size_type sz) { return vec<T>(sz); }

template <typename T, typename... Tail> 
auto make_v(size_type hs, Tail&&... ts) { 
    auto v = std::move(make_v<T>(std::forward<Tail>(ts)...)); 
    return vec<decltype(v)>(hs, v); 
}

namespace init__ { 
struct InitIO { 
    InitIO() { 
        std::cin.tie(nullptr); 
        std::ios_base::sync_with_stdio(false); 
        std::cout << std::fixed << std::setprecision(30); 
    } 
} init_io; 
}

template <typename T>
T ceil_pow2(T bound) {
    T ret = 1;
    while (ret < bound) ret *= 2;
    return ret;
}

template <typename T>
T ceil_div(T a, T b) { return a / b + !!(a % b); }



namespace utility {

template <typename T>
using validate_integer = typename std::enable_if<std::is_integral<T>::value, ll>::type;

template <typename T>
auto popcount(T n) -> validate_integer<T> {
    return __builtin_popcountll(n);
}

// 0 indexed
template <typename T>
auto msb(T n) -> validate_integer<T> {
    return 64 - __builtin_clzll(n) - 1;
}

}

namespace utility {

constexpr ll ceil_div(ll a, ll b) {
    return a / b + !!(a % b);
}

}

namespace succinct { 

template <typename T>
constexpr T ceil_log2(T n) {
    for (int i = 0; i < 63; i++) {
        T mask = 1ll << i;
        if (n <= mask) return i;
    }
    return -1;
}

template <typename T>
constexpr T ceil_even(T n) {
    return n + (n & 1);
}

template <std::size_t MaxSize>
struct FullyIndexableDictionary {
    using size_type = ssize_t;

private:
    constexpr static size_type sz_log2 = ceil_even(ceil_log2<size_type>(MaxSize));
    constexpr static size_type chunk_sz = sz_log2 * sz_log2;
    constexpr static size_type block_sz = sz_log2 / 2;
    constexpr static size_type block_per_chunk = chunk_sz / block_sz;

    size_type sz, cnt1;
    vec<size_type> chunk, block, dat;

public:
    FullyIndexableDictionary() { } 

    template <typename F>
    FullyIndexableDictionary(F f, size_type sz) 
        : sz(sz), cnt1(0),
          chunk(utility::ceil_div(sz, chunk_sz)), 
          block(chunk.size() * block_per_chunk),
          dat(block.size())
    {
        size_type idx = 0;
        for (size_type i = 0; i < chunk.size(); i++) {
            chunk[i] = cnt1;
            size_type tmp = 0;
            for (size_type j = 0; j < block_per_chunk; j++, idx++) {
                block[idx] = tmp;
                for (size_type k = 0; k < block_sz; k++) {
                    auto v = f(idx * block_sz + k);
                    size_type mask = (1ll << k) * v;
                    dat[idx] |= mask;
                    tmp += v;
                }
            }
            cnt1 += tmp;
        }
    }

    size_type rank(bool b, size_type pos) const {
        if (!b) return pos - rank(!b, pos);
        if (pos == sz) return sum(b);
        size_type ret = 0;
        
        ret += chunk[pos / chunk_sz];
        ret += block[pos / block_sz];

        auto mask = (1ll << (pos % block_sz)) - 1;
        return ret + utility::popcount(dat[pos / block_sz] & mask);
    }

    size_type rank(bool b, size_type l, size_type r) const {
        return rank(b, r) - rank(b, l);
    }

    size_type select(bool b, size_type n) const {
        if (rank(b, sz) < n) return -1;

        size_type ok = sz, ng = 0;
        while (1 < std::abs(ok - ng)) {
            size_type mid = (ok + ng) / 2;
            (n <= rank(b, mid) ? ok : ng) = mid;
        }
        return ok;
    }

    bool operator [](size_type i) const {
        size_type offset = i % block_sz;
        return (dat[i / block_sz] >> offset) & 1;
    }

    size_type sum(bool b) const {
        return b ? cnt1 : sz - cnt1;
    }
};

}

namespace succinct { 

template <typename T, std::size_t MaxSize, std::size_t MaxValueLog = 32>
class WaveletMatrix {
    using data_type = FullyIndexableDictionary<MaxSize>;

public:
    using size_type = typename data_type::size_type;

private:
    size_type sz;
    std::array<data_type, MaxValueLog> mat;
    std::array<vec<size_type>, MaxValueLog> acc_sum;

    void update_range_aux(bool bt, size_type &l, size_type &r, size_type i) const {
        const auto &dic = mat[i];
        l = dic.rank(bt, l) + bt * dic.sum(0);
        r = dic.rank(bt, r) + bt * dic.sum(0);
    }

    size_type range_freq_aux(size_type l, size_type r, T bound) const {
        if ((1ll << MaxValueLog) - 1 <= bound) return r - l;
        size_type ret = 0;
        for (size_type i = 0; i < MaxValueLog && l < r; i++) {
            T mask = 1ll << (MaxValueLog - (i + 1));
            bool bt = !!(bound & mask);
            if (bt) ret += mat[i].rank(0, l, r);
            update_range_aux(bt, l, r, i);
        }
        return ret;
    }

    size_type range_sum_aux(size_type l, size_type r, T bound) const {
        if ((1ll << MaxValueLog) - 1 <= bound) return acc_sum[0][r] - acc_sum[0][l];
        T ret = 0;
        for (size_type i = 0; i < MaxValueLog && l < r; i++) {
            T mask = 1ll << (MaxValueLog - (i + 1));
            bool bt = !!(bound & mask);
            if (bt) ret += acc_sum[i][r] - acc_sum[i][l];
            update_range_aux(bt, l, r, i);
        }
        return ret;
    }

public:
    WaveletMatrix(vec<T> dat) : sz(dat.size()) {
        vec<T> buf(sz);
        for (size_type i = 0; i < MaxValueLog; i++) {
            size_type mask = 1ll << (MaxValueLog - i - 1);
            auto f = [&](size_type idx) -> bool {
                if (dat.size() <= idx) return false;
                return !!(dat[idx] & mask);
            };
            mat[i] = std::move(data_type(f, dat.size()));
            size_type l = 0, r = 0;
            acc_sum[i].resize(sz + 1, 0);
            for (size_type j = 0; j < dat.size(); j++) {
                auto e = dat[j];
                if (e & mask) {
                    r++;
                    buf[sz - r] = e;
                } else {
                    buf[l] = e;
                    acc_sum[i][j + 1] += e;
                    l++;
                }
            }
            for (size_type k = 0; k < dat.size(); k++) acc_sum[i][k + 1] += acc_sum[i][k];
            std::reverse(buf.begin() + l, buf.end());
            std::swap(buf, dat);
        }
    }

    size_type rank(T t, size_type pos) const {
        size_type mask = 1ll << (MaxValueLog - 1);
        size_type l = 0, r = pos;
        for (size_type i = 0; i < MaxValueLog; i++, mask /= 2) {
            bool bt = !!(t & mask);
            update_range_aux(bt, l, r, i);
        }
        return r - l;
    }

    size_type select(T t, size_type n) const {
        std::array<size_type, MaxValueLog> larr, rarr;
        size_type l = 0, r = sz;
        for (size_type i = 0, mask = (1ll << (MaxValueLog - 1)); i < MaxValueLog; i++, mask /= 2) {
            larr[i] = l, rarr[i] = r;
            bool bt = !!(t & mask);
            update_range_aux(bt, l, r, i);
        }

        for (size_type i = 0, mask = 1; i < MaxValueLog; i++, mask *= 2) {
            size_type idx = MaxValueLog - (i + 1);
            const auto &dic = mat[idx];
            bool bt = !!(t & mask);
            n += dic.rank(bt, larr[idx]);
            size_type ra = dic.sel(bt, n);
            if (ra == -1) return -1;
            n = ra;
        }
        return n;
    }

    // k >= 0
    T quantile(size_type l, size_type r, size_type k) const {
        T ret = 0;
        for (size_type i = 0; i < MaxValueLog && l < r; i++) {
            T mask = 1ll << (MaxValueLog - (i + 1));
            const auto &dic = mat[i];
            size_type cl = dic.rank(1, l), cr = dic.rank(1, r);
            if (k < cr - cl) {
                l = cl + dic.sum(0);
                r = cr + dic.sum(0);
                ret |= mask;
            } else {
                k -= cr - cl;
                l -= cl;
                r -= cr;
            }
        }
        return ret;
    }

    // [l, r), [min, max)
    size_type range_freq(size_type l, size_type r, T min, T max) const {
        return range_freq_aux(l, r, max) - range_freq_aux(l, r, min);
    }

    size_type range_sum(size_type l, size_type r, T min, T max) const {
        return range_sum_aux(l, r, max) - range_sum_aux(l, r, min);
    }
};

}
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")



int main() {
    ll n;
    std::cin >> n;
    vec<ll> xv(n);
    for (ll &e : xv) std::cin >> e;
    ll q;
    std::cin >> q;
 
    auto mat = succinct::WaveletMatrix<ll, ll(3e5 + 10), 32>(xv);
    const ll inf = 1e9 + 10;
 
    while (q--) {
        ll l, r, x;
        std::cin >> l >> r >> x;
        l--;
        ll ok = inf, ng = -1;
        while (std::abs(ok - ng) > 1) {
            const ll mid = (ok + ng) / 2;
            const ll lb = std::max<ll>(0, x - mid);
            const ll ub = x + mid + 1;
            const ll cnt = mat.range_freq(l, r, lb, ub);
            (cnt ? ok : ng) = mid;
        }
        std::cout << ok << "\n";
    }
    return 0;
}
0