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. Error : L6218E: Undefined symbol downloadAddress (referred from nand.o).

    MKD 报错: linking...LCD.axf: Error: L6218E: Undefined symbol EnZK (referred from ht128x64.o).LCD.axf: ...

  2. HTML中调用servlet的问题(?)

    最近在学习servlet.刚开始时,按照案例一行一行的敲代码.不过,出问题了. hello1.html <!DOCTYPE html> <html> <head> ...

  3. js字符串转化为方法调用

    function alertFunc(str1,str2,str3){ alert(str1); alert(str2); alert(str3); } function callAlert(func ...

  4. 认识javascript

    javascript小知识 www.phonegap.com(跨平台开发框架) Cocos2d-Html5(WebGL渲染  javascript语言) creatjs.com(融合了flash动画的 ...

  5. c++之路进阶——bzoj3343(教主的魔法)

    F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser  gryz2016 Logout 捐赠本站 Notice:由于本OJ ...

  6. CCF真题之字符串匹配

    201409-3 问题描述 给出一个字符串和多行文字,在这些文字中找到字符串出现的那些行.你的程序还需支持大小写敏感选项:当选项打开时,表示同一个字母的大写和小写看作不同的字符:当选项关闭时,表示同一 ...

  7. 夺命雷公狗—angularjs—21—解决angularjs压缩问题

    我们在实际的开发中往往离不开js的代码压缩,因为这样可以减轻服务器的压力,是的的方法如下所示: <!DOCTYPE html> <html lang="en" n ...

  8. js同步访问后台资源

    $.ajax( {  type : 'post',  url : url,  data : data,  async : false,//false代表只有在等待ajax执行完毕后才执行window. ...

  9. 四则运算GUI版本功能展现

    对于四则运算的GUI版本实现支持批量出题,由于我的不积极导致教师没找到对应的连接,现在重新补上链接 http://www.cnblogs.com/liquan/p/5978687.html codin ...

  10. 【SPFA】 最短路计数

    最短路计数 [问题描述]   给出一个N个顶点M条边的无向无权图,顶点编号为1-N.问从顶点1开始,到其他每个点的最短路有几条. [输入格式]   输入第一行包含2个正整数N,M,为图的顶点数与边数. ...