-
Notifications
You must be signed in to change notification settings - Fork 0
/
303. Range Sum Query - Immutable.c
83 lines (71 loc) · 1.6 KB
/
303. Range Sum Query - Immutable.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
83
typedef struct {
int data;
} NumArray;
NumArray* numArrayCreate(int* nums, int numsSize) {
NumArray *obj=malloc(sizeof(NumArray)*numsSize);
for (int i=0;i<numsSize;i++) {
(obj+i)->data=*(nums+i);
}
return obj;
}
int numArraySumRange(NumArray* obj, int i, int j) {
int returnint=0;
for (int n=i;n<=j;n++) {
returnint+=(*(obj+n)).data;
}
return returnint;
}
void numArrayFree(NumArray* obj) {
free(obj);
}
/**
* Your NumArray struct will be instantiated and called as such:
* struct NumArray* obj = numArrayCreate(nums, numsSize);
* int param_1 = numArraySumRange(obj, i, j);
* numArrayFree(obj);
*/
/** C faster
typedef struct {
int *BIT;
} NumArray;
int getSum(int *BIT, int i)
{
i = i + 1;
int sum = 0;
while(i > 0){
sum += BIT[i];
i -= (i & (-i));
}
return sum;
}
void UpdateBIT(int *BIT, int val, int i, int n)
{
int index = i + 1;
while(index <= n){
BIT[index] += val;
index += (index & (-index));
}
}
int *constructBITree(int *nums, int n)
{
int *BIT = calloc(n + 1, sizeof(int));
for(int i = 0; i < n; i++)
UpdateBIT(BIT, nums[i], i, n);
return BIT;
}
NumArray* numArrayCreate(int* nums, int numsSize) {
if(numsSize == 0) return NULL;
NumArray *na = (NumArray*)malloc(sizeof(NumArray));
na->BIT = constructBITree(nums, numsSize);
return na;
}
int numArraySumRange(NumArray* obj, int i, int j) {
return getSum(obj->BIT, j) - getSum(obj->BIT, i - 1);
}
void numArrayFree(NumArray* obj) {
if(obj && obj->BIT){
free(obj->BIT);
free(obj);
}
}
*/