树状数组

b站视频 : 【ACM】树状数组与ST表

int a[10005];
int c[10005];
int n;

int lowbit(int x){
	return x&(-x);
}

int getSum(int x){
	int ans = 0;
	while(x > 0){
		ans += c[x];
		x -= lowbit(x);
	}
	return ans;
}

void update(int x, int value){
	a[x] += value;
	while(x <= n){
		c[x] += value;
		x += lowbit(x);
	}
}
class BIT {
private:
    vector<int> tree;
    int n;

public:
    BIT(int _n) : n(_n), tree(_n + 1) {}

    static constexpr int lowbit(int x) {
        return x & (-x);
    }

    void update(int x, int d) {
        while (x <= n) {
            tree[x] += d;
            x += lowbit(x);
        }
    }

    int query(int x) const {
        int ans = 0;
        while (x) {
            ans += tree[x];
            x -= lowbit(x);
        }
        return ans;
    }
};
posted @ 2020-11-16 13:58  miyanyan  阅读(43)  评论(0编辑  收藏  举报