Solve this interesting problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 422    Accepted Submission(s): 98

Problem Description
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: Lu and Ru.
- If Lu=Ru,
u is a leaf node. 
- If Lu≠Ru,
u has two children x and y,with Lx=Lu,Rx=⌊Lu+Ru2⌋,Ly=⌊Lu+Ru2⌋+1,Ry=Ru.
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 Lroot=0 and Rroot=n contains
a node u with Lu=L and Ru=R.

 
Input
The input consists of several test cases. 
Each test case contains two integers L and R, as described above.
0≤L≤R≤109
LR−L+1≤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
 
Source

它的上一个节点有(2*l-2-r,r),(2*l-1-r,r),(l,2*r-l+1),(l,2*r - l)四种情况,直接搜索,加上几个特殊判断即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
ll maxn = 0x3f3f3f3f;
ll ma; void dfs(ll l,ll r)
{
if(l == 0)
{
ma = min(r,ma);
return;
}
if(l > r)
return ;
if(r >= 1e18)
return ;
if(r - l + 1 > l)
return ;
if(l == r)
{
ma = r;
return ;
}
dfs(2*l-2-r,r);
dfs(2*l-1-r,r);
dfs(l,2*r-l+1);
dfs(l,2*r - l);
return ;
} int main()
{
ll a,b;
//freopen("8.txt","r",stdin);
while(~scanf("%I64d%I64d",&a,&b))
{
ma = maxn;
dfs(a,b); if(ma == maxn)
printf("-1\n");
else
printf("%I64d\n",ma);
}
return 0;
}

  

2015 多校联赛 ——HDU5323(搜索)的更多相关文章

  1. 2015 多校联赛 ——HDU5305(搜索)

    Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  2. 2015 多校联赛 ——HDU5348(搜索)

    Problem Description As we all kown, MZL hates the endless loop deeply, and he commands you to solve ...

  3. 2015 多校联赛 ——HDU5334(构造)

    Virtual Participation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  4. 2015 多校联赛 ——HDU5335(Walk out)

    Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total S ...

  5. 2015 多校联赛 ——HDU5302(构造)

    Connect the Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. 2015 多校联赛 ——HDU5294(最短路,最小切割)

    Tricks Device Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  7. 2015 多校联赛 ——HDU5325(DFS)

    Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Tota ...

  8. 2015 多校联赛 ——HDU5316(线段树)

    Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an ...

  9. 2015 多校联赛 ——HDU5319(模拟)

    Painter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

随机推荐

  1. python array 使用创建10万浮点数

    from array import array from random floats = array('d',random((for i in range(10**7)) fp = open('flo ...

  2. 前端之bootstrap模态框

    简介:模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动.子窗体可提供信息.交互等. Modal简介 Modal实现弹出表单 M ...

  3. 剑指offer-两个链表的第一个公共节点

    题目描述 输入两个链表,找出它们的第一个公共结点. 解题思路 分析可得如果两个链表有公共节点,那么公共节点出现在两个链表的尾部,即从某一节点开始,两链表之后的节点全部相等.可以首先遍历两个链表得出各自 ...

  4. 《深入实践Spring Boot》阅读笔记之一:基础应用开发

    上上篇「1718总结与计划」中提到,18年要对部分项目拆分,进行服务化,并对代码进行重构.公司技术委员会也推荐使用spring boot,之前在各个技术网站中也了解过,它可以大大简化spring配置和 ...

  5. python开发:python字符串操作方法

    name = "my \tname is {name} and i am {year} old" capitalize:第一个单词的首字母大写的方法 print(name.capi ...

  6. [洛谷P2234][HNOI2002] 营业额统计 - Treap

    Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额. ...

  7. 初学Java Web(7)——文件的上传和下载

    文件上传 文件上传前的准备 在表单中必须有一个上传的控件 <input type="file" name="testImg"/> 因为 GET 方式 ...

  8. 1.4 正则化 regularization

    如果你怀疑神经网络过度拟合的数据,即存在高方差的问题,那么最先想到的方法可能是正则化,另一个解决高方差的方法就是准备更多数据,但是你可能无法时时准备足够多的训练数据,或者获取更多数据的代价很高.但正则 ...

  9. 【vuejs深入一】深入学习vue指令,自定义指令解决开发痛点

    写在前面  一个好的架构需要经过血与火的历练,一个好的工程师需要经过无数项目的摧残. 最近博主我沉淀了几个月,或者说懒了几个月.然而大佬的指点总是一针见血,能够让人看到方向.所以我现在有觉得,一个好的 ...

  10. 定义一个方法get_page(url),url参数是需要获取网页内容的网址,返回网页的内容。提示(可以了解python的urllib模块)

    定义一个方法get_page(url),url参数是需要获取网页内容的网址,返回网页的内容.提示(可以了解python的urllib模块) import urllib.request def get_ ...