목차

수열과 쿼리 15

ps
링크acmicpc.net/…
출처BOJ
문제 번호14427
문제명수열과 쿼리 15
레벨골드 1
분류

우선순위큐

시간복잡도O(n+mlogn)
인풋사이즈n<=100,000, m<=100,000
사용한 언어Python
제출기록51624KB / 468ms
최고기록468ms
해결날짜2021/03/20

풀이

코드

코드 1 - 우선순위큐

"""Solution code for "BOJ 14427. 수열과 쿼리 15".

- Problem link: https://www.acmicpc.net/problem/14427
- Solution link: http://www.teferi.net/ps/problems/boj/14427
"""

import sys
import heapq


def main():
    N = int(sys.stdin.readline())
    A = [int(x) for x in sys.stdin.readline().split()]
    heap = [(a_i, i + 1) for i, a_i in enumerate(A)]
    heapq.heapify(heap)
    M = int(sys.stdin.readline())
    for i in range(M):
        query = [int(x) for x in sys.stdin.readline().split()]
        if query[0] == 1:
            _, i, v = query
            heapq.heappush(heap, (v, i))
            A[i - 1] = v
            while heap[0][0] != A[heap[0][1] - 1]:
                heapq.heappop(heap)
        else:
            print(heap[0][1])


if __name__ == '__main__':
    main()

코드 2 - 세그먼트 트리

"""Solution code for "BOJ 14427. 수열과 쿼리 15".

- Problem link: https://www.acmicpc.net/problem/14427
- Solution link: http://www.teferi.net/ps/problems/boj/14427
"""

import sys
from teflib import segmenttree


def main():
    N = int(sys.stdin.readline())
    A = [int(x) for x in sys.stdin.readline().split()]
    segtree = segmenttree.SegmentTree((a_i, i + 1) for i, a_i in enumerate(A))
    M = int(sys.stdin.readline())
    for i in range(M):
        query = [int(x) for x in sys.stdin.readline().split()]
        if query[0] == 1:
            _, i, v = query
            segtree.set(i - 1, (v, i))
        else:
            print(segtree.query(0, N)[1])


if __name__ == '__main__':
    main()