結果

問題 No.1471 Sort Queries
ユーザー むかでむかで
提出日時 2021-04-09 21:37:56
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 4,391 bytes
コンパイル時間 6,586 ms
コンパイル使用メモリ 201,148 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-07 10:20:44
合計ジャッジ時間 3,657 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 4 ms
4,380 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 3 ms
4,376 KB
testcase_23 AC 5 ms
4,380 KB
testcase_24 AC 4 ms
4,380 KB
testcase_25 AC 6 ms
4,380 KB
testcase_26 AC 5 ms
4,380 KB
testcase_27 AC 5 ms
4,376 KB
testcase_28 AC 5 ms
4,380 KB
testcase_29 AC 5 ms
4,380 KB
testcase_30 AC 4 ms
4,380 KB
testcase_31 AC 6 ms
4,380 KB
testcase_32 AC 7 ms
4,380 KB
testcase_33 AC 7 ms
4,380 KB
testcase_34 AC 7 ms
4,380 KB
testcase_35 AC 7 ms
4,380 KB
testcase_36 AC 7 ms
4,376 KB
testcase_37 AC 7 ms
4,376 KB
testcase_38 AC 7 ms
4,380 KB
testcase_39 AC 6 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>

#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert

#define debug(a) cerr<<(a)<<endl
#define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl
#define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;}

using namespace std;

template<class A, class B>
ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";}
template<class A, class B>
istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;}

//-------------------------------------------------
//--Wavelet Matrix
//-------------------------------------------------
class bit_vector
{
private:
    static constexpr size_t BITS = 6;
    static constexpr size_t UMASK = (1<<BITS)-1;

    const size_t nchunks;
    uint64_t *dat;
    int *sum;
public:
    bit_vector(const size_t n):nchunks((n>>BITS)+1){
        sum = (int*)malloc(nchunks*sizeof(int));
        dat = (uint64_t*)malloc(nchunks*sizeof(uint64_t));
        memset(dat,0,nchunks*sizeof(uint64_t));
    }
    inline void set(const int k){dat[k>>BITS]|=1ULL<<(k&UMASK);}
    inline void build(){
        sum[0]=0ULL;
        for(size_t i=1; i!=nchunks; i++){
            sum[i]=sum[i-1]+__builtin_popcountll(dat[i-1]);
        }
    }
    inline int rank1(const int k){
        return sum[k>>BITS] + 
            __builtin_popcountll(dat[k>>BITS]&~(UINT64_MAX<<(k&UMASK)));
    }
    inline int rank0(const int k){return k-rank1(k);}
    inline bool operator[](const int k){return dat[k>>BITS]>>(k&UMASK)&1;};
};
template<typename T, int MAXLOG=18>
class WaveletMatrix
{
private:
    bit_vector *vs[MAXLOG];
    int r0[MAXLOG];
    
    inline int next_idx(const int k, bool f, int i){
        return f ? vs[i]->rank1(k)+r0[i] : vs[i]->rank0(k);
    }
public:
    template<typename E>
    WaveletMatrix(E v){
        const size_t N = v.size();
        E left(N),right(N);
        for(int i=MAXLOG-1; i>=0; i--){
            vs[i] = new bit_vector(N);
            size_t l=0,r=0;
            for(size_t j=0; j<N; j++){
                if (v[j]>>i&1) vs[i]->set(j), right[r++]=v[j];
                else left[l++]=v[j];
            }
            vs[i]->build();
            r0[i] = l;
            v.swap(left);
            for(size_t j=0; j<r; j++)
                v[j+l]=right[j];
        }
    }
    T access(int k){
        T ret=static_cast<T>(0);
        for(int i=MAXLOG-1; i>=0; i--){
            const bool f = (*vs[i])[k];
            ret |= static_cast<T>(f)<<i;
            k = next_idx(k,f,i);
        }
        return ret;
    }
    int rank(int l, int r, const T x){
        for(int i=MAXLOG-1; i>=0; i--){
            const bool f = (x>>i&1);
            l = next_idx(l,f,i);
            r = next_idx(r,f,i);
        }
        return r-l;
    }
    inline int rank(const int k, const T x){return rank(0,k,x);}
    T quantile(int l, int r, int k){
        T ret=static_cast<T>(0);
        for(int i=MAXLOG-1; i>=0; i--){
            const int c0_l = vs[i]->rank0(l);
            const int c0_r = vs[i]->rank0(r);
            const int c0 = c0_r-c0_l;
            const bool f = (c0>k)?(false):(k-=c0,true);
            ret |= static_cast<T>(f)<<i;
            l = f ? l-c0_l+r0[i] : c0_l;
            r = f ? r-c0_r+r0[i] : c0_r;
        }
        return ret;
    }
};

//-------------------------------------------------

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
	int N,Q; cin>>N>>Q;
	string S; cin>>S;

	vi vec(N);
	rep(i,N) vec[i] = S[i]-'a';
	WaveletMatrix<int> wm(vec);
	while(Q--){
		int l,r,x; cin>>l>>r>>x; l--; x--;
		int res = wm.quantile(l,r,x);
		cout<<(char)(res+'a')<<"\n";
	}
    return 0;
}
0