版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq1965610770/article/details/81388387
原版::
const int N = 100100;
int sz[N];
int lowbit(itn x)
{
return x&(-x);
}
//更新节点
void update(int x,int d)
{
while(x < N)
{
sz[x] = sz[x]+d;
x = x+lowbit(x);
}
}
//区间和 [0,x]
int getsum(int x)
{
int res = 0;
while(x > 0)
{
res = res+sz[x];
x = x-lowbit(x) ;
}
return res;
}
区间修改,单点查询::
//定义为 a[i] = c1[i]-c1[i-1]
//则 a[i] = c1[0]+ca[i]+...+c1[i]
const int N = 100100;
int a[N],c1[N];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int d)
{
while(x < N)
{
c1[x] = c1[x]+d;
x = x+lowbit(x);
}
}
int getsum(int x)
{
int res = 0;
while(x > 0)
{
res = res+c1[x];
x = x-lowbit(x) ;
}
return res;
}
/*
*输入处理
* update(i,a[i]-a[i-1]);
*
*修改区间 [l,r] + ad
* update(l,ad);
* update(r+1,-ad);
*
*查询位置 x 的值
* x = getsum(x);
*
*/
区间修改,区间查询::
//定义 c1[i] = a[i]-a[i-1];
//则 a[i] = c1[0]+c1[1]+...+c1[i]
//由 sum(1,k) = c1[1]+
// (c1[1]+c1[2])+
// (c1[1]+c1[2]+c1[3])+
// ...+
// (c1[1]+c1[2]+c1[3]+...+c1[k])
//可知sum(1,k) = k*(c1[1]+...+c1[k])-
// (0*c1[1] + 1*c1[2] + 2*c1[3] +...+ (k-1)*c1[k])
//则定义c2[i] = (i-1)*c1[i];const int N = 100100;
int a[N],c1[N],c2[N];
int lowbit(itn x)
{
return x&(-x);
}
void update(int *q,int x,int d)
{
while(x < N)
{
c1[x] = c1[x]+d;
x = x+lowbit(x);
}
}
int getsum(int *q,int x)
{
int res = 0;
while(x > 0)
{
res = res+c1[x];
x = x-lowbit(x) ;
}
return res;
}
int sum(int x)
{
int ans1,ans2;
ans1 = x*getsum(c1,x);
ans2 = getsum(c2,x);
return ans1 - ans2;
}
int query(int l,int r)
{
int ans1,ans2;
ans1 = sum(y);
ans2 = sum(x-1);
return ans1 - ans2;
}
/*
*输入处理
* update(c1,i,a[i]-a[i-1]);
* update(c2,i,(i-1)*(a[i]-a[i-1]));
*
*修改区间 [l,r] + ad
* update(c1,l,ad);
* update(c1,r+1,-ad);
* update(c2,l,(l-1)*ad);
* update(c2,r+1,-r*ad);
*
*查询区间和 [l,r]
* query(l,r);
*/