Skip to content

Commit 28ff5b1

Browse files
authored
Merge pull request #285 from AlgorithmWithGod/lkhyun
[20250323] BOJ / G1 / 음주 코딩 / 이강현
2 parents 127b12e + f737c69 commit 28ff5b1

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
public class Main {
5+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
6+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
7+
static StringTokenizer st;
8+
static int N = 0;
9+
static int K = 0;
10+
static int[] arr;
11+
static int[] tree;
12+
public static void main(String[] args) throws Exception {
13+
st = new StringTokenizer(br.readLine());
14+
while(st.hasMoreTokens()){
15+
N = Integer.parseInt(st.nextToken());
16+
K = Integer.parseInt(st.nextToken());
17+
arr = new int[N+1];
18+
tree = new int[4*N];
19+
20+
st = new StringTokenizer(br.readLine());
21+
for(int i=1;i<=N;i++){
22+
arr[i] = Integer.parseInt(st.nextToken());
23+
}
24+
init(1,1,N);
25+
for(int i=1;i<=K;i++){
26+
st = new StringTokenizer(br.readLine());
27+
String cmd = st.nextToken();
28+
if(cmd.equals("C")){//변경
29+
int idx = Integer.parseInt(st.nextToken());
30+
int newValue = Integer.parseInt(st.nextToken());
31+
32+
if(newValue>0) update(1,1,N,idx,1);
33+
else if(newValue<0) update(1,1,N,idx,-1);
34+
else update(1,1,N,idx,0);
35+
36+
}else if(cmd.equals("P")){//곱셈
37+
int left = Integer.parseInt(st.nextToken());
38+
int right = Integer.parseInt(st.nextToken());
39+
int result = multiplication(1,1,N,left,right);
40+
if(result>0) bw.write("+");
41+
else if(result<0) bw.write("-");
42+
else bw.write("0");
43+
}
44+
}
45+
bw.write("\n");
46+
47+
String nextcmd = br.readLine();
48+
if(nextcmd == null) break;
49+
else st = new StringTokenizer(nextcmd);
50+
}
51+
bw.close();
52+
}
53+
public static void init(int cur, int start, int end){
54+
if(start==end){
55+
if(arr[start]>0) tree[cur] = 1;
56+
else if(arr[start]<0) tree[cur] = -1;
57+
else tree[cur] = 0;
58+
}else{
59+
init(cur*2,start,(start+end)/2);
60+
init(cur*2 + 1, (start+end)/2 + 1,end);
61+
tree[cur] = tree[cur*2] * tree[cur*2 + 1];
62+
}
63+
}
64+
public static void update(int cur, int start, int end, int idx, int newValue){
65+
if(idx<start || idx>end) return;
66+
if(start == end) {
67+
tree[cur] = newValue;
68+
return;
69+
}
70+
update(cur * 2, start, (start + end) / 2, idx, newValue);
71+
update(cur * 2 + 1, (start + end) / 2 + 1, end, idx, newValue);
72+
tree[cur] = tree[cur*2] * tree[cur*2 + 1];
73+
}
74+
public static int multiplication(int cur, int start, int end, int left, int right){
75+
if(right<start || left>end) return 1;
76+
if(left<=start && right>=end) return tree[cur];
77+
78+
int l = multiplication(cur*2,start,(start+end)/2,left,right);
79+
int r = multiplication(cur*2 + 1, (start+end)/2 + 1,end,left,right);
80+
return l*r;
81+
}
82+
}
83+
84+
```

0 commit comments

Comments
 (0)