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;
}

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

H - Solve this interesting problem 分类: 比赛 2015-07-29 21:06 15人阅读 评论(0) 收藏的更多相关文章

  1. 【solr基础教程之二】索引 分类: H4_SOLR/LUCENCE 2014-07-18 21:06 3331人阅读 评论(0) 收藏

    一.向Solr提交索引的方式 1.使用post.jar进行索引 (1)创建文档xml文件 <add> <doc> <field name="id"&g ...

  2. Hiking 分类: 比赛 HDU 函数 2015-08-09 21:24 3人阅读 评论(0) 收藏

    Hiking Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Subm ...

  3. Safecracker 分类: HDU 搜索 2015-06-25 21:12 12人阅读 评论(0) 收藏

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

  4. 浅谈IOS8之size class 分类: ios技术 2015-02-05 19:06 62人阅读 评论(0) 收藏

    文章目录 1. 简介 2. 实验 3. 实战 3.1. 修改 Constraints 3.2. 安装和卸载 Constraints 3.3. 安装和卸载 View 3.4. 其他 4. 后话 以前和安 ...

  5. C语言基础:函数指针 分类: iOS学习 c语言基础 2015-06-10 21:55 15人阅读 评论(0) 收藏

    函数指针:指向函数的指针变量. 函数名相当于首地址. 函数指针定义:返回值类型  (*函数指针变量名)(参数类型1,参数类型2,....)=初始值 函数指针类型:返回值类型  (*)(参数类型1,参数 ...

  6. Base64编码与解码 分类: 中文信息处理 2014-11-03 21:58 505人阅读 评论(0) 收藏

    Base64是一种将二进制转为可打印字符的编码方法,主要用于邮件传输.Base64将64个字符(A-Z,a-z,0-9,+,/)作为基本字符集,把所有符号转换为这个字符集中的字符. 编码: 编码每次将 ...

  7. NPOI 通用导出数据到Excel 分类: C# Helper 2014-11-04 16:06 246人阅读 评论(0) 收藏

    应用场景: 在项目中,经常遇到将数据库数据导出到Excel,针对这种情况做了个程序封装.工作原理:利用NPOI将SQL语句查询出的DataTable数据导出到Excel,所见即所得. 程序界面:   ...

  8. 哈夫曼树-Fence Repair 分类: 树 POJ 2015-08-05 21:25 2人阅读 评论(0) 收藏

    Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32424 Accepted: 10417 Descri ...

  9. 二分图匹配(KM算法)n^3 分类: ACM TYPE 2014-10-01 21:46 98人阅读 评论(0) 收藏

    #include <iostream> #include<cstring> #include<cstdio> #include<cmath> const ...

随机推荐

  1. Velocity(1)——注释

    Velocity的单行注释,使用## 多行注释使用#* cooments *#

  2. Java异步IO/NIO

  3. PHYLIP linux安装

    PHYLIP的安装: http://download.chinaunix.net/download.php?id=29483&ResourceID=8135下载 gunzip phylip-3 ...

  4. CentOS 7 安装Dukto(局域网通信工具)

    rmp包 http://download.opensuse.org/repositories/home:/colomboem/CentOS_7/x86_64/dukto-6.0-13.1.x86_64 ...

  5. 封装page分页类

    类: <?php //分页工具类 class Page{ /*         * 获取分页字符串         * @param1 string $uri,分页要请求的脚本url       ...

  6. mysql bin log日志

    装mysql,运行一段时间后,在mysql目录下出现一堆类似mysql-bin.000***,从mysql-bin.000001开始一直排列下来,而且占用了大量硬盘空间,高达几十个G. 对于这些超大空 ...

  7. 关于MVC4中EFCoderFirst 数据迁移的三句经典指令

    首先输入这句指令     enable-migrations -contexttypename SchoolContext  ---------(SchoolContext为你设置的数据库名)它会自动 ...

  8. 夺命雷公狗ThinkPHP项目之----企业网站17之网站配置页的添加

    为了网站可以智能一点,所以我们开始来写一个网站配置的功能.. 所以我来写他的数据表: 先来完成他的添加功能,页面效果如下所示: lists.html代码如下所示: <!doctype html& ...

  9. 夺命雷公狗---DEDECMS----7dedecms目录结构

    我们dedecms的目录结构其实只需要一张图即可明了了,如下图所示:

  10. dbo与db_owner区别

    dbo 是具有在数据库中执行所有活动的暗示性权限的用户.将固定服务器角色 sysadmin 的任何成员都映射到每个数据库内称为 dbo 的一个特殊用户上.另外,由固定服务器角色 sysadmin 的任 ...