这道题就是给一个节点n,让你求这个以这个节点为根的子树的最小值和最大值
显然,n为奇数时,min = max =n
#include "stdio.h"
#include "stdlib.h"
int lowbit(int x)
{
return x&(-x);
}
int main()
{
int x,n;
scanf("%d",&n);
while(n--)
{
scanf("%d",&x);
printf("%d %d\n",x-lowbit(x)+1,x+lowbit(x)-1);
}
system("pause");
return 0;
}
1. 附:lowbit(x)函数用法
int Lowbit(x)
{
return x&(-x);
}
如:
x =1: 1 &-1(设位数为8)0000 0001 & 1111 1111 = 1
x = 6:6 & -6 0000 0110
&1111 1010 = 2
总结一下,其实就是:
求出2^p(其中p: x 的二进制表示数中, 右向左数第一个1的位置),如6的二进制表示为110,向左数第零个为0,第一个为1,则p=1,故Lowbit(6) = 2^1 = 2。
本文介绍了一种通过输入节点n来快速计算以该节点为根的子树最小值与最大值的方法。对于奇数节点n,其最小值与最大值均为n本身;而对于偶数节点,则利用lowbit函数计算出相邻的奇数作为极值。此外,文章还详细解释了lowbit函数的工作原理及其应用。
1175

被折叠的 条评论
为什么被折叠?



