Skip to content

Commit 3fc2a47

Browse files
authored
[20250319] BOJ / 골드1 / 구간 곱 구하기 / 이강현
1 parent ba9ea19 commit 3fc2a47

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class Main {
6+
static int N;
7+
static int M;
8+
static int K;
9+
static long[] arr;
10+
static long[] tree;
11+
final static long MOD = 1000000007L;
12+
public static void main(String[] args) throws Exception {
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
15+
StringTokenizer st = new StringTokenizer(br.readLine());
16+
N = Integer.parseInt(st.nextToken());
17+
M = Integer.parseInt(st.nextToken());
18+
K = Integer.parseInt(st.nextToken());
19+
arr = new long[N*4];
20+
tree = new long[N*4];
21+
22+
for(int i=1;i<=N;i++){
23+
arr[i] = Long.parseLong(br.readLine());
24+
}
25+
init(1,1,N);
26+
for(int i=0;i<M+K;i++){
27+
st = new StringTokenizer(br.readLine());
28+
int option = Integer.parseInt(st.nextToken());
29+
int arg1 = Integer.parseInt(st.nextToken());
30+
long arg2 = Long.parseLong(st.nextToken());
31+
if(option == 1){//수정
32+
arr[arg1] = arg2;
33+
update(1,1,N,arg1);
34+
}else if(option == 2){//구간 곱
35+
bw.write(query(1,1,N,arg1,(int)arg2) + "\n");
36+
}
37+
}
38+
bw.close();
39+
}
40+
public static void init(int num, int start, int end){ //초기화하는 거
41+
if(start==end){
42+
tree[num] = arr[start];
43+
}else{
44+
init(num*2, start, (start+end)/2);
45+
init(num*2 + 1,(start+end)/2 + 1, end);
46+
tree[num] = (tree[num*2] * tree[num*2+1]) % MOD;
47+
}
48+
}
49+
public static long query(int num, int start, int end, int left, int right){
50+
if(right<start || left>end){
51+
return 1;
52+
}else if(left<=start && right>=end){
53+
return tree[num];
54+
}
55+
long lsum = query(num*2, start, (start+end)/2, left, right);
56+
long rsum = query(num*2+1, (start+end)/2 + 1, end, left, right);
57+
return (lsum * rsum) % MOD;
58+
}
59+
public static long update(int num, int start, int end, int index){// 수정
60+
if(start>index || end<index) return tree[num]; //현재 위치가 수정할 곳과 연관이 있는가?
61+
62+
if(start != end){//현재 위치가 리프노드인가?
63+
long left = update(num*2, start,(start+end)/2,index);
64+
long right = update(num*2+1,(start+end)/2 + 1,end,index);
65+
tree[num] = (left*right) % MOD;
66+
return tree[num];
67+
}else{
68+
tree[num] = arr[start];
69+
return tree[num];
70+
}
71+
}
72+
}
73+
```

0 commit comments

Comments
 (0)