#include <iostream>
#include <cmath>
std::pair<int,int> segment_of(int index, int size) {
int depth = log2(size - index);
int segment_size = 1 + size / (2 << depth);
int position = (2 << depth) - size + index - 1;
return {position * segment_size, (position + 1) * segment_size - 1};
}
int main() {
auto segment = segment_of(9, 15);
std::cout << '[' << segment.first << ',' << segment.second << ']';
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y21hdGg+CgpzdGQ6OnBhaXI8aW50LGludD4gc2VnbWVudF9vZihpbnQgaW5kZXgsIGludCBzaXplKSB7CglpbnQgZGVwdGggPSBsb2cyKHNpemUgLSBpbmRleCk7CglpbnQgc2VnbWVudF9zaXplID0gMSArIHNpemUgLyAoMiA8PCBkZXB0aCk7CglpbnQgcG9zaXRpb24gPSAoMiA8PCBkZXB0aCkgLSBzaXplICsgaW5kZXggLSAxOwoJCglyZXR1cm4ge3Bvc2l0aW9uICogc2VnbWVudF9zaXplLCAocG9zaXRpb24gKyAxKSAqIHNlZ21lbnRfc2l6ZSAtIDF9Owp9CgppbnQgbWFpbigpIHsKCWF1dG8gc2VnbWVudCA9IHNlZ21lbnRfb2YoOSwgMTUpOwoJCglzdGQ6OmNvdXQgPDwgJ1snIDw8IHNlZ21lbnQuZmlyc3QgPDwgJywnIDw8IHNlZ21lbnQuc2Vjb25kIDw8ICddJzsKCQoJcmV0dXJuIDA7Cn0=