苹果

Loading

树状数组

1.lowbit x & (-x)

2.C[x] [x - lowbit(x) + 1 , x]的区间和

   用处:l->r的和 

   eg.25(10)   = 11001(2)

       ① ans += C[25]

       ② 25 -= lowbit(25)

       ③ ans += C[24]

       ④ 24 -= lowbit(24)

       ⑤ ans += C[16]

       ⑥ 16 -= lowbit(16) = 0;

C[]修改

        对于所有y 若x∈[y - lowbit(y) + 1,y] 则C[y] += v;

        [y - lowbit(y) + 1,y]:影响范围

int ask(int x){
    int res = 0;
    while(x) res += c[x], x -= lowbit(x);
    return res;
}
void add(int x,int v){
    while(x <= n){
        c[x] += v;
        x += lowbit(x);
    }
}

 

posted @ 2023-02-01 11:14  CatalinaQ  阅读(30)  评论(0)    收藏  举报