-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentTree.java
More file actions
97 lines (77 loc) · 2.07 KB
/
SegmentTree.java
File metadata and controls
97 lines (77 loc) · 2.07 KB
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
92
93
94
95
96
97
import java.util.Scanner;
import java.io.*;
import java.lang.String;
import java.lang.Integer;
class SegmentTree {
int[] array;
final int INFINITY = Integer.MAX_VALUE;
int root;
public static void main(String[] args) {
SegmentTree tree = new SegmentTree();
Scanner in = new Scanner(new InputStreamReader(System.in));
int n = in.nextInt();
int m = in.nextInt();
int[] preArray;
//Create initial array
preArray = new int[n+1]
for(int i = 1; i <= n; i++){
preArray[i] = in.nextInt();
}
//Create array with size expanded to nearest power of two
int expandedSize = tree.nextPowerOfTwo(n);
int[] expandedArray = new int[expandedSize+1];
for(int i = 1; i <= n; i++) {
expandedArray[i] = preArray[i]
}
for(int i = n+1; i<= expandedSize; i++){
expandedArray[i] = INFINITY;
}
//Create & build representational segment tree array
tree.array = new int[2*expandedSize];
int expIndex = 1;
for(int i = expandedSize/2; i <= (2*expandedSize)-1; i++) {
tree.array[i] = expandedArray[expIndex];
expIndex++;
}
for(int i = (2*expandedSize)-2; i >= 2; i-=2){
if(tree.array[i] < tree.array[i+1])
tree.array[i/2] = tree.array[i];
else tree.array[i/2] = tree.array[i+1];
}
//Execute commands
String command;
int left, right, index, value;
for(int i = 0; i < m; i++) {
command = in.next();
if(command.equals(Min)) {
//set arguments to actual index in array
left = in.nextInt();
left = left + (expandedSize/2) - 1;
right = in.nextInt();
right = right + (expandedSize/2) - 1;
tree.Min(left, right)
}
if(command.equals(Set)){
index = in.nextInt();
index = index + (expandedSize/2) - 1;
value = in.nextInt();
tree.array[index] = value;
}
}
}
public int nextPowerOfTwo(int num){
int pwr = 1;
while (pwr < num)
{
pwr = pwr << 1;
}
System.out.println(pwr);
return pwr;
}
//Recursive function to get minimum
public int Min(int left, int right){
if(left )
}
public Set(int index, int value){
}
}