lowbit(x) 为x的二进制表达式中最右边的1所对应的值
比如:38228的二进制是1001010110010000,所以lowbit(38228) = 16;(二进制是10000)
lowbit(x) = x&(-x);计算机里的整数采用补码表示因此-x实际上是x按位取反,末尾加1以后的结果
38288 = 1001010110010000
-38288 = 0110101001110000
二者按位取"与"之后,前面的部分变0,之后lowbit保持不变
sum[1] = a[1];
sum[2] = a[2];
sum[3] = a[3];
sum[4] = a[1] + a[2] + a[3] + a[4];
sum[5] = a[5];
sum[6] = a[5] + a[6];
sum[7] = a[7];
sum[8] = a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7] + a[8];
int lowbit(int x)
{
return x&(-x);
}
int Getsum(int pos)
{
int res = 0;
while(pos > 0)
{
res += a[pos];
pos -= lowbit(pos);
}
return res;
}
void add(int pos,int num)
{
while(pos <= n)
{
a[pos] += num;
pos += lowbit(pos);
}
}