forked from AllAlgorithms/java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SegmentTree.java
89 lines (73 loc) · 2.43 KB
/
SegmentTree.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
import java.util.*;
import java.lang.*;
import java.io.*;
class SegmentTree {
int st[];
Segment(int []arr, int n)
{
int x = (int) Math.ceil((Math.log(n)/Math.log(2)));
int max_size = 2 *(int) (Math.pow(2,x) - 1);
st = new int[max_size];
constSegmentUtil(arr,0,n-1,0);
}
int constSegmentUtil(int[] arr, int ss, int se, int si) {
if (ss == se) {
st[si] = arr[ss];
return arr[ss];
}
int mid = getMid(ss, se);
st[si] = max(constSegmentUtil(arr, ss, mid, 2 * si + 1), constSegmentUtil(arr, mid + 1, se, 2 * si + 2));
return st[si];
}
int getMid(int s, int e) {
return s + (e - s) / 2;
}
int max(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
void print() {
for (int i = 0; i < st.length; i++) {
System.out.print(st[i]);
}
}
int getMaxUtil(int ss, int se, int qs, int qe, int si) {
if (qs <= ss && qe >= se) {
return st[si];
}
if (se < qs || ss > qe) {
return 0;
}
int mid = getMid(ss, se);
return max(getMaxUtil(ss, mid, qs, qe, 2 * si + 1), getMaxUtil(mid + 1, se, qs, qe, 2 * si + 2));
}
int getMax(int n, int qs, int qe) {
if (qs > qe || qs < 0 || qe > n - 1) {
return -1;
}
return getMaxUtil(0, n - 1, qs, qe, 0);
}
public static void main(String[] args) throws java.lang.Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());// for testcase
String[] temp = br.readLine().split(" ");// taking array as string
int[] arr = new int[t];
for (int i = 0; i < t; i++) // to convert array of string to array of integers
{
arr[i] = Integer.parseInt(temp[i]);
}
Segment tree = new Segment(arr, arr.length);
// tree.print();
int q = Integer.parseInt(br.readLine()); // no of queries
while (q-- != 0) {
String[] temp1 = br.readLine().split(" ");
int s = Integer.parseInt(temp1[0]); // q1
int e = Integer.parseInt(temp1[1]); // q2
System.out.println(tree.getMax(arr.length, s - 1, e - 1)); // maximum in range [q1,q2]
// System.out.println(getMax(arr,n,s,e));
}
}
}