-
Notifications
You must be signed in to change notification settings - Fork 0
/
segmentTree.cpp
129 lines (116 loc) · 3.31 KB
/
segmentTree.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
//function to build segment tree for RangeSum query
void build_rangesum_tree(ll* t, ll n) {
ll i;
for(ll i=n-1; i>0; i--) {
t[i] = t[i<<1] + t[i<<1|1]; //t[i] = t[2*i] + t[2*i + 1];
}
/*for(i=1; i<2*n; i++)
cout<<t[i]<<" ";
cout<<endl;*/
}
//function to build segment tree for RangeMin query
void build_rangemin_tree(ll* t_min, ll n) {
ll i;
for(i=n-1; i>0; i--) {
t_min[i] = min(t_min[i<<1], t_min[i<<1|1]);
}
/*for(i=1; i<2*n; i++)
cout<<t_min[i]<<" ";
cout<<endl;*/
}
//function to build segment tree for RangeMax query
void build_rangemax_tree(ll* t_max, ll n) {
ll i;
for(i=n-1; i>0; i--) {
t_max[i] = max(t_max[i<<1], t_max[i<<1|1]);
}
/*for(i=1; i,2*n; i++)
cout<<t_max[i]<<" ";
cout<<endl;*/
}
//returns the sum in the range [l,r)
ll range_sum(ll* t, ll n, ll l, ll r) {
ll sum=0;
for(l+=n, r+=n; l<r; l>>=1, r>>=1) {
if(l & 1)
sum += t[l++];
if(r & 1)
sum += t[--r];
}
return sum;
}
//returns the minimum element in the range [l,r)
ll range_min(ll* t, ll n, ll l, ll r) {
ll minim = INT_MAX;
for(l+=n, r+=n; l<r; l>>=1, r>>=1) {
if(l & 1)
minim = min(minim, t[l++]);
if(r & 1)
minim = min(minim, t[--r]);
}
return minim;
}
//returns the maximum element in the range [l,r)
ll range_max(ll* t, ll n, ll l, ll r) {
ll maxim = INT_MIN;
for(l+=n, r+=n; l<r; l>>=1, r>>=1) {
if(l & 1)
maxim = max(maxim, t[l++]);
if(r & 1)
maxim = max(maxim, t[--r]);
}
return maxim;
}
//changes the value of array[index] to key and modifies the tree accordingly
void modify(ll* t, ll* t_min, ll* t_max, ll n, ll index, ll key) {
index += n;
t[index] = t_min[index] = t_max[index] = key;
for(; index>1; index>>=1) {
t[index>>1] = t[index] + t[index^1];
t_min[index>>1] = min(t_min[index], t_min[index^1]);
t_max[index>>1] = max(t_max[index], t_max[index^1]);
}
}
int main() {
ll i,n,l,h,val;
int ch;
cout<<"Enter n\n";
cin>>n;
cout<<"Enter array\n";
ll t[2*n + 1], t_min[2*n + 1], t_max[2*n + 1];
for(i=0;i<n;i++) {
cin>>t[n+i];
t_min[n+i] = t_max[n+i] = t[n+i];
}
build_rangesum_tree(t,n);
build_rangemin_tree(t_min, n);
build_rangemax_tree(t_max, n);
while(1) {
cout<<"1. RangeSum[l,h) 2. RangeMin[l,h) 3. RangeMax[l,h) 4. Modify 5. Exit\n";
cin>>ch;
switch(ch) {
case 1: cout<<"Enter l and h\n";
cin>>l>>h;
cout<<range_sum(t,n,l,h)<<"\n";
break;
case 2: cout<<"Enter l and h\n";
cin>>l>>h;
cout<<range_min(t_min,n,l,h)<<"\n";
break;
case 3: cout<<"Enter l and h\n";
cin>>l>>h;
cout<<range_max(t_max,n,l,h)<<"\n";
break;
case 4: cout<<"Enter index and new value\n";
cin>>i>>val;
modify(t,t_min,t_max,n,i,val);
break;
}
if(ch == 5)
break;
}
return 0;
}