ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 7469 |
문제명 | K번째 수 |
레벨 | 플래티넘 3 |
분류 |
구간 쿼리 |
시간복잡도 | O(nlogn + qlog^2(n)) |
인풋사이즈 | n<=100,000, q<=5000 |
사용한 언어 | Python |
제출기록 | 70436KB / 452ms |
최고기록 | 452ms |
해결날짜 | 2021/05/02 |
"""Solution code for "BOJ 7469. K번째 수".
- Problem link: https://www.acmicpc.net/problem/7469
- Solution link: http://www.teferi.net/ps/problems/boj/7469
"""
import bisect
import operator
import sys
class MergeSortTreeForKthElem:
def __init__(self, values):
l = [[i] for i, value
in sorted(enumerate(values), key=operator.itemgetter(1))]
self._values = values
self._size = 1 << (len(l) - 1).bit_length()
self._tree = ([[] for _ in range(self._size)]
+ l + [[]] * (self._size - len(l)))
for i in range(self._size - 1, 0, -1):
self._tree[i] = self._tree[i * 2] + self._tree[i * 2 + 1]
self._tree[i].sort()
def kth(self, beg: int, end: int, k: int) -> int:
i = 1
while i < self._size:
i += i
node = self._tree[i]
t = bisect.bisect_left(node, end) - bisect.bisect_left(node, beg)
if t < k:
k -= t
i += 1
return self._values[self._tree[i][0]]
def main():
n, m = [int(x) for x in sys.stdin.readline().split()]
nums = [int(x) for x in sys.stdin.readline().split()]
mst = MergeSortTreeForKthElem(nums)
for _ in range(m):
i, j, k = [int(x) for x in sys.stdin.readline().split()]
print(mst.kth(i - 1, j, k))
if __name__ == '__main__':
main()