forked from kanika2107/Advanced-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment_tree.cpp
57 lines (56 loc) · 1.3 KB
/
segment_tree.cpp
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
#include<bits/stdc++.h>
using namespace std;
int size=0;
int construct_tree(int segment_tree[100000],int current_index,int start,int end,int arr[100000])
{
if(start==end)
{
if(size<current_index)
size=current_index;
segment_tree[current_index]=arr[start];
return arr[start];
}
int mid=(start+end)/2;
segment_tree[current_index]=construct_tree(segment_tree,2*current_index+1,start,mid,arr)+construct_tree(segment_tree,2*current_index+2,mid+1,end,arr);
return segment_tree[current_index];
}
int getsum(int segment_tree[100000],int start,int end,int left,int right,int current_index)
{
if(start>=left && end<=right)
{
return segment_tree[current_index];
}
if(end<left || right<start)
{
return 0;
}
int mid=(start+end)/2;
return getsum(segment_tree,start,mid,left,right,2*current_index+1)+getsum(segment_tree,mid+1,end,left,right,2*current_index+2);
}
int main()
{
int n;
cin >> n;
int arr[100000];
for(int i=0;i<n;i++)
{
cin >> arr[i];
}
int segment_tree[100000]={0};
construct_tree(segment_tree,0,0,n-1,arr);
for(int i=0;i<=size;i++)
{
cout << segment_tree[i] << " ";
}
cout << "\n";
int left,right;
cin >> left >> right;
if(left<0 || right>n-1 || left>right)
{
cout << "invalid input";
return 0;
}
int ans=getsum(segment_tree,0,n-1,left,right,0);
cout << ans;
return 0;
}