結果

問題 No.1332 Range Nearest Query
ユーザー ningenMeningenMe
提出日時 2021-01-08 23:20:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,123 ms / 2,500 ms
コード長 9,095 bytes
コンパイル時間 4,288 ms
コンパイル使用メモリ 245,880 KB
実行使用メモリ 12,872 KB
最終ジャッジ日時 2024-11-16 18:10:32
合計ジャッジ時間 35,203 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 996 ms
11,756 KB
testcase_04 AC 986 ms
11,756 KB
testcase_05 AC 965 ms
11,756 KB
testcase_06 AC 294 ms
12,744 KB
testcase_07 AC 292 ms
12,868 KB
testcase_08 AC 293 ms
12,864 KB
testcase_09 AC 295 ms
12,868 KB
testcase_10 AC 301 ms
12,740 KB
testcase_11 AC 303 ms
12,736 KB
testcase_12 AC 298 ms
12,864 KB
testcase_13 AC 304 ms
12,868 KB
testcase_14 AC 313 ms
12,740 KB
testcase_15 AC 294 ms
12,740 KB
testcase_16 AC 1,029 ms
12,740 KB
testcase_17 AC 1,015 ms
12,740 KB
testcase_18 AC 1,123 ms
12,868 KB
testcase_19 AC 1,120 ms
12,868 KB
testcase_20 AC 1,044 ms
12,868 KB
testcase_21 AC 1,070 ms
12,872 KB
testcase_22 AC 1,065 ms
12,868 KB
testcase_23 AC 1,047 ms
12,744 KB
testcase_24 AC 1,016 ms
12,864 KB
testcase_25 AC 1,043 ms
12,740 KB
testcase_26 AC 249 ms
12,740 KB
testcase_27 AC 238 ms
12,736 KB
testcase_28 AC 144 ms
5,248 KB
testcase_29 AC 149 ms
5,248 KB
testcase_30 AC 150 ms
5,248 KB
testcase_31 AC 140 ms
5,248 KB
testcase_32 AC 157 ms
5,248 KB
testcase_33 AC 158 ms
5,248 KB
testcase_34 AC 140 ms
5,248 KB
testcase_35 AC 145 ms
5,248 KB
testcase_36 AC 141 ms
5,248 KB
testcase_37 AC 148 ms
5,248 KB
testcase_38 AC 786 ms
7,948 KB
testcase_39 AC 436 ms
5,248 KB
testcase_40 AC 1,053 ms
12,484 KB
testcase_41 AC 575 ms
5,248 KB
testcase_42 AC 780 ms
7,852 KB
testcase_43 AC 654 ms
5,988 KB
testcase_44 AC 886 ms
10,268 KB
testcase_45 AC 850 ms
9,508 KB
testcase_46 AC 732 ms
7,244 KB
testcase_47 AC 798 ms
8,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using int128  = __int128_t;
using int64   = long long;
using int32   = int;
using uint128 = __uint128_t;
using uint64  = unsigned long long;
using uint32  = unsigned int;

#define ALL(obj) (obj).begin(),(obj).end()
template<class T> using priority_queue_reverse = priority_queue<T,vector<T>,greater<T>>;

constexpr int64 MOD = 1'000'000'000LL + 7; //'
constexpr int64 MOD2 = 998244353;
constexpr int64 HIGHINF = 1'000'000'000'000'000'000LL;
constexpr int64 LOWINF = 1'000'000'000'000'000LL; //'
constexpr long double PI = 3.1415926535897932384626433L;

template <class T> vector<T> multivector(size_t N,T init){return vector<T>(N,init);}
template <class... T> auto multivector(size_t N,T... t){return vector<decltype(multivector(t...))>(N,multivector(t...));}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const deque<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max(a,b);}
template <class T> void chmin(T& a, const T b){a=min(a,b);}
vector<string> split(const string &str, const char delemiter) {vector<string> res;stringstream ss(str);string buffer; while( getline(ss, buffer, delemiter) ) res.push_back(buffer); return res;}
inline constexpr int msb(int x) {return x?31-__builtin_clz(x):-1;}
inline constexpr int64 ceil_div(const int64 a,const int64 b) {return (a+(b-1))/b;}// return ceil(a/b)
void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;}

/*
 * @title BitVector
 * @docs md/segment/BitVector.md
 */
class BitVector{
    inline static constexpr size_t BIT_BLOCK_SIZE = 5;
    inline static constexpr size_t BIT_BLOCK_NUM  = 1<<BIT_BLOCK_SIZE;
    inline static constexpr uint32 popcount(uint32 x) {return __builtin_popcount(x);}
    inline static constexpr uint32 popcount(uint64 x) {return __builtin_popcountll(x);}

    vector<uint32> vec, acc;
    bool is_builded = false;
public:
    BitVector(uint32 N) {
        uint32 tmp = (N + BIT_BLOCK_NUM-1) / BIT_BLOCK_NUM;
        vec.assign(tmp, 0);
        acc.assign(tmp, 0);
    }
    void build() {
        for (size_t i = 0,sum = 0; i < acc.size(); ++i) acc[i] = (sum += popcount(vec[i]) );
        is_builded = true;
    }
    //[0,r) count of bit
    uint32 rank(uint32 r, const bool bit) const {
        assert(is_builded);
        uint32 bit_r = r >> BIT_BLOCK_SIZE;
        uint32 sum = (bit_r ? acc[bit_r - 1] : 0) + popcount(vec[bit_r] & ((1U << (r & (BIT_BLOCK_NUM-1))) - 1));
        return bit ? sum : r - sum;
    }
    //[l,l+1) = bit
    void update(uint32 l, const bool bit) {
        uint32 bit_l = l >> BIT_BLOCK_SIZE;
        if (bit) vec[bit_l] |=   1U << (l & (BIT_BLOCK_NUM-1));
        else     vec[bit_l] &= ~(1U << (l & (BIT_BLOCK_NUM-1)));
    }
    //[l,l+1)
    bool operator[](uint32 l) const { 
        assert(is_builded);
        return ((vec[l >> BIT_BLOCK_SIZE] >> (l & (BIT_BLOCK_NUM-1))) & 1); 
    }
};

/*
 * @title WaveletMatrix
 * @docs md/segment/WaveletMatrix.md
 */
template<class T> class WaveletMatrix{
    size_t length;
    size_t depth;
    vector<BitVector> multi_bit_vector;
    vector<uint32> sum_bit_off;
    vector<T> vec;
    vector<uint32> zarts(const vector<T>& ar) {
        vector<uint32> ord(ar.size()),compressed(ar.size());
        iota(ord.begin(),ord.end(),0);
        sort(ord.begin(),ord.end(),[&](size_t l, size_t r){return ar[l]<ar[r];});
        uint32 cnt = 0;
        compressed.front() = 0;
        T pre = ar[ord.front()];
        for(size_t i=1;i<ord.size();++i) {
            size_t j=ord[i];
            if(pre < ar[j]) ++cnt;
            compressed[j] = cnt;
            pre = ar[j];
        }
        return compressed;
    }
public:
    WaveletMatrix(const vector<T> &arg_vec): vec(arg_vec) {
        length = vec.size();
        auto compressed = zarts(vec);
        sort(vec.begin(),vec.end());
        vec.erase(unique(vec.begin(),vec.end()),vec.end());
        uint32 maxi = *max_element(compressed.begin(),compressed.end()) + 1;
        for(depth=0; (1<<depth) <= maxi; ++depth );

        multi_bit_vector.assign(depth, BitVector(length));
        sum_bit_off.assign(depth, 0UL);
        vector<uint32> prev = compressed, next = prev;
        for(uint32 j = 0; j < depth; ++j,swap(prev,next)) {
            uint32 bit = 1UL << (depth - j - 1);
            for(uint32 i = 0; i < length; ++i) sum_bit_off[j] += !(prev[i] & bit);

            uint32 idx_bit_off = 0, idx_bit_on = sum_bit_off[j];
            for(uint32 i = 0; i < length; ++i) {
                if (prev[i] & bit) multi_bit_vector[j].update(i,1), next[idx_bit_on++] = prev[i];
                else next[idx_bit_off++] = prev[i];
            }
            multi_bit_vector[j].build();
        }
    }
    //[l,l+1) element
    T operator[](uint32 l) const {
        uint32 val = 0;
        for (uint32 j = 0; j < depth; ++j) {
            const bool bit = multi_bit_vector[j][l];
            val = ((val << 1) | bit);
            l = multi_bit_vector[j].rank(l, bit);
            if(bit) l += sum_bit_off[j];
        }
        return vec[val];
    }

    //[l,r) range count of more and less than c (c'<c, c'=c, c<c')
    // tuple<uint32, uint32, uint32> rank_all(T c, uint32 l, uint32 r) const
    // {
    // 	if (c > vec.back()) return make_tuple(r - l, 0U, 0U);
    // 	array<uint32,2> more_and_less = {0,0};
    // 	for (uint32 j = 0; j < depth; ++j) {
    // 		const bool bit = (c >> (depth - j - 1)) & 1;
    // 		more_and_less[bit] += r - l;
    // 		l = multi_bit_vector[j].rank(l, bit);
    // 		r = multi_bit_vector[j].rank(r, bit);
    // 		if (bit) l += sum_bit_off[j], r += sum_bit_off[j];
    // 		more_and_less[bit] -= r - l;
    // 	}
    // 	return make_tuple(more_and_less[1], r - l, more_and_less[0]);
    // }

    //[l,r) range k (0-indexed) th smallest number
    T quantile(uint32 l, uint32 r, uint32 k) const {
        uint32 val = 0;
        for (uint32 j = 0; j < depth; ++j) {
            uint32 cnt_bit_off = multi_bit_vector[j].rank(r, 0) - multi_bit_vector[j].rank(l, 0);
            const bool bit = (k >= cnt_bit_off);
            val = ((val << 1) | bit);
            l = multi_bit_vector[j].rank(l, bit);
            r = multi_bit_vector[j].rank(r, bit);
            if (bit) l += sum_bit_off[j], r += sum_bit_off[j], k -= cnt_bit_off;
        }
        return vec[val];
    }
};

/**
 * @url 
 * @est
 */ 
int main() {
    cin.tie(0);ios::sync_with_stdio(false);
    int N; cin >> N;
    vector<int64> A(N);
    for(int i=0;i<N;++i) cin >> A[i];
    WaveletMatrix<int64> wm(A);

    int Q; cin >> Q;
    while(Q--) {
        int l,r; int64 x;
        cin >> l >> r >> x;
        l--;
        int k = r-l;
        int64 ans = LOWINF;

        chmin(ans,abs(x-A[l]));
        chmin(ans,abs(x-A[r-1]));
        if(k<=2) {
            cout << ans << endl;
            continue;
        }

        // print(l,r,x,ans);
        int64 mini = wm.quantile(l,r,0);
        int64 maxi = wm.quantile(l,r,k-1);
        chmin(ans,abs(x-mini));
        chmin(ans,abs(x-maxi));
        if(x <= mini || maxi <= x) {
            cout << ans << endl;
            continue;
        }

        // print(l,r,x,ans);
        int ok=0,ng=k,md;
        while(ng-ok>1) {
            md = (ok+ng)/2;
            int64 y = wm.quantile(l,r,md);
            (y<x?ok:ng)=md;
        }
        if(0<=ok-1 && ok-1<k) chmin(ans,abs(x- wm.quantile(l,r,ok-1)));
        if(0<=ok   && ok  <k) chmin(ans,abs(x- wm.quantile(l,r,ok  )));
        if(0<=ok+1 && ok+1<k) chmin(ans,abs(x- wm.quantile(l,r,ok+1)));
        cout << ans << endl;
    }
    return 0;
}
0