单点修改,区间查询Code
int lowbit(int x)
{
return (x&(-x));
}
void add(int w,int x)
{
while(w <= n)
{
c[w] += x;
w += lowbit(w);
}
}
int getsum(int w)
{
int res=0;
while(w)
{
res += c[w];
w -= lowbit(w);
}
return res;
}
区间修改,区间查询Code
int lowbit(int x)
{
return x & -x;
}
void add(int x, int val)
{
ll val2 = 1ll * val * x;
while(x <= n)
{
c1[x] += val;
c2[x] += val2;
x += lowbit(x);
}
}
ll getsum1(int x)
{
ll res = 0;
while(x)
{
res += c1[x];
x -= lowbit(x);
}
return res;
}
ll getsum2(int x)
{
ll res = 0;
while(x)
{
res += c2[x];
x -= lowbit(x);
}
return res;
}
ll getsum(int l, int r)
{
ll res1 = getsum1(r) * (r + 1) - getsum1(l-1) * l;
ll res2 = getsum2(r) - getsum2(l-1);
return res1 - res2;
}