-
Notifications
You must be signed in to change notification settings - Fork 0
/
GSS1.cpp
77 lines (72 loc) · 1.33 KB
/
GSS1.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<iostream>
#include<vector>
#include<limits>
using namespace std;
vector<int> arr;
vector<int> segTree;
int N;
void buildTree(int node, int a, int b)
{
if(node >= ((2*N) - 1))
return;
else if( a == b)
{
segTree[node] = arr[a];
return;
}
else
{
cout<<"Reached Here"<<"\n";
buildTree(((2*node) + 1), a, ((a+b)/2));
buildTree(((2*node) + 2), (((a+b)/2)+1), b);
if(segTree[((2*node) + 1)] >= segTree[((2*node) + 2)])
segTree[node] = segTree[((2*node) + 1)];
else
segTree[node] = segTree[((2*node) + 2)];
return;
}
}
int queryTree(int node, int a, int b, int i, int j)
{
if( i > b || a > j || node >= ((2*N) - 1))
return numeric_limits<int>::min();
else if( a >= i && b<=j )
return segTree[node];
else
{
int q1 = queryTree(((2*node) + 1), a, ((a+b)/2), i, j);
int q2 = queryTree(((2*node) + 2), (((a+b)/2)+1), b, i, j);
if(q1 >= q2)
return q1;
else
return q2;
}
}
int main()
{
cin>>N;
int value = 0;
for (int i = 0; i < N; ++i)
{
cin>>value;
arr.push_back(value);
}
for (int j = 0; j < ((2*N)-1); ++j)
{
segTree.push_back(0);
}
buildTree(0,0,N-1);
int M;
cin>>M;
cout<<"Segment tree"<<"\n";
for (int j = 0; j < ((2*N)-1); ++j)
cout<<segTree[j]<<" ";
cout<<"\n";
for (int i = 0; i < M; ++i)
{
int a=0, b=0;
cin>>a>>b;
cout<<queryTree(0,0,N-1,a-1,b-1)<<"\n";
}
return 0;
}