H - Solve this interesting problem 分类: ...



Have you learned something about segment tree? If not, don’t worry, I will explain it for you. 
Segment Tree is a kind of binary tree, it can be defined as this: 
- For each node u in Segment Tree, u has two values: $L_u$ and $R_u$. 
- If $L_u = R_u$, u is a leaf node. 
- If $L_u \neq R_u$, u has two children x and y,with $L_x = L_u$,$R_x = \lfloor \frac{L_u + R_u }{2}\rfloor$,$L_y = \lfloor \frac{L_u + R_u }{2}\rfloor + 1$,$R_y = R_u$. 
Here is an example of segment tree to do range query of sum. 



Given two integers L and R, Your task is to find the minimum non-negative n satisfy that: A Segment Tree with root node's value $L_{root} = 0$ and $R_{root} = n$ contains a node u with $L_u = L$ and $R_u = R$. 
 

Input

The input consists of several test cases. 
Each test case contains two integers L and R, as described above. 
$0 \leq L \leq R \leq 10^9$ 
$\frac{L}{R-L+1} \leq 2015$ 
 

Output

For each test, output one line contains one integer. If there is no such n, just output -1.
 

Sample Input

 
    
6 7 10 13 10 11
 

Sample Output

 
    
7 -1 12

 

补题的时候有一个临界的条件不太懂,请教了一下金巨巨,多谢金巨巨了

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>

using namespace std;

const int MAX = int(10e9+10);

long long  n;

void DFS(long long L,long long  R)
{
    if(n&&R>=n)
    {
        return ;
    }
    if(L==0)
    {
        if(n)
        {

            n=min(n,R);
        }
        else
        {
            n=R;
        }
        return ;
    }
    if(L<R-L+1)
    {
        return ;
    }
    DFS(2*(L-1)-R,R);
    DFS(2*(L-1)+1-R,R);
    DFS(L,2*R-L);
    DFS(L,2*R+1-L);
}

int main()
{
    long long  L,R;
    while(~scanf("%I64d %I64d",&L,&R))
    {
        n=0;
        DFS(L,R);
        if(R==0)
        {
            printf("0\n");
            continue;
        }
        if(n)
            printf("%I64d\n",n);
        else
        {
            printf("-1\n");
        }
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/juechen/p/4721951.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值