题干:

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 l to 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.

题目大意:

给一个数n,和一个区间[l,r] (r-l<1e5,n<2^50),每次需要把序列中大于1的数字分成(n/2,n%2,n/2)(其中n/2是向下取整),直到所有数变成0或1,问[l,r]区间内有多少个1?

解题报告:

想着先把数字分好,然后再o(n)区间查询一下?有点麻烦,,其实分数字的过程中,答案就可以带出来了。

AC代码:

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
ll l,r;
ll dfs(ll n,ll curl,ll curr) {if(curl > r || curr < l) return 0 ;if(n == 1) return 1;ll res = 0;ll mid = (curl+curr)/2;if(n % 2 == 1 && (mid<=r && mid>=l)) res++;return res + dfs(n>>1,curl,mid-1) + dfs(n>>1,mid+1,curr);
}int main()
{ll n,x,len = 1;cin>>n>>l>>r;x = n;while(x > 1) {len = len*2+1;x>>=1;}printf("%lld\n",dfs(n,1,len));return 0 ;}

总结:

刚开始写代码的时候两个地方写萎了

第一处:两个出口写颠倒了,想想也是不对啊,肯定是首先要在区间内部啊!!这是毋庸置疑的啊!!

第二处:if(n % 2 == 1) res++;

至于为什么没有像线段树一样写上如果两者都在所求区间内部的话 单独返回一个值,这是因为我们提前把len算好了,所以最后分出的答案一定是0或者1,可以直接通过那个n==1来防止无限递归。然后就是对于0的处理,只需要那个res那里处理一下就可以了,因为只有中间的值才会出现等于0的情况,两边的值是不会出现为0的情况的,因为是除2操作嘛!!枚举几个终点可能值,就可以简单证明了,3/2=1,,,2/2=1,,,,1的话就是出口了,所以拆数的时候不会在两边有0出现,只可能是中间会有0出现。

所以其实这题我们也可以直接模拟一下过程,找找会有几个mid值是偶数,那么才会有0出现,假设求出0的数量是cnt0,那么结果就是r-l-cnt0。但是其实还是要模拟线段树build的过程的、、、、因为还要找和l,r的大小关系。

另附一个网络的二分思路的代码:(还未看)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, l, r, s = 1, ans;
void solve(ll a, ll b, ll l, ll r, ll d) { //二分的思想if ( a > b || l > r ) return;ll mid = (a+b)/2;if ( r < mid )solve(a,mid-1,l,r,d/2);else if( mid < l )solve(mid+1,b,l,r,d/2);else {ans += d%2;solve(a,mid-1,l,mid-1,d/2);solve(mid+1,b,mid+1,r,d/2);}
}
int main() {cin >> n >> l >> r;long long p = n;while ( p >= 2 ) {p /= 2;s = s*2+1;}solve(1,s,l,r,n);cout << ans << endl;return 0;
}

*【CodeForces - 768B】Code For 1 (分治策略,模拟二分思想,模拟线段树思想)相关推荐

  1. BZOJ 3836 Codeforces 280D k-Maximum Subsequence Sum (模拟费用流、线段树)

    题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=3836 (Codeforces) http://codeforces.com ...

  2. Codeforces Round #699 (Div. 2) E.Sorting Books(贪心+DP / 线段树)超高质量题解,看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 E - Sorting Books 一排书架上有 nnn 本书排成一排,每本书上有一个颜色 aia_i ...

  3. Codeforces Round #807 (Div. 2) E. Mark and Professor Koro 二进制/线段树

    题目分析 模拟题目不难发现,实际上擦除操作就是在模拟二进制加法进位.那么可以得到原题意的转述: 将每个 a [ i ] a[i] a[i]看作 2 a [ i ] 2^{a[i]} 2a[i],维护整 ...

  4. 线段树分治 ---- F. Extending Set of Points(线段树分治 + 可撤销并查集)

    题目链接 题目大意: 你有个点集合SSS,每次往集合里面加点或者删点(如果要加的点出现过),如果(x1,y1),(x2,y1),(x1,y2),(x2,y2)(x1,y1),(x2,y1),(x1,y ...

  5. BZOJ 1920 Luogu P4217 [CTSC2010]产品销售 (模拟费用流、线段树)

    题目链接 (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=1920 (luogu) https://www.luogu.org/prob ...

  6. BZOJ 5326 [JSOI2017]博弈 (模拟费用流、线段树)

    题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=5326 题解 终于成为第8个A掉这题的人--orz tzw神仙早我6小时 本以为这东西常数 ...

  7. [2021.1.27多校省选模拟10]跑步(线段树合并)

    [2021.1.27多校省选模拟10]跑步 经典的树上启发式合并题目,维护对应子树的从当前点到子树内一个节点这个链待定,其他部分已经确定的方案数,这个东西按照对应点到根节点的路径点权和为下标存在一个权 ...

  8. Educational Codeforces Round 73 (Rated for Div. 2) F. Choose a Square 线段树 + 二维转一维

    传送门 文章目录 题意: 思路: 题意: 给你nnn个点(xi,yi)(x_i,y_i)(xi​,yi​),每个点有个价值cic_ici​,现在你可以框一个正方形,要求左下角和右上角的坐标(x,y)( ...

  9. Codeforces Round #620 (Div. 2) F2. Animal Observation (hard version) dp + 线段树

    传送门 文章目录 题意: 思路: 题意: 比如下面这个图: 思路: 对于这个题,比较容易就能考虑到dpdpdp,设f[i][j]f[i][j]f[i][j]为到了第iii行,覆盖了[j,j+k−1][ ...

最新文章

  1. VS2010中“工具选项中的VC++目录编辑功能已被否决”解决方法
  2. 13. PDE_PTE属性
  3. Android FragmentManage FragmentTransaction介绍
  4. Sentinel连接 Azure 活动日志中的数据
  5. ThinkJS入门+实例(实现认证权限等基本功能)
  6. 个人计算机好用的pdf软件,win10好用的pdf阅读器推荐 推荐几款好用的pdf阅读器
  7. 安装包时后面的参数以及简写
  8. 3线8线译码器74HC138门电路设计一位二进制全减器电路
  9. 产品数据管理(PDM)技术说明书
  10. python的socket
  11. java 数字转大写_Java实现数字大写转换
  12. 【天光学术】学前教育论文:幼儿园区角活动中存在的问题及有效对策(节选)
  13. 如何把wps随机数据固定_WPS Excel:巧用随机函数rand和randbetween生成各种数据
  14. 音视频开发: ffmpeg采集桌面屏幕、摄像头保存为视频
  15. picker-view-column自定义picker
  16. 「BUG记录」关于在安装AD9910 Evaluation Software过程中遇到的问题
  17. 憨牛女装旗舰店开业啦!
  18. # [1007]魔法少女小Scarlet
  19. aizuda 学习之 @ControllerAdvice 和RequestBodyAdviceAdapter运用 自动配置介绍
  20. LuLu UI表单验证

热门文章

  1. 完美的正方形分割(二)
  2. 零基础学软件测试有前途吗?
  3. diff 比较两个文件夹下各个文件的内容(差别)
  4. ANDROID 系统下载
  5. CodeForces 13A - Numbers
  6. Micro API使用
  7. 一晚上写出来的游戏 苹果也会推荐!
  8. gRPC(四)基础:gRPC流
  9. 特别提醒:人脸识别时,一定要穿衣服,一不小心就中招了
  10. MATLAB 制作抖音同款 立体人物文字海报