[백준] 10505번 구간 곱 구하기

HL·2021년 7월 11일
0

백준

목록 보기
102/104

문제링크

https://www.acmicpc.net/problem/11505

코드

import sys, math

tree = []
a = []


def solution():
    global a, tree

    read = sys.stdin.readline
    write = sys.stdout.write
    n, m, k = map(int, read().split())
    a = [int(read()) for _ in range(n)]
    commands = [list(map(int, read().split())) for _ in range(m+k)]

    h = math.ceil(math.log(n, 2))
    tree_size = 2 ** (h+1)
    tree = [0] * tree_size
    init(0, n-1, 1)

    for aa, b, c in commands:
        if aa == 1:
            diff = c
            origin = a[b-1]
            a[b-1] = c
            update(b-1, 0, n-1, 1, diff, origin)
        elif aa == 2:
            write(f'{get_multi(b-1, c-1, 0, n-1, 1)}\n')


def init(start, end, node):

    # 리프노드일때
    if start == end:
        tree[node] = a[start]
        return tree[node]

    # 아닐때
    tree[node] = (init(start, (start+end)//2, node*2) * init((start+end)//2+1, end, node*2+1)) % 1000000007
    return tree[node]


def get_multi(left, right, start, end, node):
    # 포함하지 않을 때
    if left > end or right < start:
        return 1

    # 포함할 때
    if left <= start and end <= right:
        return tree[node] % 1000000007

    return (get_multi(left, right, start, (start+end)//2, node*2) * get_multi(left, right, (start+end)//2+1, end, node*2+1)) % 1000000007


def update(index, start, end, node, diff, origin):
    global tree

    # 포함하지 않을 때
    if index < start or index > end:
        return tree[node]

    # 리프노드일때
    if start == end:
        tree[node] = diff
        return tree[node]

    # 아닐때
    tree[node] = (update(index, start, (start+end)//2, node*2, diff, origin) * update(index, (start+end)//2+1, end, node*2+1, diff, origin)) % 1000000007
    return tree[node]


solution()
profile
Frontend 개발자입니다.

0개의 댓글