-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment_tree.cpp
85 lines (64 loc) · 1.6 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
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
#include <bits/stdc++.h>
using namespace std;
//the code of array is index for one
void construct_tree(int input[],int segment[], int left ,int right , int pos)
{
if (left == right)
{
segment[pos] = input[left];
return;
}
else
{
int mid = (left + right) / 2;
construct_tree(input,segment,left,mid,2*pos);// (left + mid / 2)
construct_tree(input,segment,mid+1,right,2*pos+1);// (mid+1 + right / 2)
segment[pos] = max(segment[2*pos],segment[2*pos+1]);//change,min,max
}
}
int range_max(int segment[],int range_left,int range_right,int left,int right,int pos)
{
if (range_left <= left && range_right >= right)//overlaps
{
return segment[pos];
}
if (range_left > right || range_right < left)//no overlap
{
return INT_MIN;
}
else
{
int mid = (left + right) / 2;
return max(range_max(segment , range_left , range_right , left , mid , 2*pos), range_max(segment , range_left , range_right ,mid+1 ,right ,2*pos+1));
}
}
int main()
{
#ifdef DBG
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
#endif
int len;
scanf("%d",&len);//scan the length
int input[len];
for (int i = 1; i <= len; ++i)
{
scanf("%d",&input[i]);
}
int segment[4*len];//4 length is definetly enough
fill(segment,segment+4*len,0);
int rightmost = 0;
if ((int)(log(len)/log(2)) < log(len)/log(2) )
{
rightmost = (int)(log(len)/log(2)) + 1;
}
else
{
rightmost = (int)(log(len)/log(2));
}
construct_tree(input,segment,0,(1<<rightmost),1);
int left,right;
while(scanf("%d %d",&left,&right) != EOF)
printf("max:%d\n",range_max(segment,left,right,0,(1<<rightmost),1));
return 0;
}