[Gold I] Title: 구간 곱 구하기, Time: 1292 ms, Memory: 113308 KB -BaekjoonHub

This commit is contained in:
SSUM
2025-03-04 15:39:19 +09:00
parent c50a7f63e1
commit 27460e957c
2 changed files with 85 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
# [Gold I] 구간 곱 구하기 - 11505
[문제 링크](https://www.acmicpc.net/problem/11505)
### 성능 요약
메모리: 113308 KB, 시간: 1292 ms
### 분류
세그먼트 트리, 자료 구조
### 제출 일자
2025년 3월 4일 15:38:39
### 문제 설명
<p>어떤 N개의 수가 주어져 있다. 그런데 중간에 수의 변경이 빈번히 일어나고 그 중간에 어떤 부분의 곱을 구하려 한다. 만약에 1, 2, 3, 4, 5 라는 수가 있고, 3번째 수를 6으로 바꾸고 2번째부터 5번째까지 곱을 구하라고 한다면 240을 출력하면 되는 것이다. 그리고 그 상태에서 다섯 번째 수를 2로 바꾸고 3번째부터 5번째까지 곱을 구하라고 한다면 48이 될 것이다.</p>
### 입력
<p>첫째 줄에 수의 개수 N(1 ≤ N ≤ 1,000,000)과 M(1 ≤ M ≤ 10,000), K(1 ≤ K ≤ 10,000) 가 주어진다. M은 수의 변경이 일어나는 횟수이고, K는 구간의 곱을 구하는 횟수이다. 그리고 둘째 줄부터 N+1번째 줄까지 N개의 수가 주어진다. 그리고 N+2번째 줄부터 N+M+K+1 번째 줄까지 세 개의 정수 a,b,c가 주어지는데, a가 1인 경우 b번째 수를 c로 바꾸고 a가 2인 경우에는 b부터 c까지의 곱을 구하여 출력하면 된다.</p>
<p>입력으로 주어지는 모든 수는 0보다 크거나 같고, 1,000,000보다 작거나 같은 정수이다.</p>
### 출력
<p>첫째 줄부터 K줄에 걸쳐 구한 구간의 곱을 1,000,000,007로 나눈 나머지를 출력한다.</p>

View File

@@ -0,0 +1,55 @@
import sys
input = sys.stdin.readline
n, m, k = map(int, input().split()) #수의 개수, 변경 횟수, 곱 구하는 횟수
length = n
height = 0
MOD = 1000000007
while length!=0:
length //= 2
height += 1
tree_size = pow(2, height + 1)
start_tree_left_index = pow(2, height) - 1
tree = [1] * (tree_size + 1)
for i in range(start_tree_left_index+1, start_tree_left_index+ n+1):
tree[i] = int(input())
temp = tree_size
while temp != 1:
tree[temp//2] = tree[temp//2] * tree[temp] % MOD
temp -= 1
def change_val(index, value):
tree[index] = value
while index > 1:
index //= 2
tree[index] = tree[index*2] % MOD * tree[index*2+1] %MOD
def mux(s, e):
result = 1
while s<=e:
if s%2==1:
result = tree[s] * result % MOD
s+=1
if e%2==0:
result = tree[e] * result % MOD
e-=1
s //= 2
e //= 2
return result
for i in range(m+k):
mode, s, e = map(int, input().split())
s += start_tree_left_index
if (mode == 1):
change_val(s, e)
else:
e += start_tree_left_index
print(mux(s, e))