B. Code For 1
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On his arrival, Sam tells him that he wants to go to Oldtown to train at the Citadel to become a maester, so he can return and take the deceased Aemon's place as maester of Castle Black. Jon agrees to Sam's proposal and Sam sets off his journey to the Citadel. However becoming a trainee at the Citadel is not a cakewalk and hence the maesters at the Citadel gave Sam a problem to test his eligibility.

Initially Sam has a list with a single element n. Then he has to perform certain operations on this list. In each operation Sam must remove any element x, such that x > 1, from the list and insert at the same position  sequentially. He must continue with these operations until all the elements in the list are either 0 or 1.

Now the masters want the total number of 1s in the range l to r (1-indexed). Sam wants to become a maester but unfortunately he cannot solve this problem. Can you help Sam to pass the eligibility test?

Input

The first line contains three integers nlr (0 ≤ n < 250, 0 ≤ r - l ≤ 105, r ≥ 1, l ≥ 1) – initial element and the range lto r.

It is guaranteed that r is not greater than the length of the final list.

Output

Output the total number of 1s in the range l to r in the final sequence.

Examples
input
7 2 5
output
4
input
10 3 10
output
5
Note

Consider first example:

Elements on positions from 2-nd to 5-th in list is [1, 1, 1, 1]. The number of ones is 4.

For the second example:

Elements on positions from 3-rd to 10-th in list is [1, 1, 1, 0, 1, 0, 1, 0]. The number of ones is 5.

题意:将一个数列中大于1的数x,经过一个操作,在其位置上用   代替x,直至将整个数列化作01序列。

也是悲剧,比赛时把n的规模看成了10^50,卡了好久。。。

直接用dfs会超时,因为n的规模是2^50。看题解,用了类似线段树的区间查询,二分区间查询。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define LL long long LL getLen(LL num)
{
if(num==)
return ;
if(num==)
return ;
return getLen(num/)*+;
} int query(LL nu,LL L,LL R,LL l,LL r)
{
if(R<l||L>r||nu==)
return ;
if(nu==)
return ;
LL mid=l+getLen(nu/);
return query(nu/,L,R,l,mid-)+query(nu%,L,R,mid,mid)+query(nu/,L,R,mid+,r);
} int main()
{
//cout<<getLen(7)<<endl;
LL n,l,r;
scanf("%I64d%I64d%I64d",&n,&l,&r);
LL len=getLen(n);
//cout<<len<<endl;
int res=query(n,l,r,,len);
printf("%d\n",res);
return ;
}

Codeforces_768_B_(二分)的更多相关文章

  1. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

  2. BZOJ 2756: [SCOI2012]奇怪的游戏 [最大流 二分]

    2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 3352  Solved: 919[Submit][Stat ...

  3. 整体二分QAQ

    POJ 2104 K-th Number 时空隧道 题意: 给出一个序列,每次查询区间第k小 分析: 整体二分入门题? 代码: #include<algorithm> #include&l ...

  4. [bzoj2653][middle] (二分 + 主席树)

    Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序列s. 回答Q个这样的询问:s的左端点在[a,b ...

  5. [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  6. [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  7. jvascript 顺序查找和二分查找法

    第一种:顺序查找法 中心思想:和数组中的值逐个比对! /* * 参数说明: * array:传入数组 * findVal:传入需要查找的数 */ function Orderseach(array,f ...

  8. BZOJ 1305: [CQOI2009]dance跳舞 二分+最大流

    1305: [CQOI2009]dance跳舞 Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲 ...

  9. BZOJ 3110 [Zjoi2013]K大数查询 ——整体二分

    [题目分析] 整体二分显而易见. 自己YY了一下用树状数组区间修改,区间查询的操作. 又因为一个字母调了一下午. 貌似树状数组并不需要清空,可以用一个指针来维护,可以少一个log 懒得写了. [代码] ...

随机推荐

  1. C#如何给Listbox添加右键菜单

    1 拖一个ContextMenuStrip控件,然后可以直接在界面上编辑,也可以在FormLoad的时候动态添加 2 把这两个控件关联起来就可以实现listBox1的右键菜单跟ContextMenuS ...

  2. cocos2d-x 3.0 CREATE_FUNC解析

    #define CREATE_FUNC(__TYPE__) \ static __TYPE__* create() \ { \ __TYPE__ *pRet = new __TYPE__(); \ i ...

  3. 神马都是浮云,unity中自己写Coroutine协程源代码

    孙广东   2014.7.19 无意之间看到了,Unity维基上的一篇文章,  是关于自己写协程的介绍. 认为非常好,这样能更好的了解到协程的执行机制等特性.还是不错的. 原文链接地址例如以下: ht ...

  4. Centos下mahout安装与配置

    对于Mahout的安装与配置,须要一个前提.就是hadoop已经安装. 假设没有安装能够參考. http://blog.csdn.net/u012965373/article/details/4533 ...

  5. hdu 2665 Kth number(划分树)

    Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. Android Java包各种功能简述

    开发Android应用程序基本上使用的都是Java语言. 那么要想灵活的应用这一系统,就应当熟悉当中的Android Java包的各种功能. 一般的JAVA应用中.假设需用引用基础类库,通常须要使用例 ...

  7. 【bzoj4597】 [Shoi2016]随机序列

    可以发现加减号之间可以互相抵消. 真正加到答案里的只有一些前缀积. 记s[i]为a[1]*a[2]*a[3]...*a[i].那s[i]在答案中出现的次数就是2*3^(n-i-1); 修改一个数只会对 ...

  8. javax.servlet.http.Part 文件上传

    编辑jsp页面: <html> <head> <base href="<%=basePath%>"> <title>My ...

  9. 使用Zabbix监控ZooKeeper服务的健康状态

    一 应用场景描述 在目前公司的业务中,没有太多使用ZooKeeper作为协同服务的场景.但是我们将使用Codis作为Redis的集群部署方案,Codis依赖ZooKeeper来存储配置信息.所以做好Z ...

  10. 【bug】Android版QQ浏览器广告过滤

    省流加速 - 广告过滤   默认开启 设别广告是添加的标签最外层标签带有adv download