結果

問題 No.1332 Range Nearest Query
ユーザー mugen_1337mugen_1337
提出日時 2021-01-12 11:16:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,718 ms / 2,500 ms
コード長 4,835 bytes
コンパイル時間 2,360 ms
コンパイル使用メモリ 209,584 KB
実行使用メモリ 10,112 KB
最終ジャッジ日時 2024-05-01 07:03:43
合計ジャッジ時間 58,464 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 1,645 ms
9,216 KB
testcase_04 AC 1,634 ms
9,088 KB
testcase_05 AC 1,612 ms
9,216 KB
testcase_06 AC 1,311 ms
10,112 KB
testcase_07 AC 1,306 ms
9,984 KB
testcase_08 AC 1,306 ms
9,984 KB
testcase_09 AC 1,302 ms
9,984 KB
testcase_10 AC 1,307 ms
10,112 KB
testcase_11 AC 1,334 ms
10,112 KB
testcase_12 AC 1,318 ms
10,112 KB
testcase_13 AC 1,315 ms
10,112 KB
testcase_14 AC 1,318 ms
9,984 KB
testcase_15 AC 1,306 ms
9,984 KB
testcase_16 AC 1,666 ms
10,112 KB
testcase_17 AC 1,668 ms
9,984 KB
testcase_18 AC 1,641 ms
9,984 KB
testcase_19 AC 1,687 ms
9,984 KB
testcase_20 AC 1,699 ms
9,984 KB
testcase_21 AC 1,718 ms
9,984 KB
testcase_22 AC 1,707 ms
9,984 KB
testcase_23 AC 1,702 ms
10,112 KB
testcase_24 AC 1,683 ms
10,112 KB
testcase_25 AC 1,687 ms
9,984 KB
testcase_26 AC 970 ms
10,112 KB
testcase_27 AC 875 ms
10,112 KB
testcase_28 AC 136 ms
6,940 KB
testcase_29 AC 146 ms
6,944 KB
testcase_30 AC 162 ms
6,944 KB
testcase_31 AC 96 ms
6,940 KB
testcase_32 AC 174 ms
6,940 KB
testcase_33 AC 165 ms
6,940 KB
testcase_34 AC 113 ms
6,940 KB
testcase_35 AC 134 ms
6,940 KB
testcase_36 AC 124 ms
6,940 KB
testcase_37 AC 152 ms
6,940 KB
testcase_38 AC 1,301 ms
6,944 KB
testcase_39 AC 793 ms
6,940 KB
testcase_40 AC 1,662 ms
9,856 KB
testcase_41 AC 1,030 ms
6,940 KB
testcase_42 AC 1,276 ms
6,940 KB
testcase_43 AC 1,128 ms
6,944 KB
testcase_44 AC 1,493 ms
8,448 KB
testcase_45 AC 1,410 ms
7,680 KB
testcase_46 AC 1,239 ms
6,940 KB
testcase_47 AC 1,376 ms
7,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

struct SuccinctIndexableDictionary{
    int len,blocks;// blocks = ceil(len/32)
    vector<unsigned> bit,sum;// bit[i]: block iの下位桁のbit状態の有無
    SuccinctIndexableDictionary()=default;
    SuccinctIndexableDictionary(int len):len(len),blocks((len+31)>>5){
        bit.assign(blocks,0u);
        sum.assign(blocks,0u);
    }
    void set(int k){bit[k>>5]|=1u<<(k&31);}
    // kの有無
    bool operator[](int k){return (bool((bit[k>>5]>>(k&31))&1));}
    void build(){
        sum[0]=0u;
        for(int i=1;i<blocks;i++) sum[i]=sum[i-1]+__builtin_popcount(bit[i-1]);
    }
    // [0, k)の1の個数
    int rank(int k){
        return (sum[k>>5]+__builtin_popcount(bit[k>>5]&((1u<<(k&31))-1)));
    }
    // valの数
    int rank(bool val,int k){return (val?rank(k):k-rank(k));}
};

template<typename T,int MAXLOG>
struct WaveletMatrix{
    int len;
    // mat[i] : i桁で基数ソートする前.bitが立ってるindex
    SuccinctIndexableDictionary mat[MAXLOG];
    int mid[MAXLOG];

    WaveletMatrix()=default;
    WaveletMatrix(vector<T> v):len(v.size()){
        vector<T>l(len),r(len);
        for(int b=MAXLOG-1;b>=0;b--){
            mat[b]=SuccinctIndexableDictionary(len+1);
            int lp=0,rp=0;
            for(int i=0;i<len;i++){
                if((v[i]>>b)&1){
                    mat[b].set(i);
                    r[rp++]=v[i];
                }
                else l[lp++]=v[i]; 
            }
            mid[b]=lp;
            mat[b].build();
            swap(v,l);
            for(int i=0;i<rp;i++) v[lp+i]=r[i];
        }
    }
    pair<int,int> succ(bool f,int l,int r,int b){
        return {mat[b].rank(f,l)+mid[b]*f,mat[b].rank(f,r)+mid[b]*f};
    }
    // v[k]
    T access(int k){
        T ret=0;
        for(int b=MAXLOG-1;b>=0;b--){
            // 基数ソートされるときの行方を追う
            bool f=mat[b][k];
            if(f) ret|=(T(1)<<b);
            k=mat[b].rank(f,k)+mid[b]*f;
        }
        return ret;
    }
    T operator[](const int &k){return access(k);}
    // count x in [0, r) 
    int rank(const T &x,int r){
        int l=0;
        for(int b=MAXLOG-1;b>=0;b--) tie(l,r)=succ((x>>b)&1,l,r,b);
        return r-l;
    }
    // k-th smallest in [l,r)
    T kth_smallest(int l,int r,int k){
        assert(0<=k and k<r-l);
        T ret=0;
        for(int b=MAXLOG-1;b>=0;b--){
            int cnt=mat[b].rank(0,r)-mat[b].rank(0,l);
            bool f=(cnt<=k);
            if(f){
                ret|=T(1)<<b;
                k-=cnt;
            }
            tie(l,r)=succ(f,l,r,b);
        }
        return ret;
    }
    // k-th largest in [l,r)
    T kth_largest(int l,int r,int k){
        return kth_smallest(l,r,r-l-k-1);
    }
    // count v[i]<upper in [l,r)
    int range_freq(int l,int r,T upper){
        int ret=0;
        for(int b=MAXLOG-1;b>=0;b--){
            bool f=((upper>>b)&1);
            if(f) ret+=mat[b].rank(0,r)-mat[b].rank(0,l);
            tie(l,r)=succ(f,l,r,b);
        }
        return ret;
    }
    int range_freq(int l,int r,T lower,T upper){
        return range_freq(l,r,upper)-range_freq(l,r,lower);
    }
    // max v[i] in [l,r), v[i]<upper
    T prev_value(int l,int r,T upper){
        int cnt=range_freq(l,r,upper);
        return cnt==0?T(-1):kth_smallest(l,r,cnt-1);
    }
};


signed main(){
    int n;cin>>n;
    vector<int> v(n);
    cin>>v;

    WaveletMatrix<int,30> wav(v);

    int q;cin>>q;
    while(q--){
        int l,r,x;cin>>l>>r>>x;l--;

        int lw=0,hi=r-l-1,lb=r-l-1;
        while(lw<=hi){
            int mid=(lw+hi)/2;
            if(wav.kth_smallest(l,r,mid)>=x) hi=mid-1,lb=mid;
            else lw=mid+1;
        }

        int res=INF;
        chmin(res,abs(wav.kth_smallest(l,r,lb)-x));
        if(lb>0) chmin(res,abs(wav.kth_smallest(l,r,lb-1)-x));
        cout<<res<<"\n";
    }
    return 0;
}
0