-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBIT.c
70 lines (52 loc) · 1.07 KB
/
BIT.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
#include<stdio.h>
#include<stdlib.h>
int lower_bit(int n){
return n&-n;
}
int getSum(int *BITree, int index){
int sum = 0;
index += 1;
while(index > 0){
sum += BITree[index];
index -= lower_bit(index);
}
return sum;
}
void updateBIT(int *BITree, int length, int index, int value){
index += 1;
while(index<=length){
BITree[index] += value;
index += lower_bit(index);
}
}
/*
int query(int a, int b){
if(a > b)
swap(a, b);
return sum(b) - sum(a-1);
}
*/
int *constructBITree(int arr[], int n){
int *BITree = (int *)malloc(n * sizeof(int));
int i;
for(i=1 ; i<=n ; i++)
BITree[i] = 0;
for(i=0 ; i<n ; i++)
updateBIT(BITree, n, i, arr[i]);
return BITree;
}
int main(void)
{
int array[] = {10, 25, 22, 7, 34, 2, 9, 12, 16, 16};
int length = sizeof(array)/sizeof(array[0]);
int i;
for(i=0 ; i<length ; i++)
printf("%d, ", array[i]);
printf("\n");
int *arr2 = constructBITree(array, length);
for(i=0 ; i<=length ; i++)
printf("%d, ", arr2[i]);
printf("\n");
printf("Sum of elements in arr[0..5] is %d\n", getSum(arr2, 5));
return 0;
}