这道题就是给一个节点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。