POJ 2309 BST (tree-like array lowbit)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.