Test instructions is to give you a full two fork tree, give a number, and the maximum and minimum values of the tree with this number root.
Understand the use of lowbit in a tree array.
Say this before I say a thing called Lowbit, Lowbit (k) is the K of the binary high 1 all empty, leaving only the lowest bit of 1, such as 10 of the binary is 1010, then Lowbit (k) =lowbit (1010) =0010 (2 binary), In this lowbit is often used in the following, here is a very convenient way to implement, the more common method Lowbit (k) =k&-k, this is a bitwise operation, we know that a number plus a minus sign is the binary of this number is reversed +1, such as 10 of the binary is -1010= 0101+1=0110 (the binary representation of negative numbers is the complement of positive binary), and then with 1010&0110, the answer is 0010! Understand the method of solving lowbit can be, continue below. In the following discussion of the decimal has no meaning (the world is a binary, the person should be subjective to build a decimal), the following all the number is not specifically described as a binary.
The current personal understanding is that Lowbit is seeking out the jurisdiction of the root.
Code:
1#include <stdio.h>2 3 intLowbit (intN)4 { 5 returnN & (-N); 6 } 7 8 intMain ()9 { Ten intncase; Onescanf"%d", &ncase); A while(ncase--) - { - intK; thescanf"%d", &k); -printf"%d%d\n", K-lowbit (k) +1, K + lowbit (k)-1); - } - return 0; +}
POJ 2309 BST (tree-like array lowbit)