BOJ - 11505 - 구간 곱 구하기

Updated:

import sys, math
mod = 1000000007

def initSegmentTree(arr, segmentTree, node, left, right):
    if left == right:
        segmentTree[node] = arr[left]
    else:
        mid = (left + right) // 2
        segmentTree[node] = (initSegmentTree(arr, segmentTree, node * 2, left, mid) * initSegmentTree(arr, segmentTree, node * 2 + 1, mid + 1, right)) % mod
    return segmentTree[node]

def findSectionValue(segmentTree, node, left, right, start, end):
    if left > end or right < start:
        return 1
    if start <= left and right <= end:
        return segmentTree[node]

    mid = (left + right) // 2
    return (findSectionValue(segmentTree, node * 2, left, mid, start, end) * findSectionValue(segmentTree, node * 2 + 1, mid + 1, right, start, end)) % mod

def updateValue(segmentTree, node, left, right, target, value):
    if target < left or right < target:
        return
    if left == right:
        segmentTree[node] = value
        return

    mid = (left + right) // 2
    updateValue(segmentTree, node * 2, left, mid, target, value)
    updateValue(segmentTree, node * 2 + 1, mid + 1, right, target, value)
    segmentTree[node] = (segmentTree[node * 2] * segmentTree[node * 2 + 1]) % mod

def solution():
    N, M, K = map(int, sys.stdin.readline().split())

    arr = []
    for _ in range(N):
        arr.append(int(sys.stdin.readline()))

    height = int(math.ceil(math.log2(N)))
    treeSize = 1 << (height + 1)

    segmentTree = [0] * treeSize
    initSegmentTree(arr, segmentTree, 1, 0, N-1)

    for _ in range(M + K):
        a, b, c = map(int, sys.stdin.readline().split())
        if a == 1:
            arr[b-1] = c
            updateValue(segmentTree, 1, 0, N-1, b-1, c)
        else:
            print(findSectionValue(segmentTree, 1, 0, N-1, b-1, c-1))

solution()

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

Categories:

Updated:

Leave a comment