树状数组

树状数组

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

#include <bits/stdc++.h>

#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);
}
posted @ 2020-06-23 18:22  Waitti  阅读(92)  评论(0编辑  收藏  举报