H - Solve this interesting problem

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

Appoint description: 
System Crawler  (2015-07-28)

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






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

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

  1. H - Solve this interesting problem 分类: 比赛 2015-07-29 21:06 15人阅读 评论(0) 收藏

    Have you learned something about segment tree? If not, don't worry, I will explain it for you.  Segm ...

  2. 多校5-MZL's Border 分类: 比赛 2015-08-05 21:28 7人阅读 评论(0) 收藏

    MZL's Border Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...

  3. short-path problem (Spfa) 分类: ACM TYPE 2014-09-02 00:30 103人阅读 评论(0) 收藏

    #include <cstdio> #include <iostream> #include <cstring> #include <queue> #i ...

  4. short-path problem (Floyd) 分类: ACM TYPE 2014-09-01 23:58 100人阅读 评论(0) 收藏

    #include <cstdio> #include <iostream> #include <cstring> using namespace std; cons ...

  5. short-path problem (Dijkstra) 分类: ACM TYPE 2014-09-01 23:51 111人阅读 评论(0) 收藏

    #include <cstdio> #include <iostream> #include <cstring> using namespace std; cons ...

  6. 多校赛3- Painter 分类: 比赛 2015-07-29 19:58 3人阅读 评论(0) 收藏

    D - Painter Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status P ...

  7. 山东理工大学第七届ACM校赛-LCM的个数 分类: 比赛 2015-06-26 10:37 18人阅读 评论(0) 收藏

    LCM的个数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 对于我们来说求两个数的LCM(最小公倍数)是很容易的事,现在我遇到了 ...

  8. 山东理工大学第七届ACM校赛-完美素数 分类: 比赛 2015-06-26 10:36 15人阅读 评论(0) 收藏

    完美素数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 我们定义:如果一个数为素数,且这个数中含有7或3,那么我们称这个数为完美 ...

  9. 山东理工大学第七届ACM校赛-经济节约 分类: 比赛 2015-06-26 10:34 19人阅读 评论(0) 收藏

    经济节约 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 由于经济紧张,某国国王决定减少一部分多余的士兵,这些士兵在边界都有各自的 ...

随机推荐

  1. BenchmarkSQL测试脚本实现

    sqlTableCreates DROP SCHEMA IF EXISTS benchmarksql CASCADE; CREATE SCHEMA benchmarksql; create table ...

  2. js功能汇总

    请编写一个JavaScript 函数toRGB,它的作用是转换CSS中常用的颜色编码. 要求: 1 alert(toRGB("#0000FF")); // 输出 rgb(0, 0, ...

  3. 河南省第八届ACM程序设计大赛总结

    简单的对这次省赛做个总结:总体来说这个比赛过程中做的还算比较顺利,虽然中间多多少少遇到一些坑,正式比赛开始后,我们就开始找水题,当然了我首先把英文题目翻译了一遍,发现了一道水题,这道题目其实就是判断点 ...

  4. hdu1251(字典树)

    统计难题(hdu1251) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Tota ...

  5. C# Winform 水波纹效果

    //添加自定义控件 using System; using System.Collections.Generic; using System.ComponentModel; using System. ...

  6. 省市联动初探AJAX操作数据

    主要学习代码: List.html <script type="text/javascript"> function GetXhr() { return new XML ...

  7. [原创]java WEB学习笔记80:Hibernate学习之路--- hibernate配置文件:JDBC 连接属性,C3P0 数据库连接池属性等

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  8. 利用MyEclipes的反转工程来配置Hibernate各种配置

    首先需要有设计好的数据库,然后创建一个Web Project然后右键点击项目选择MyEclipse→add Hibernate Capabilities →→ →→,然后如果没有管理员的话需要在选择M ...

  9. mongo自带测试

    benchrun > res = benchRun({ ops:[{ ns:"test.foo", op:"insert", doc:{y:{,]}} } ...

  10. 夺命雷公狗---node.js---20之项目的构建在node+express+mongo的博客项目5mongodb在项目中实现添加数据

    我们上一步就引入了mongodb了,那么下一步就要开始写添加数据了,不过有个前提是先将表单的数据处理好: 最基本的这部现在已经成功了,因为最基本的这步就是先将表单处的提交方式和提交地址给处理好,这里和 ...