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.
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
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
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) 收藏的更多相关文章
- 【solr基础教程之二】索引 分类: H4_SOLR/LUCENCE 2014-07-18 21:06 3331人阅读 评论(0) 收藏
一.向Solr提交索引的方式 1.使用post.jar进行索引 (1)创建文档xml文件 <add> <doc> <field name="id"&g ...
- 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 ...
- 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 ...
- 浅谈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. 后话 以前和安 ...
- C语言基础:函数指针 分类: iOS学习 c语言基础 2015-06-10 21:55 15人阅读 评论(0) 收藏
函数指针:指向函数的指针变量. 函数名相当于首地址. 函数指针定义:返回值类型 (*函数指针变量名)(参数类型1,参数类型2,....)=初始值 函数指针类型:返回值类型 (*)(参数类型1,参数 ...
- Base64编码与解码 分类: 中文信息处理 2014-11-03 21:58 505人阅读 评论(0) 收藏
Base64是一种将二进制转为可打印字符的编码方法,主要用于邮件传输.Base64将64个字符(A-Z,a-z,0-9,+,/)作为基本字符集,把所有符号转换为这个字符集中的字符. 编码: 编码每次将 ...
- NPOI 通用导出数据到Excel 分类: C# Helper 2014-11-04 16:06 246人阅读 评论(0) 收藏
应用场景: 在项目中,经常遇到将数据库数据导出到Excel,针对这种情况做了个程序封装.工作原理:利用NPOI将SQL语句查询出的DataTable数据导出到Excel,所见即所得. 程序界面: ...
- 哈夫曼树-Fence Repair 分类: 树 POJ 2015-08-05 21:25 2人阅读 评论(0) 收藏
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32424 Accepted: 10417 Descri ...
- 二分图匹配(KM算法)n^3 分类: ACM TYPE 2014-10-01 21:46 98人阅读 评论(0) 收藏
#include <iostream> #include<cstring> #include<cstdio> #include<cmath> const ...
随机推荐
- (Factory method)工厂方法设计模式
定义: 1.) 工厂方法模式是用来封装对象的创建,通过让子类来决定创建的对象是什么,来达到将对象创建的过程封装的目的: 2.) 定义了一个创建对象的接口,但由子类决定要实例的泪是哪一个.工厂方法让类把 ...
- redhat linux 安装mysql5.6.27
1.yum安装mysql(root身份) yum install mysql-server mysql-devel mysql -y 如没有配置yum,请参见博客:http://www.cnblogs ...
- PostgreSQL中关于关键字(保留字)在表名和字段名中的应用文件解决
标识符和关键词 受限标识符或被引号修饰的标识符.它是由双引号(")包围的一个任意字符序列.一个受限标识符总是一个标识符而不会是一个关键字.因此"select"可以用于引用 ...
- ShowMessage和MessageDlg消息对话框(VCL)
ShowMessage一个简单的消息提示: 例如:ShowMessage("xxxx"); MessageDlg(constAnsiString Msg, TMsgDlgType ...
- 将url转化成对象
<script> var url = "baidu.com?"; //var url = window.location.search; //获取url functio ...
- Android studio删除工程项目,androidstudio
本新手最近学Android都是用的eclipse.其实个人觉得eclipse不错,可能接触Android不久,倒也不觉得它慢还是怎样.对于Google的Android studio也是早有耳闻,前两天 ...
- HDU 5029 Relief grain(离线+线段树+启发式合并)(2014 ACM/ICPC Asia Regional Guangzhou Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 Problem Description The soil is cracking up beca ...
- android waiting for debugger
在Studio开发环境中,使用真机开发经常会出现waiting for debugger,卡死不动了,让人很崩溃啊,现在我就总结下几种解决方法,希望能帮到出现同样情况的朋友! 问题出现及解决办法: 多 ...
- paper 37 : WINCE的BIB文件解析
WINCE的BIB文件解析 BIB的全称为Binary Image Builder,在Wince编译过程中的最后MakeImage阶段会用到BIB文件,BIB文件的作用是指示构建系统如何构建二进制映像 ...
- sql 中的回车和换行问题
--移除回车符 update master_locationSET street_number = REPLACE(street_number, CHAR(13), '') --移除换行符 updat ...