BOJ - 1275 - 커피숍2

Updated:

import sys, math

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))
    return segmentTree[node]

def findSectionValue(segmentTree, node, left, right, start, end):
    if left > end or right < start:
        return 0
    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))

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

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


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

    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):
        x, y, a, b = map(int, sys.stdin.readline().split())
        if x > y: print(findSectionValue(segmentTree, 1, 0, N - 1, y - 1, x - 1))
        else: print(findSectionValue(segmentTree, 1, 0, N - 1, x - 1, y - 1))
        value = b - arr[a-1]
        arr[a-1] = b
        updateValue(segmentTree, 1, 0, N-1, a-1, value)

solution()

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

Categories:

Updated:

Leave a comment