0 Members and 1 Guest are viewing this topic.
// Note that indices start at 1// Get sum from elements 1 to kint get_prefix_sum(int k) { int sum=0; for(; k; k-=(k&-k)) sum += array[k-1]; return sum;}// Add n to element kvoid add_to_element(int k, int n) { for(; k<=array_size; k+=(k&-k)) array[k-1] += n;}
// Sum from element a to bint range_sum(int a, int b) { return get_prefix_sum(b) - get_prefix_sum(a-1);}// Get element kint get_element(int k) { return range_sum(k,k);}// Set element k to value nvoid set_element(int k, int n) { add_to_element(k, n - get_element(k));}