forked from Amisha-here/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegment_tree.cpp
More file actions
61 lines (55 loc) · 1.05 KB
/
segment_tree.cpp
File metadata and controls
61 lines (55 loc) · 1.05 KB
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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll s[4010],n,ar[1010];
void build(int id,int l,int r)
{
if(r-l<2)
{
s[id]=ar[l];
return;
}
int mid=(l+r)/2;
build(id*2+1,l,mid);
build(id*2+2,mid,r);
s[id]=s[id*2+1]+s[id*2+2];
}
// prints sum of elements [x,y)
int sum(int x,int y,int id=0,int l=0,int r=n)
{
if(x>=r || y<=l)
return 0;
if(x<=l && y>=r)
return s[id];
int mid=(l+r)/2;
return sum(x,y,id*2+1,l,mid) + sum(x,y,id*2+2,mid,r);
}
void modify(int p,int x,int id = 0,int l = 0,int r = n)
{
s[id] += x - ar[p];
if(r - l < 2)
{
ar[p] = x;
return ;
}
int mid = (l + r)/2;
if(p < mid)
modify(p, x, id*2+1, l, mid);
else
modify(p, x, id*2+2, mid, r);
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
cin>>ar[i];
build(0,0,n);
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
cout<<"From: "<<i<<" to: "<<j<<" = "<<sum(i,j+1)<<endl;
}
}
return 0;
}