結果

問題 No.1332 Range Nearest Query
ユーザー mugen_1337mugen_1337
提出日時 2021-01-12 11:16:19
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,623 ms / 2,500 ms
コード長 4,835 bytes
コンパイル時間 2,392 ms
コンパイル使用メモリ 206,388 KB
実行使用メモリ 10,076 KB
最終ジャッジ日時 2023-08-13 13:51:38
合計ジャッジ時間 55,463 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1,518 ms
9,076 KB
testcase_04 AC 1,532 ms
9,048 KB
testcase_05 AC 1,523 ms
9,080 KB
testcase_06 AC 1,246 ms
9,904 KB
testcase_07 AC 1,239 ms
10,072 KB
testcase_08 AC 1,235 ms
9,916 KB
testcase_09 AC 1,246 ms
9,988 KB
testcase_10 AC 1,258 ms
9,840 KB
testcase_11 AC 1,237 ms
9,824 KB
testcase_12 AC 1,246 ms
9,936 KB
testcase_13 AC 1,280 ms
9,952 KB
testcase_14 AC 1,261 ms
9,964 KB
testcase_15 AC 1,272 ms
9,944 KB
testcase_16 AC 1,611 ms
9,876 KB
testcase_17 AC 1,623 ms
9,932 KB
testcase_18 AC 1,601 ms
9,920 KB
testcase_19 AC 1,579 ms
9,864 KB
testcase_20 AC 1,621 ms
9,988 KB
testcase_21 AC 1,619 ms
10,076 KB
testcase_22 AC 1,592 ms
9,936 KB
testcase_23 AC 1,578 ms
9,936 KB
testcase_24 AC 1,610 ms
9,876 KB
testcase_25 AC 1,575 ms
9,880 KB
testcase_26 AC 955 ms
9,988 KB
testcase_27 AC 873 ms
9,876 KB
testcase_28 AC 129 ms
4,376 KB
testcase_29 AC 138 ms
4,376 KB
testcase_30 AC 151 ms
4,376 KB
testcase_31 AC 90 ms
4,380 KB
testcase_32 AC 163 ms
4,376 KB
testcase_33 AC 155 ms
4,380 KB
testcase_34 AC 107 ms
4,376 KB
testcase_35 AC 127 ms
4,380 KB
testcase_36 AC 118 ms
4,376 KB
testcase_37 AC 143 ms
4,376 KB
testcase_38 AC 1,235 ms
6,492 KB
testcase_39 AC 740 ms
4,376 KB
testcase_40 AC 1,545 ms
9,656 KB
testcase_41 AC 986 ms
4,420 KB
testcase_42 AC 1,226 ms
6,644 KB
testcase_43 AC 1,085 ms
5,080 KB
testcase_44 AC 1,436 ms
8,308 KB
testcase_45 AC 1,360 ms
7,492 KB
testcase_46 AC 1,182 ms
6,160 KB
testcase_47 AC 1,308 ms
7,012 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