Skip to content

Commit 8762929

Browse files
committed
Added range sum query and sum of a range [l,r] for segment tree!
1 parent 020490a commit 8762929

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include<bits/stdc++.h>
2+
#define ll long long;
3+
using namespace std;
4+
5+
int s[100];
6+
int sn=INT_MIN;
7+
8+
int build(int v[],int s[],int pos,int start,int end)
9+
{
10+
if(start==end)
11+
{s[pos]=v[start];
12+
cout<<pos<<"#"<<s[pos]<<" ";
13+
if(pos>sn)
14+
sn=pos;
15+
return s[pos];}
16+
// cout<<pos<<" ";
17+
int mid=(start+end)/2;
18+
19+
s[pos]=min(build(v,s,2*pos+1,start,mid),
20+
build(v,s,2*pos+2,mid+1,end));
21+
if(pos>sn)
22+
sn=pos;
23+
cout<<pos<<"#"<<s[pos]<<" ";
24+
return s[pos];
25+
}
26+
27+
int buildtree(int v[],int n)
28+
{
29+
// cout<<v.size()-1;
30+
build(v,s,0,0,n-1);
31+
return 0;
32+
}
33+
34+
void disp(int n)
35+
{
36+
// vector<int>::iterator i;
37+
int i=0;
38+
for(i=0;i!=2*n+5;i++)
39+
// for(i=0;i<sizeof(t)/sizeof(int);i++)
40+
cout<<s[i]<<" ";
41+
}
42+
43+
int rmq(int qlow,int qhigh,int high,int low,int pos)
44+
{
45+
if(qlow<=low&&qhigh>=high)//the query overlaps the segment tree nodes
46+
return s[pos];
47+
if(qlow>high||qhigh<low)//no overlap
48+
return INT_MAX;
49+
int mid=(high+low)/2;
50+
return min(rmq(qlow,qhigh,mid,low,2*pos+1),rmq(qlow,qhigh,high,mid+1,2*pos+2));
51+
}
52+
53+
int main()
54+
{
55+
int n=0,i=0,a=0,v[100],qlow=0,qhigh=0;
56+
cin>>n;
57+
for(i=0;i<n;i++)
58+
cin>>v[i];
59+
buildtree(v,n);
60+
cout<<endl;
61+
disp(n);
62+
cout<<endl;
63+
cin>>qlow>>qhigh;
64+
cout<<rmq(qlow,qhigh,n-1,0,0);
65+
return 0;
66+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include<bits/stdc++.h>
2+
#define ll long long;
3+
using namespace std;
4+
5+
int s[100];
6+
7+
int build(int v[],int s[],int pos,int start,int end)
8+
{
9+
if(start==end)
10+
{s[pos]=v[start];
11+
cout<<pos<<"#"<<s[pos]<<" ";
12+
return s[pos];}
13+
// cout<<pos<<" ";
14+
int mid=(start+end)/2;
15+
16+
s[pos]=(build(v,s,2*pos+1,start,mid)+
17+
build(v,s,2*pos+2,mid+1,end));
18+
cout<<pos<<"#"<<s[pos]<<" ";
19+
return s[pos];
20+
}
21+
22+
int buildtree(int v[],int n)
23+
{
24+
// cout<<v.size()-1;
25+
build(v,s,0,0,n-1);
26+
return 0;
27+
}
28+
29+
void disp(int n)
30+
{
31+
// vector<int>::iterator i;
32+
int i=0;
33+
for(i=0;i!=2*n+5;i++)
34+
// for(i=0;i<sizeof(t)/sizeof(int);i++)
35+
cout<<s[i]<<" ";
36+
}
37+
38+
int sum(int qlow,int qhigh,int high,int low,int pos)
39+
{
40+
if(qlow<=low&&qhigh>=high)//the query overlaps the segment tree nodes
41+
return s[pos];
42+
if(qlow>high||qhigh<low)//no overlap
43+
return 0;
44+
int mid=(high+low)/2;
45+
return (sum(qlow,qhigh,mid,low,2*pos+1)+sum(qlow,qhigh,high,mid+1,2*pos+2));
46+
}
47+
48+
void updatetree(int d,int i,int low,int high,int pos)
49+
{
50+
if(i<low||i>high)
51+
return;
52+
s[pos]+=d;
53+
if(low!=high){
54+
int mid=(low+high)/2;
55+
updatetree(d,i,low,mid,2*pos+1);
56+
updatetree(d,i,mid+1,high,2*pos+2);
57+
}
58+
}
59+
60+
int update(int n,int a,int b)
61+
{
62+
updatetree(a,b,0,n-1,0);
63+
return 0;
64+
}
65+
66+
int main()
67+
{
68+
int n=0,i=0,a=0,v[100],qlow=0,qhigh=0,b=0;
69+
cin>>n;
70+
for(i=0;i<n;i++)
71+
cin>>v[i];
72+
buildtree(v,n);
73+
cout<<endl;
74+
disp(n);
75+
cout<<endl;
76+
cin>>qlow>>qhigh;
77+
cout<<sum(qlow,qhigh,n-1,0,0);
78+
cout<<"\nenter new value and index(0th based) to update!";
79+
cin>>a>>b;
80+
update(n,a-v[b],b);
81+
cout<<endl;
82+
disp(n);
83+
return 0;
84+
}

0 commit comments

Comments
 (0)