zjffun blog

插入排序交换次数 - Binary Indexed Tree

更新于 写于 算法

c
// get cumulative sum up to and including i
int Get(int i) {
  int res = 0;
  while(i) {
    res += B[i];
    i -= (i & -i);
  }
  return res;
}

// add val to value at i
void Set(int i, int val) {
  while(i <= N) {
    B[i] += val;
    i += (i & -i);
  }
}