-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentTree.java
More file actions
64 lines (51 loc) · 2.04 KB
/
SegmentTree.java
File metadata and controls
64 lines (51 loc) · 2.04 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
import java.util.Scanner;
public class SegmentTree{
public static void buildtree(int ind, int low, int high, int arr[], int seg[]) {
if (low == high) {
seg[ind] = arr[low]; // Use low instead of ind here
return;
}
int mid = (low + high) / 2;
buildtree(2 * ind + 1, low, mid, arr, seg);
buildtree(2 * ind + 2, mid + 1, high, arr, seg);
// Combine the results from the left and right children
seg[ind] = Math.min(seg[2 * ind + 1], seg[2 * ind + 2]);
// Use seg[2 * ind + 1] and seg[2 * ind + 2]
}
public static int query(int ind, int low , int high ,int l ,int r ,int seg[]){
if (r < low || l > high) {
return Integer.MAX_VALUE; // Represents no overlap
}
// If the segment represented by this node is completely inside the query range
if (l <= low && high <= r) {
return seg[ind];
}
int mid = (low + high)/2;
int left = query(2*ind+1,low,mid,l,r,seg);
int right = query(2*ind+2,mid+1,high,l,r,seg);
return Math.min(left,right);
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t > 0){
int n = sc.nextInt();
int arr[] = new int[n];
int seg[] = new int[4 * n];
for(int i = 0; i < n; i++) arr[i] = sc.nextInt();
buildtree(0,0,n-1,arr,seg);
System.out.println("Enter the range to query (l r):");
int l = sc.nextInt();
int r = sc.nextInt();
// Validate the input range
if (l < 0 || r >= n || l > r) {
System.out.println("Invalid range. Please ensure 0 <= l <= r < " + n);
} else {
// Query the segment tree
int result = query(0, 0, n - 1, l, r, seg);
System.out.println("Minimum value in range [" + l + ", " + r + "] is: " + result);
}
t--;
}
}
}