学习笔记:树状数组
int lowbit(int x)
{
return x&(-x);
}
void update(int i, int k)
{
while(i <= n)
{
c[i] += k;
i += lowbit(i);
}
}
int getsum(int i)
{
int res = 0;
while (i > 0)
{
res += c[i];
i -= lowbit(i);
}
return res;
}
void update(int i, int k)
{
int x = i;
while(i <= n)
{
sum1[i] += k;
sum2[i] += k * (x - 1);
i += lowbit(i);
}
}
int getsum(int i)
{
int x = i;
int res = 0;
while (i > 0)
{
res += sum1[i] * x - sum2[i];
i -= lowbit(i);
}
return res;
}