Algorithm - the number of exchanges insertion sort - Binary Indexed Tree

// 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);
  }
}

Guess you like

Origin www.cnblogs.com/jffun-blog/p/12046221.html