-
Notifications
You must be signed in to change notification settings - Fork 0
/
Segment_tree.c
95 lines (65 loc) · 866 Bytes
/
Segment_tree.c
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
#include <stdio.h>
void build(int t[],int n)
{
int i;
for(i=n-1;i>0;i--)
t[i]=t[2*i]+t[2*i+1];
}
void modify(int p,int x,int t[],int n)
{
p+=n;
t[p]=x;
while(p>1)
{
t[p/2]=t[p]+t[p^1];
p>>=1;
}
}
int query(int l,int r,int t[],int n)
{
int sum=0;
l+=n;
r+=n;
while(l<r)
{
if(l&1)
{
sum+=t[l];
l++;
}
if(!(r&1))
{
sum+=t[r];
r--;
}
l>>=1;
r>>=1;
}
sum+=t[l];
return sum;
}
int main()
{
int n,i;
printf("Array size : ");
scanf("%d",&n);
int t[2*n];
printf("Enter array elements\n");
for(i=n;i<2*n;i++)
scanf("%d",&t[i]);
build(t,n);
for(i=1;i<2*n;i++)
printf("%d ",t[i]);
printf("\nNow enter queries\n");
while(1)
{
int x,y,z;
scanf("%d %d %d",&x,&y,&z);
if(x==1)
{
modify(y,z,t,n);
}
else
printf("query [%d,%d] : %d\n",y,z,query(y,z,t,n));
}
}