#include <bits/stdc++.h>
using
namespace
std;
void
updateBIT(
int
BITree[],
int
n,
int
index,
int
val)
{
index = index + 1;
while
(index <= n)
{
BITree[index] += val;
index += index & (-index);
}
}
int
*constructBITree(
int
arr[],
int
n)
{
int
*BITree =
new
int
[n+1];
for
(
int
i=1; i<=n; i++)
BITree[i] = 0;
for
(
int
i=0; i<n; i++)
updateBIT(BITree, n, i, arr[i]);
return
BITree;
}
int
getSum(
int
BITree[],
int
index)
{
int
sum = 0;
index = index + 1;
while
(index>0)
{
sum += BITree[index];
index -= index & (-index);
}
return
sum;
}
void
update(
int
BITree[],
int
l,
int
r,
int
n,
int
val)
{
updateBIT(BITree, n, l, val);
updateBIT(BITree, n, r+1, -val);
}
int
main()
{
int
arr[] = {0, 0, 0, 0, 0};
int
n =
sizeof
(arr)/
sizeof
(arr[0]);
int
*BITree = constructBITree(arr, n);
int
l = 2, r = 4, val = 2;
update(BITree, l, r, n, val);
int
index = 4;
cout <<
"Element at index "
<< index <<
" is "
<<
getSum(BITree,index) <<
"\n"
;
l = 0, r = 3, val = 4;
update(BITree, l, r, n, val);
index = 3;
cout <<
"Element at index "
<< index <<
" is "
<<
getSum(BITree,index) <<
"\n"
;
return
0;
}