fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. std::pair<int,int> segment_of(int index, int size) {
  5. int depth = log2(size - index);
  6. int segment_size = 1 + size / (2 << depth);
  7. int position = (2 << depth) - size + index - 1;
  8.  
  9. return {position * segment_size, (position + 1) * segment_size - 1};
  10. }
  11.  
  12. int main() {
  13. auto segment = segment_of(9, 15);
  14.  
  15. std::cout << '[' << segment.first << ',' << segment.second << ']';
  16.  
  17. return 0;
  18. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
[2,3]