-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegmentTree.java
More file actions
45 lines (35 loc) · 995 Bytes
/
segmentTree.java
File metadata and controls
45 lines (35 loc) · 995 Bytes
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
import java.util.*;
class SegmentTree
{
static int ST[];
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
int n=scan.nextInt();
int x=(int)Math.ceil(Math.log(n)/Math.log(2));
int size=2*(int)Math.pow(2,x)-1;
System.out.println(size);
ST=new int[size];
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=scan.nextInt();
}
Create_Tree(arr,0,n-1,0);
for(int i=0;i<size;i++)
{
System.out.println(i+" "+ST[i]);
}
}
static int Create_Tree(int arr[],int start,int end,int index)
{
if(start==end)
{
ST[index]=arr[start];
return ST[index];
}
int mid=(start+end)/2;
ST[index]= Create_Tree(arr,start,mid,index*2+1)+Create_Tree(arr,mid+1,end,index*2+2);
return ST[index];
}
}