-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.java
91 lines (74 loc) Β· 2.91 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package IndexedTree.P5676;
import java.io.*;
import java.util.*;
public class Main {
static int N, K;
static int[] nums;
final static char CH_CMD = 'C', MUL_CMD = 'P';
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/IndexedTree/P5676/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line);
N = stoi(st.nextToken());
K = stoi(st.nextToken());
st = new StringTokenizer(br.readLine());
nums = new int[N];
for (int i = 0; i < N; i++) nums[i] = stoi(st.nextToken());
SegmentTree tree = new SegmentTree(N, nums);
StringBuilder sb = new StringBuilder();
while (K-- > 0) {
st = new StringTokenizer(br.readLine());
char command = st.nextToken().charAt(0);
int from = stoi(st.nextToken()), to = stoi(st.nextToken());
if (command == CH_CMD) tree.changeValue(0, 0, N-1, from-1, to);
else if (command == MUL_CMD) {
int result = tree.multiple(0, 0, N-1, from-1, to-1);
if (result == 0) sb.append(0);
else sb.append(result > 0 ? '+' : '-');
}
}
System.out.println(sb.toString());
}
br.close();
}
static int stoi(String s) { return Integer.parseInt(s); }
}
class SegmentTree {
int[] tree;
int[] arr;
public SegmentTree(int N, int[] arr) {
this.tree = new int[(int) Math.pow(2, (Math.ceil(Math.log(N) / Math.log(2)) + 1))];
this.arr = arr;
makeTree(0, 0, N-1);
}
private int setValue(int value) {
return Integer.compare(value, 0);
}
void makeTree(int idx, int start, int end) {
if (start == end) {
tree[idx] = setValue(arr[start]);
return;
}
makeTree(idx*2+1, start, (start+end)/2);
makeTree(idx*2+2, (start+end)/2+1, end);
tree[idx] = tree[idx*2+1] * tree[idx*2+2];
}
void changeValue(int idx, int start, int end, int target, int value) {
if (target < start || target > end) return;
if (start == end) {
tree[idx] = setValue(value);
return;
}
changeValue(idx*2+1, start, (start+end)/2, target, value);
changeValue(idx*2+2, (start+end)/2+1, end, target, value);
tree[idx] = tree[idx*2+1] * tree[idx*2+2];
}
int multiple(int idx, int start, int end, int left, int right) {
if (end < left || start > right) return 1;
if (left <= start && end <= right) return tree[idx];
return multiple(idx*2+1, start, (start+end)/2, left, right)
* multiple(idx*2+2, (start+end)/2+1, end, left, right);
}
}