<html>
<HEAD></HEAD>
<BODY>
<textarea rows="50" cols="50">
/*****************
http://www.anycodes.cn/zh/
[[树状数组]线段数]
高效:log(n)
操作:位操作
思想:二分法
百度百科之外还有以下博客
http://dongxicheng.org/structure/binary_indexed_tree/
http://blog.****.net/int64ago/article/details/7429868#
t3
******************/
#include <iostream>
using namespace std;
int in[]={1,2,3,4,5,6,7,8,9};int n=9;
int lowbit0(int t)
{
return t & ( t ^ ( t - 1 ) );
}
int lowbit(int x)
{
return x&-x;
}
/**************
http://jinzhi.supfree.net/
再度复习内存与位操作
如 存3 为0000 0011
-3 1111 1101
按位与 0000 0001
**************/
//求前n项和
int sum(int end)
{ int sum = 0;
while(end > 0)
{
sum += in[end];
end -= lowbit(end);
}
return sum;
}
//增加某个元素的大小
void addx(int pos, int num)
{
while(pos <= n)
{
in[pos] += num;
pos += lowbit(pos);
}
}
void show()
{
for(int i=0;i<9;i++)
cout<<in[i]<<" ";
cout<<endl;
}
int main()
{
show();
addx(2,2);
show();
cout<<sum(5);
return 0;
}
/****
对结果13还是有点疑问
***/
</textarea>
<textarea rows="50" cols="50"> </textarea>
</BODY>
</html>