/
**
*树状数组 ( Binary Indexed Tree)
*时间复杂度:
log(n)
*数组下标必须从
1 开始!
*Max为范围
*Inssert(
int pos,
int val):
pos位置增加val
*Sum(
int pos) 计算从
1 ~
pos 所有元素的和
*/
int Max
long long c[Max];
int lowbit(
int x)
{
return x&(-
1*x);
}
void Insert(
int pos,
int val)
{
while(
pos<=Max)
{
c[Max]+=val;
pos+=lowbit(
pos);
}
}
long long Sum(
int pos)
{
long long ans =
0;
while(
pos>
0)
{
ans+=c[
pos];
pos-=lowbit(
pos);
}
return ans;
}
如有问题请联系QQ:826409960
转载请注明原文地址: https://www.6miu.com/read-24754.html