【存模板】树状数组

一维

int lowbit(int x){
    return x&-x;
}
int sum(int x){
    int ret=0;
    while(x){
        ret+=c[x];
        x-=lowbit(x);
    }
    return ret;
}
void update(int x,int d){
    while(x<=n){
        c[x]+=d;
        x+=lowbit(x);
    }
}

二维

inline int lowbit(int x){
    return x&-x;
}
void update(int x,int y,int d){
    for(;x<=s;x+=lowbit(x))
        for(int j=y;j<=s;j+=lowbit(j))
            c[x][j]+=d;
}
int getsum(int x,int y){
    int ret=0,j;
    for(;x;x-=lowbit(x))
        for(j=y;j;j-=lowbit(j))
            ret+=c[x][j];
    return ret;
}
posted @ 2015-09-15 21:01  outer_form  阅读(125)  评论(0编辑  收藏  举报