树状数组扩展

一维区间加减、单点查询
<pre name="code" class="cpp">const int N = 10000;
int n = 10;
int a[N],s[N] = {0};
int lowbit(int x){return (x&(-x));}
int sum(int * a,int x)
{
    int res = 0;
    for(int i = x;i > 0; i -= lowbit(i)) res += a[i];
    return res;
}
void update1(int *a,int x,int w){
    for(int i = x;i <= n; i += lowbit(i))
        a[i] += w;
}
void update(int x,int y,int v)//在区间x,y加减一个值v
{
    update1(s,x,v);
    update1(s,y + 1,-v);
}
int query(int x)
{
    return sum(s,x);
}


 一维区间加减,区间查询 

const int N = 10000;
int d[N],s[N];
int lowbit(int x){return (x&(-x));}
int n = 10;
int sum(int * a,int x)
{
    int res = 0 ;
    for(int i = x;i > 0; i -= lowbit(i)) res += a[i];
    return res;
}
void update1(int *a,int x,int w){
    for(int i = x;i <= n; i += lowbit(i))
        a[i] += w;
}
void update(int x,int y,int v)//在区间x,y加减一个值v
{
    update1(s,x,v);
    update1(s,y + 1,-v);
    update1(d,x,v*x);
    update1(d,y + 1,-v*(y + 1));

}
int query(int x,int y) //查询区间和
{
    return (y + 1) * sum(s,y) - sum(d,y) - ((x ) * sum(s,x - 1) - sum(d,x - 1));
}

矩阵区间加减,单点查询

const int N = 10000;
int d[N][N],s[N][N];
int lowbit(int x){return (x&(-x));}
int n = 10;
int sum(int a[][N],int x,int y)
{
    int res = 0 ;
    for(int i = x; i > 0; i -= lowbit(i))
        for(int j = y; j > 0; j -= lowbit(j))
            res += a[i][j];
    return res;
}
void update(int a[][N],int x,int y,int v)
{
    for(int i = x; i <= n; i += lowbit(i))
        for(int j = y; j <= n; j += lowbit(j))
            a[i][j] += v;
}
void add(int x1,int y1,int x2,int y2,int v)
{
    update(s,x1,y1,v);
    update(s,x1,y2 + 1,-v);
    update(s,x2 + 1,y1,-v);
    update(s,x2 + 1,y2 + 1,v);
}
int query(int x,int y)
{
    return sum(s,x,y);
}

矩阵区间加减,区间求和

const int N = 100;
int s[N][N],d1[N][N],d2[N][N],d3[N][N];
int n = 10;
inline int lowbit(int x) {return x&(-x);}
int sum(int a[][N],int x,int y)
{
    int res = 0;
    for(int i = x; i > 0; i -= lowbit(i))
        for(int j = y; j > 0; j -= lowbit(j))
            res += a[i][j];
    return res;
}
void gp(int a[][N],int x,int y,int v)
{
    for(int i = x; i <= n; i += lowbit(i))
        for(int j = y; j <= n; j += lowbit(j))
            a[i][j] += v;
}
void add(int x1,int y1,int x2,int y2,int w)
{
    gp(s,x1,y1,w);
    gp(s,x2+1,y1,-w);
    gp(s,x1,y2+1,-w);
    gp(s,x2+1,y2+1,w);
    gp(d1,x1,y1,w*x1);
    gp(d1,x2+1,y1,-w*(x2+1));
    gp(d1,x1,y2+1,-w*x1);
    gp(d1,x2+1,y2+1,w*(x2+1));
    gp(d2,x1,y1,w*y1);
    gp(d2,x2+1,y1,-w*y1);
    gp(d2,x1,y2+1,-w*(y2+1));
    gp(d2,x2+1,y2+1,w*(y2+1));
    gp(d3,x1,y1,w*x1*y1);
    gp(d3,x2+1,y1,-w*(x2+1)*y1);
    gp(d3,x1,y2+1,-w*x1*(y2+1));
    gp(d3,x2+1,y2+1,w*(x2+1)*(y2+1));
}

int getsum(int x2,int y2)
{
    return (x2 + 1)*(y2 + 1)*sum(s,x2,y2) -
           (x2 + 1)*sum(d2,x2,y2) - (y2 + 1)*sum(d1,x2,y2) +
            sum(d3,x2,y2);

}
int query(int x1,int y1,int x2,int y2)
{
     return getsum(x2,y2) - getsum(x1 - 1,y2) - getsum(x2,y1 - 1) + getsum(x1 - 1,y1 - 1);

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值