树状数组

树状数组

本博客仅贴出树状数组模板

#include 

#define lowbit(x) (x & -x)

using namespace std;

const int N = 10010;
int a[N], n;

//a[x] += c
void insert(int x, int c){
      for(;x <= n; x += lowbit(x))a[x] += c;
}

// sum([1, x])
int get(int x){
      int res = 0;
      for(;x > 0; x -= lowbit(x))res += a[x];
      return res;
}

int main(){
      int t;
      cin >> n;
      for(int i = 1;i <= n; i++)cin >> t, insert(i, t);
}

你可能感兴趣的:(树状数组)