B. Code For 1 一个类似于线段树的东西
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;
}
B. Code For 1 一个类似于线段树的东西的更多相关文章
- 有趣的 zkw 线段树(超全详解)
zkw segment-tree 真是太棒了(真的重口味)!写篇博客纪念入门 emmm...首先我们来介绍一下 zkw 线段树这个东西(俗称 "重口味" ,与 KMP 类似,咳咳. ...
- ACM/ICPC 之 数据结构-线段树思想(POJ2182,含O(n^2)插入式解法)
这道题在一定程度上体现了线段树的一种用法,解决的问题是:对于总计n个元素的第i个元素,已知其在[1,i]上部分序列的排名,求第i个元素在所有n个元素中的排名. 当然这道题数据比较水,所以用O(n^2) ...
- BZOJ 1835: [ZJOI2010]base 基站选址(DP,线段树)
可以很容易的写出dp方程: F[i][j]=min(F[l][j-1]+w[l][i])+c[i] (w[i][j]是从l+1到i-1这些点p里,所有满足d[p]+s[p]<d[i] & ...
- poj_2528Mayor's posters(线段树)
poj_2528Mayor's posters(线段树) 标签: 线段树 题目连接 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K To ...
- 『zkw线段树及其简单运用』
阅读本文前,请确保已经阅读并理解了如下两篇文章: 『线段树 Segment Tree』 『线段树简单运用』 引入 这是一种由\(THU-zkw\)大佬发明的数据结构,本质上是经典的线段树区间划分思想, ...
- 线段树 by yyb
线段树 by yyb Type1 维护特殊信息 1.[洛谷1438]无聊的数列 维护一个数列,两种操作 1.给一段区间加上一个等差数列 2.单点询问值 维护等差数列 不难发现,等差数列可以写成\(ad ...
- hdu 4366 Successor - CDQ分治 - 线段树 - 树分块
Sean owns a company and he is the BOSS.The other Staff has one Superior.every staff has a loyalty an ...
- poj2991 Crane(线段树)
Description ACM has bought a new crane (crane -- jeřáb) . The crane consists of n segments of variou ...
- 刷题向》关于线段树的区间开根号 BZOJ3211(NORMAL+)
这是一道关于线段树的区间开根号的裸题,没什么好讲的. 值得注意的是,因为有区间开根号的性质,所以我们每一次更改操作只能把更改区间所覆盖的所有元素全部查找,当然你直接找效率明显爆炸... 能够注意到,指 ...
随机推荐
- Scrapy 'module' object has no attribute 'Spider'错误
在“Scrapy入门教程”中,在创建的“dmoz_spider.py”文件中是通过 import scrapy class DmozSpider(scrapy.Spider): 的方式导入.但是用这种 ...
- spark通信原理
https://github.com/apache/spark/tree/master/core/src/main/scala/org/apache/spark/network https://git ...
- Mongoose Embedded Web Server Library
https://github.com/cesanta/mongoose http://ltp.ai/docs/ltpserver.html LTP Server在轻量级服务器程序mongoose基础上 ...
- EventStore文件存储设计
背景 ENode是一个CQRS+Event Sourcing架构的开发框架,Event Sourcing需要持久化事件,事件可以持久化在DB,但是DB由于面向的是CRUD场景,是针对数据会不断修改或删 ...
- 请问snmp到底是干啥的。
这个事情分两方面来说:首先是路由器这部分.路由器开启SNMP功能之后,它能够对自己的每个接口上的流量有一个统计,当然统计的不单单只有流量.然后路由器把统计到的内容按一定的格式保存起来.这个格式是大家都 ...
- eclipse安装lombok和常用注解使用
1.下载lombok.jar lombok 的官方网址:http://projectlombok.org/ 2.运行lombok.jar: java -jar D:\eclipse-luna\l ...
- django错误 - Reason given for failure: CSRF cookie not set.
练习Django表单提交时遇到如下问题: 在网上各种查找,终于找到了解决方法. 1.在from 表单中添加 {% csrf_token %} 2.在视图中添加 from django.template ...
- jsch上传文件功能
转载:http://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html JSch是Java Secure Channel的缩写.JSch是一个 ...
- 「网络流24题」「LuoguP4015」 运输问题
Description W 公司有 m 个仓库和 n 个零售商店.第 i 个仓库有 ai 个单位的货物:第 j 个零售商店需要 bj 个单位的货物. 货物供需平衡,即 ∑ai=∑bj . 从第 i ...
- BZOJ3355
3355: [Usaco2004 Jan]有序奶牛 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 37 Solved: 19[Submit][Sta ...