結果

問題 No.1471 Sort Queries
ユーザー toyuzukotoyuzuko
提出日時 2021-04-17 19:09:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 117 ms / 2,000 ms
コード長 4,188 bytes
コンパイル時間 140 ms
コンパイル使用メモリ 82,332 KB
実行使用メモリ 78,276 KB
最終ジャッジ日時 2024-07-04 02:10:08
合計ジャッジ時間 4,699 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,292 KB
testcase_01 AC 38 ms
53,136 KB
testcase_02 AC 37 ms
54,536 KB
testcase_03 AC 45 ms
61,792 KB
testcase_04 AC 47 ms
62,564 KB
testcase_05 AC 42 ms
60,976 KB
testcase_06 AC 37 ms
54,040 KB
testcase_07 AC 39 ms
55,428 KB
testcase_08 AC 46 ms
63,044 KB
testcase_09 AC 41 ms
55,328 KB
testcase_10 AC 37 ms
54,424 KB
testcase_11 AC 45 ms
61,468 KB
testcase_12 AC 48 ms
62,476 KB
testcase_13 AC 115 ms
77,204 KB
testcase_14 AC 108 ms
77,188 KB
testcase_15 AC 111 ms
77,284 KB
testcase_16 AC 104 ms
77,060 KB
testcase_17 AC 103 ms
77,464 KB
testcase_18 AC 98 ms
77,212 KB
testcase_19 AC 100 ms
77,032 KB
testcase_20 AC 111 ms
77,580 KB
testcase_21 AC 98 ms
77,644 KB
testcase_22 AC 101 ms
77,304 KB
testcase_23 AC 108 ms
77,556 KB
testcase_24 AC 108 ms
77,384 KB
testcase_25 AC 111 ms
77,368 KB
testcase_26 AC 108 ms
78,116 KB
testcase_27 AC 109 ms
77,412 KB
testcase_28 AC 109 ms
77,256 KB
testcase_29 AC 113 ms
77,700 KB
testcase_30 AC 110 ms
77,464 KB
testcase_31 AC 111 ms
77,928 KB
testcase_32 AC 116 ms
77,808 KB
testcase_33 AC 114 ms
77,576 KB
testcase_34 AC 115 ms
77,504 KB
testcase_35 AC 112 ms
77,576 KB
testcase_36 AC 117 ms
77,620 KB
testcase_37 AC 115 ms
77,572 KB
testcase_38 AC 115 ms
77,912 KB
testcase_39 AC 112 ms
78,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class FullyIndexableDictionary():
    def __init__(self, size):
        self.size = size
        self.block = (size + 31) >> 5
        self.bit = [0] * self.block
        self.sum = [0] * self.block

    def popcount(self, x):
        x = x - ((x >> 1) & 0x55555555)
        x = (x & 0x33333333) + ((x >> 2) & 0x33333333)
        x = x + (x >> 4) & 0x0f0f0f0f
        x = x + (x >> 8)
        x = x + (x >> 16)
        return x & 0x0000007f

    def set(self, k):
        self.bit[k >> 5] |= 1 << (k & 31)

    def build(self):
        for i in range(1, self.block):
            self.sum[i] = self.sum[i - 1] + self.popcount(self.bit[i - 1])

    def access(self, k):
        return (self.bit[k >> 5] >> (k & 31)) & 1

    def rank(self, k, v):
        r = self.sum[k >> 5] + self.popcount(self.bit[k >> 5] & ((1 << (k & 31)) - 1))
        return r if v else k - r

    def select(self, k, v):
        if k < 0 or self.rank(self.size, v) <= k: return -1
        l, r = 0, self.size
        while r - l > 1:
            m = (l + r) // 2
            if self.rank(m, v) >= k + 1:
                r = m
            else:
                l = m
        return l

class WaveletMatrix():
    def __init__(self, log=32):
        self.size = 0
        self.log = log
        self.mat = [None] * log
        self.mid = [None] * log

    def build(self, arr):
        self.size = len(arr)
        for lv in range(self.log)[::-1]:
            self.mat[lv] = FullyIndexableDictionary(self.size + 1)
            lt = []
            rt = []
            for i, a in enumerate(arr):
                if (a >> lv) & 1:
                    self.mat[lv].set(i)
                    rt.append(a)
                else:
                    lt.append(a)
            self.mid[lv] = len(lt)
            self.mat[lv].build()
            arr = lt + rt

    def access(self, k):
        res = 0
        for lv in range(self.log)[::-1]:
            if self.mat[lv].access(k):
                res |= 1 << lv
                k = self.mat[lv].rank(k, 1) + self.mid[lv]
            else:
                k = self.mat[lv].rank(k, 0)
        return res

    def rank(self, x, r):
        l = 0
        for lv in range(self.log)[::-1]:
            if (x >> lv) & 1:
                l = self.mat[lv].rank(l, 1) + self.mid[lv]
                r = self.mat[lv].rank(r, 1) + self.mid[lv]
            else:
                l = self.mat[lv].rank(l, 0)
                r = self.mat[lv].rank(r, 0)
        return r - l

    def select(self, x, k):
        idx = 0
        for lv in range(self.log)[::-1]:
            if (x >> lv) & 1:
                idx = self.mat[lv].rank(self.size, 0) + self.mat[lv].rank(idx, 1)
            else:
                idx = self.mat[lv].rank(idx, 0)
        idx += k
        for lv in range(self.log):
            if (x >> lv) & 1:
                idx = self.mat[lv].select(idx - self.mat[lv].rank(self.size, 0), 1)
            else:
                idx = self.mat[lv].select(idx, 0)
        return idx

    def freq(self, l, r, x):
        res = 0
        for lv in range(self.log)[::-1]:
            if (x >> lv) & 1:
                res += self.mat[lv].rank(r, 0) - self.mat[lv].rank(l, 0)
                l = self.mat[lv].rank(l, 1) + self.mid[lv]
                r = self.mat[lv].rank(r, 1) + self.mid[lv]
            else:
                l = self.mat[lv].rank(l, 0)
                r = self.mat[lv].rank(r, 0)
        return res

    def quantile(self, l, r, k):
        res = 0
        for lv in range(self.log)[::-1]:
            cnt = self.mat[lv].rank(r, 0) - self.mat[lv].rank(l, 0)
            if cnt <= k:
                res |= 1 << lv
                k -= cnt
                l = self.mat[lv].rank(l, 1) + self.mid[lv]
                r = self.mat[lv].rank(r, 1) + self.mid[lv]
            else:
                l = self.mat[lv].rank(l, 0)
                r = self.mat[lv].rank(r, 0)
        return res

N, Q = map(int, input().split())
S = input()

arr = [ord(s) for s in S]
wm = WaveletMatrix(8)
wm.build(arr)

res = []

for _ in range(Q):
    l, r, x = map(int, input().split())
    l -= 1; r -= 1; x -= 1
    x = wm.quantile(l, r + 1, x)
    res.append(chr(x))

print('\n'.join(res))
0