結果
問題 | No.1471 Sort Queries |
ユーザー | toyuzuko |
提出日時 | 2021-04-17 19:06:51 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 143 ms / 2,000 ms |
コード長 | 4,187 bytes |
コンパイル時間 | 141 ms |
コンパイル使用メモリ | 82,460 KB |
実行使用メモリ | 78,632 KB |
最終ジャッジ日時 | 2024-07-04 02:08:28 |
合計ジャッジ時間 | 5,570 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
53,732 KB |
testcase_01 | AC | 45 ms
54,640 KB |
testcase_02 | AC | 46 ms
54,112 KB |
testcase_03 | AC | 60 ms
69,572 KB |
testcase_04 | AC | 58 ms
69,260 KB |
testcase_05 | AC | 61 ms
69,164 KB |
testcase_06 | AC | 43 ms
54,672 KB |
testcase_07 | AC | 62 ms
70,012 KB |
testcase_08 | AC | 62 ms
70,300 KB |
testcase_09 | AC | 54 ms
68,716 KB |
testcase_10 | AC | 45 ms
61,492 KB |
testcase_11 | AC | 54 ms
68,240 KB |
testcase_12 | AC | 65 ms
73,100 KB |
testcase_13 | AC | 108 ms
77,224 KB |
testcase_14 | AC | 111 ms
77,060 KB |
testcase_15 | AC | 113 ms
77,216 KB |
testcase_16 | AC | 108 ms
77,272 KB |
testcase_17 | AC | 110 ms
77,416 KB |
testcase_18 | AC | 110 ms
77,072 KB |
testcase_19 | AC | 107 ms
77,304 KB |
testcase_20 | AC | 111 ms
77,188 KB |
testcase_21 | AC | 112 ms
77,088 KB |
testcase_22 | AC | 119 ms
77,464 KB |
testcase_23 | AC | 120 ms
77,632 KB |
testcase_24 | AC | 120 ms
77,760 KB |
testcase_25 | AC | 124 ms
77,584 KB |
testcase_26 | AC | 118 ms
76,948 KB |
testcase_27 | AC | 120 ms
78,104 KB |
testcase_28 | AC | 121 ms
77,880 KB |
testcase_29 | AC | 117 ms
76,996 KB |
testcase_30 | AC | 115 ms
76,936 KB |
testcase_31 | AC | 125 ms
77,188 KB |
testcase_32 | AC | 124 ms
76,892 KB |
testcase_33 | AC | 127 ms
78,104 KB |
testcase_34 | AC | 130 ms
78,120 KB |
testcase_35 | AC | 133 ms
78,632 KB |
testcase_36 | AC | 143 ms
78,040 KB |
testcase_37 | AC | 138 ms
78,112 KB |
testcase_38 | AC | 124 ms
78,144 KB |
testcase_39 | AC | 124 ms
77,856 KB |
ソースコード
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() 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))