B. Code For 1 一个类似于线段树的东西
2024-08-24 19:13:02
http://codeforces.com/contest/768/problem/B
我的做法是,观察到,只有是x % 2的情况下,才有可能出现0
其他的,都是1来的,所以开始的ans应该是R - L + 1
那么现在就是要看那些是x % 2的,然后放在的位置是属于【L, R】的,有多少个0,减去就行。
一开始,总长度是可以算出来的,然后就能算出一开始的n % 2的位置是那个,就是mid了,然后根据L和R选择递归
左边还是右边,然后我发现类似于线段树。。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e6 + 20;
LL n, L, R;
LL calc(LL val) {
if (val > 1) {
return 2 * calc(val / 2);
}
return 1;
}
LL ans;
void dfs(LL cur, LL L, LL R, LL be, LL en) {
if (cur == 0) {
return;
}
if (be == en) {
return;
}
LL mid = (be + en) >> 1;
if (L <= mid && mid <= R && cur % 2 != 1) {
ans--;
}
if (L > mid) {
dfs(cur / 2, L, R, mid + 1, en);
} else if (R <= mid) {
dfs(cur / 2, L, R, be, mid);
} else {
dfs(cur / 2, L, R, be, mid);
dfs(cur / 2, L, R, mid + 1, en);
}
}
void work() {
cin >> n >> L >> R;
if (n == 0) {
cout << 0 << endl;
return;
}
ans = R - L + 1;
dfs(n, L, R, 1, 2 * calc(n) - 1);
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return 0;
}
最新文章
- tecplot 脚本文件
- ajax 对象创建 兼容各个浏览器
- DNS初识
- CSS引入外部字体
- Objective-C运行时编程 - 实现自动化description方法的思路及代码示例
- JDBC连接MySQL数据库及演示样例
- Java 实现大整数加减乘除
- WebGIS中使用ZRender实现前端动态播放轨迹特效的方案
- 我眼中的ASP.NET Core之微服务 (二)
- Win 10 下 android studio显示 Intel haxm无法安装,以及VT-X和hyper-x的冲突问题
- 深入研究 Mini ASP.NET Core(迷你 ASP.NET Core),看看 ASP.NET Core 内部到底是如何运行的
- oi回忆录
- SpringBoot学习(三)-->;Spring的Java配置方式之读取外部的资源配置文件并配置数据库连接池
- Laravel-Excel 导入 Excel 文件----为什么只获取到最后一行数据?
- [转]zookeeper集群 initLimit和syncLimit
- 【二十二】mysqli事务处理与预处理总结
- Vigenere加密
- bzoj千题计划188:bzoj1923: [Sdoi2010]外星千足虫 (高斯—若尔当消元法解异或方程组)
- 使用Phantom omni力反馈设备控制机器人
- IOS-如何优雅地拦截按钮事件(判断是否需要登录)
热门文章
- 使用 fetch 代替 ajax(在不支持的浏览器上使用 XHR); This kind of functionality was previously achieved using XMLHttpRequest.
- mysql 数据库导入错误:40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET
- C# partial分部类和分部方法
- DeepLearningFlappyBird-深度学习玩游戏-1-环境搭建
- Mysql的Merge存储引擎实现分表查询
- TTL以及LVDS接口传输【转】
- HDU3667 Transportation —— 最小费用流(费用与流量平方成正比)
- HNOI2008 越狱 (组合数学)
- Myeclipse----Hibernate环境搭建
- eclipse恢复界面默认设置