PKU 2309 BST

  • 左/右に降りる場合のビットの変換にパターンがある
  • それをそのまま書けば良い
  • ビット演算のみで一行で書くこともおそらく出来る
int main() {
	int numberOfTestCases;
	cin >> numberOfTestCases;

	for (int caseIndex = 0; caseIndex < numberOfTestCases; ++caseIndex) {
		int N;
		cin >> N;

		if (N & 1) {
			cout << N << " " << N << endl;
		} else {
			int shift = 0;
			while (((1 << shift) & N) == 0) {
				++shift;
			}

			cout << ((N & (-1 ^ ((1 << (shift + 1)) - 1))) | 1) << " " << (N | ((1 << shift) - 1)) << endl;
		}
	}
}