結果

問題 No.1332 Range Nearest Query
ユーザー ningenMeningenMe
提出日時 2021-01-08 23:20:53
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,166 ms / 2,500 ms
コード長 9,095 bytes
コンパイル時間 4,458 ms
コンパイル使用メモリ 244,344 KB
実行使用メモリ 12,964 KB
最終ジャッジ日時 2023-08-10 12:19:23
合計ジャッジ時間 37,502 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1,068 ms
11,488 KB
testcase_04 AC 1,082 ms
11,708 KB
testcase_05 AC 1,075 ms
11,548 KB
testcase_06 AC 330 ms
12,776 KB
testcase_07 AC 329 ms
12,964 KB
testcase_08 AC 338 ms
12,732 KB
testcase_09 AC 330 ms
12,776 KB
testcase_10 AC 332 ms
12,704 KB
testcase_11 AC 329 ms
12,696 KB
testcase_12 AC 325 ms
12,784 KB
testcase_13 AC 336 ms
12,728 KB
testcase_14 AC 332 ms
12,692 KB
testcase_15 AC 328 ms
12,776 KB
testcase_16 AC 1,164 ms
12,772 KB
testcase_17 AC 1,132 ms
12,880 KB
testcase_18 AC 1,150 ms
12,688 KB
testcase_19 AC 1,166 ms
12,960 KB
testcase_20 AC 1,153 ms
12,772 KB
testcase_21 AC 1,166 ms
12,732 KB
testcase_22 AC 1,159 ms
12,760 KB
testcase_23 AC 1,145 ms
12,700 KB
testcase_24 AC 1,165 ms
12,760 KB
testcase_25 AC 1,157 ms
12,768 KB
testcase_26 AC 256 ms
12,796 KB
testcase_27 AC 253 ms
12,760 KB
testcase_28 AC 148 ms
4,380 KB
testcase_29 AC 153 ms
4,376 KB
testcase_30 AC 158 ms
4,376 KB
testcase_31 AC 146 ms
4,380 KB
testcase_32 AC 162 ms
4,376 KB
testcase_33 AC 161 ms
4,380 KB
testcase_34 AC 146 ms
4,380 KB
testcase_35 AC 151 ms
4,380 KB
testcase_36 AC 147 ms
4,380 KB
testcase_37 AC 154 ms
4,384 KB
testcase_38 AC 872 ms
8,036 KB
testcase_39 AC 461 ms
4,380 KB
testcase_40 AC 1,116 ms
12,316 KB
testcase_41 AC 630 ms
4,772 KB
testcase_42 AC 855 ms
7,552 KB
testcase_43 AC 742 ms
5,992 KB
testcase_44 AC 1,000 ms
10,184 KB
testcase_45 AC 960 ms
9,504 KB
testcase_46 AC 813 ms
7,320 KB
testcase_47 AC 923 ms
8,760 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