poj 2309 BST(lowbit)
来源:互联网 发布:mysql 导入数据乱码 编辑:程序博客网 时间:2024/05/18 20:09
题意:对于一个满二叉树,给所有节点按从左到右的顺序从1开始编号,使其成为一棵二叉查找树。现每次给定一个节点编号,问以该节点为根的子树的最小编号和最大编号节点分别为多少号。
思路:(参考http://www.cnblogs.com/rainydays/archive/2011/05/23/2054673.html)使用lowbit,即二进制码中的最靠后的1和后面的0组成的数字。规律是这样的,对于以x为根的子树,以lowbit(x)为其高位和低位的分界线,该子树上的所有节点编号的高位(不包括lowbit(x)那一位)均与x相同。不在该子树上的点的编号的高位均与x不同。因此,只要算出以x的高位为高位的数字中最大和最小的两个即为所求。
#include <stdio.h>int n;int test(int x){int temp=x&(-x);x ^= temp;x |= 1;return x;}int main(){freopen("a.txt","r",stdin);scanf("%d",&n);while(n--){long long i,m;scanf("%lld",&m);i = test(m);printf("%lld %lld\n",i,(m<<1)-i);}return 0;}
0 0
- poj 2309 BST(lowbit)
- POJ 2309:BST lowbit
- POJ 2309 BST 【lowbit()过】
- poj 2309 BST 使用树状数组的lowbit
- poj 2309(树状数组lowbit)
- poj 2309 BST (^ ^)
- POJ 2309 BST
- poj 2309 BST
- POJ 2309 BST
- POJ 2309 BST
- BST - POJ 2309 水题
- POJ 2309 BST
- POJ 2309 BST
- poj 2309 BST
- poj 2309 BST
- POJ 2309 BST
- Poj 2309 BST
- POJ 2309 BST
- 换肤效果
- poj 2533 Longest Ordered Subsequence(最长上升子序列)
- oracle_12505错误解决方法
- atitit. 日志系统的原则and设计and最佳实践(1)-----原理理论总结.
- Python命名规范
- poj 2309 BST(lowbit)
- js相关
- strstr() 函数的使用
- HTML特殊字符显示
- 《Programming in Lua 3》读书笔记(二十五)
- itext pdflayer
- 一个测试者的忏悔
- 【Qt】制作应用插件
- C# 对象与JSON串互相转换