목차

K번째 수

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()