B. Code For 1 分治
2 seconds
256 megabytes
standard input
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?
The first line contains three integers n, l, r (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 the total number of 1s in the range l to r in the final sequence.
7 2 5
4
10 3 10
5
Consider first example:
这个题目做的挺漂亮嘛!哈哈!
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<iostream>
#define MAXN 200009
#define eps 1e-11 + 1e-12/2
typedef long long LL; using namespace std;
/*
显然!分治!
*/
LL n, l ,r;
LL solve(LL x, LL beg,LL end)
{
LL mid = (beg + end) / ;
if (beg == mid) return ;
if (r < mid)
return solve(x / , beg, mid - );
else if (l > mid)
return solve(x / , mid + , end);
else
{
LL ret = x % ;
if (l < mid) ret += solve(x / , beg, mid - );
if (r > mid) ret += solve(x / , mid + , end);
return ret;
}
return ;
}
int main()
{ cin >> n >> l >> r;
if (n == )
{
cout << << endl;
return ;
}
LL len = pow(, floor(log2(n)) + ) - ;
cout << solve(n, , len) << endl;
}
B. Code For 1 分治的更多相关文章
- code for 1 - 分治
2017-08-02 17:23:14 writer:pprp 题意:将n分解为n/2, n%2, n/2三部分,再将n/2分解..得到一个序列只有0和1,给出[l, r]问l到r有几个1 题解:分治 ...
- BZOJ 2244: [SDOI2011]拦截导弹 (CDQ分治 三维偏序 DP)
题意 略- 分析 就是求最长不上升子序列,坐标取一下反就是求最长不下降子序列,比较大小是二维(h,v)(h,v)(h,v)的比较.我们不看概率,先看第一问怎么求最长不降子序列.设f[i]f[i]f[i ...
- Codeforces 768B - Code For 1(分治思想)
768B - Code For 1 思路:类似于线段树的区间查询. 代码: #include<bits/stdc++.h> using namespace std; #define ll ...
- Code Chef TSUM2(动态凸包+点分治)
题面 传送门 题解 真是毒瘤随机化算法居然一分都不给 首先这种树上的题目一般想到的都是点分 我们考虑如何统计经过当前点的路径的贡献,设当前点\(u\)在序列中是第\(c\)个,那么一条路径的贡献就是 ...
- CodeForces768B:Code For 1 (分治)
Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On hi ...
- HDU5618 & CDQ分治
Description: 三维数点 Solution: 第一道cdq分治...感觉还是很显然的虽然题目不能再傻逼了... Code: /*=============================== ...
- 【Codeforces715C&716E】Digit Tree 数学 + 点分治
C. Digit Tree time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ...
- 【BZOJ-4456】旅行者 分治 + 最短路
4456: [Zjoi2016]旅行者 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 254 Solved: 162[Submit][Status] ...
- 【BZOJ-3262】陌上花开 CDQ分治(3维偏序)
3262: 陌上花开 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1439 Solved: 648[Submit][Status][Discuss ...
随机推荐
- [IOI1998]Polygon
很早就看到这题了...但因为有个IOI标志,拖到现在才做 由于是以前在书上看到的,就没有想过其他算法,直接区间DP了... 方程式也挺好想的 跟我们平时做数学题求几个数乘积最大差不多 最大的*最大的 ...
- RabbitMQ的一些基本操作
$ sudo chkconfig rabbitmq-server on # 添加开机启动RabbitMQ服务 $ sudo /sbin/service rabbitmq-server start # ...
- 392 Is Subsequence 判断子序列
给定字符串 s 和 t ,判断 s 是否为 t 的子序列.你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=10 ...
- 387 First Unique Character in a String 字符串中的第一个唯一字符
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1.案例:s = "leetcode"返回 0.s = "loveleetcode&qu ...
- 横向移动-广告图(web)
项目 (移动的广告牌) 要求: 1,实现图片一次以移动的方式出现,到最后一张完全出现时,回弹到第一张 2,鼠标放在图片上面图片移动,鼠标离开,图片停止移动 HTML结构 <!DOCTYPE ht ...
- WCF学习笔记(1)-一个完整的例子
一.开发环境 IDE:VS2013 OS:Win10 IIS:IIS 10 二.开发流程 1.项目结构 2.添加一个WCF程序 3.删除系统自动生成的两个文件IService1.cs和Service1 ...
- C:\Windows\System32\drivers\etc\hosts文件显示
attrib -s -h C:\Windows\System32\drivers\etc\hosts
- vux+vuex+vue+Es6开发微信公众号的坑
初次开发微信公众号遇到很多问题,可能是基础不怎么牢靠,最近几天一直在看vue的东西,现在就来慢慢介绍vux和vue这个骚东西的用法: 细看文档一步步来, npm install vux --save ...
- R语言学习 - 热图美化
实际应用中,异常值的出现会毁掉一张热图.这通常不是我们想要的.为了更好的可视化效果,需要对数据做些预处理,主要有对数转换,Z-score转换,抹去异常值,非线性颜色等方式. 对数转换 为了方便描述,假 ...
- CAD得到当前选择的实体(网页版)
主要用到函数说明: IMxDrawSelectionSet::CurrentSelect 得到当前已经选择的实体.详细说明如下: 参数 说明 filterData 过滤条件 js中使用如下: 1 2 ...