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. Ionic3的HTTP请求方法

    Ionic的http请求方法,一种是使用Ionic的Native的Http方法,另一种是使用Angular的Http请求方法. 第一种真的是看着文档都实现不了,很奇怪的错(官网文档:https://i ...

  2. 20145237 《Java程序设计》第七周学习总结

    20145237 <Java程序设计>第七周学习总结 教材学习内容总结 第十三章   一.认识时间与日期   1.时间的度量   在正式认识Java提供了哪些时间处理API之前,得先来了解 ...

  3. Python web服务器

    Python 配置wsgi接口# 引入Python wsgi包 from wsgiref.simple_server import make_server # 撰写服务器端程序代码 def Appli ...

  4. SQL的介绍及MySQL的安装

    基础篇 - SQL 介绍及 MySQL 安装               SQL的介绍及MySQL的安装 课程介绍 本课程为实验楼提供的 MySQL 实验教程,所有的步骤都在实验楼在线实验环境中完成, ...

  5. 一个CSS简单入门网站

    讲的知识简单明了,很实用: http://zh.learnlayout.com/

  6. CSS你所不知的伪元素的用法

    你所不知的 CSS ::before 和 ::after 伪元素用法 博客分类: Div / Css / XML / HTML5   CSS 有两个说不上常用的伪类 :before 和 :after, ...

  7. 记一下webstorm快键键

    #####新建文件````ctrl+alt+insert````#####结构速写````div>ul>li*4>p | div>h1+p | input:text | div ...

  8. PAT1048. Find Coins(01背包问题动态规划解法)

    问题描述: Eva loves to collect coins from all over the universe, including some other planets like Mars. ...

  9. Linq SelectMany 交叉连接

    class Student1 { public int Score { get; set; } public Student1(int score) { this.Score = score; } } ...

  10. 09_Python定义方法_Python编程之路

    有关Python判断与循环的内容我们上几节已经跟大家一起学习了,这一节我们主要针对def 做一个讲解 def 定义一个方法 在项目编程中,我们往往要做很多重复的事,比如一个排序的功能(当然Python ...