4
Ideas:
The label of the parent node is I, if the left child exists, then the left child's label is 2*i, if the right child exists, then the right child labeled 2*i+1. According to this nature, I will search one layer at a level,
Without the last layer, the nodes of the current layer can all be added without having to count them all. In fact, the last layer does not have to be counted,
As long as the last layer of the tail node is more than the number of the left child's label, then you can use the tail node subscript cut to the left child's subscript plus 1.
#include <iostream>#include<string.h>#include<stdio.h>using namespacestd;intMain () {intM,n,sum,left,right; while(SCANF ("%d%d", &m,&n)!=eof&& (m| |N)) {sum=0; if(m==n) sum=1; Else { if(m<=n) sum=1; Left=2*m; Right=2*m+1; while(n>Right ) {Sum+ = (right-left+1); Left*=2; Right=right*2+1; } if(n>=left) sum=sum+ (n-left) +1; } printf ("%d\n", sum); } return 0;}
Method 2
#include <iostream>#include<string.h>#include<stdio.h>#include<math.h>using namespacestd;intMain () {Long LongM,n,sum,left,right; while(SCANF ("%lld%lld", &m,&n)!=eof&& (m| |N)) {sum=0; intI=0; Left=m; Right=m; while(right<=n) {sum+=int(Pow (2.0, i++)); Right=right*2+1;//Right sub-treeleft*=2;//left dial hand tree } //when the next right sub-tree does not exist while(left<=n) {sum++; Left++; } printf ("%lld\n", sum); } return 0;} /************************************************************** problem:1113 User:zhuoyuezai language:c++ result:accepted time:0 Ms memory:1608 kb****************************************************************/