Let’s play a game.We add numbers 1,2...n in increasing order from 1 and put them into some sets. 
When we add i,we must create a new set, and put iinto it.And meanwhile we have to bring [i-lowbit(i)+1,i-1] from their original sets, and put them into the new set,too.When we put one integer into a set,it costs us one unit physical strength. But bringing integer from old set does not cost any physical strength. 
After we add 1,2...n,we have q queries now.There are two different kinds of query: 
1 L R:query the cost of strength after we add all of [L,R](1≤L≤R≤n) 
2 x:query the units of strength we cost for putting x(1≤x≤n) into some sets. 
InputThere are several cases,process till end of the input. 
For each case,the first line contains two integers n and q.Then q lines follow.Each line contains one query.The form of query has been shown above. 
n≤10^18,q≤10^5 
OutputFor each query, please output one line containing your answer for this querySample Input
10 2
1 8 9
2 6
Sample Output
9
2
Hint
lowbit(i) =i&(-i).It means the size of the lowest nonzero bits in binary of i. For example, 610=1102, lowbit(6) =102= 210
When we add 8,we should bring [1,7] and 8 into new set.
When we add 9,we should bring [9,8] (empty) and 9 into new set.
So the first answer is 8+1=9.
When we add 6 and 8,we should put 6 into new sets.
So the second answer is 2. 题意:
多组输入,。每一组数据,有一个数字n,和一个数q,
有1~n个数,将i放入集合中同时放入i-lowbit(i)+1~ i 的所有数。
每向集合中放入一个数,就会消耗一点体力值。 然后有q个询问,询问1是,给你一个l和r,问l~r每一个数都加入到集合中,会消耗多少体力值。
2.
1~n中,对每一个数进行加入到集合中的话,数x会被加入多少次。 思路:
把i放入集合就会放入i-lowbit(i)+1~i的所有数
那么一共就是加入i - (i-lowbit(i)+1) +1 个数,即lowbit(i)个数,
那么一个数i消耗的体力值就是lowbit(i)
那么第一问就是让求l~r的lowbit的sum和。 由于数据量很大,我们不能扫一遍算出,
那么我们来分析一下是否可以优化。,
我们知道,lowbit(i)表示的是数字i的二进制最低位代表的十进制数。
例如6的二进制是110,最低位时第2个1,从右向左第2位,代表的十进制时2,(二进制10是十进制的2),那么lowbit(6)就是2。
那么l~r的lowbit的sum和就是,最低位的1在0位置上的数的数量*1(代表的十进制)+ 最低位的1在1位置上的数的数量*2 +.....
那么问题转化为如何快速的求出l~r中最低位的1在第i位的数的数量,
我们知道这样的一个求解方法(容斥的思想)n/(1<<P)-n/(1<<(p+1))就是·1~n中最低位的1在第p位(从右向左数,0开始计位)的数的数量。
那么我们只需要logn来枚举每一位,然后上面的公式即可计算出每一位的数量。 然后我们来看第二个问题,
要找到x在几个集合中,可用x=x+lowbit(x)找出有多少满足条件的x,就能得到集合的个数 细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll solve(ll x)
{
ll res=0ll;
for(ll p=1ll;p<=x;p<<=)
{
res+=(x/p-x/(p<<))*p;
}
return res;
}
ll lowbit(ll x)
{
return x&(-x);
}
ll solve2(ll x,ll n)
{
ll res=0ll;
while(x<=n)
{
res++;
x+=lowbit(x);
}
return res;
} int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout); ll n,q;
while(~scanf("%lld %lld",&n,&q))
{
int op;
while(q--)
{
scanf("%d",&op); if(op==)
{
ll l,r;
scanf("%lld %lld",&l,&r);
ll res=solve(r)-solve(l-1ll);
printf("%lld\n",res );
}else
{
ll x;
scanf("%lld",&x);
printf("%lld\n",solve2(x,n) );
}
}
} return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
												

Aninteresting game HDU - 5975 (数学+lowbit)的更多相关文章

  1. HDU 5984 数学期望

    对长为L的棒子随机取一点分割两部分,抛弃左边一部分,重复过程,直到长度小于d,问操作次数的期望. 区域赛的题,比较基础的概率论,我记得教材上有道很像的题,对1/len积分,$ln(L)-ln(d)+1 ...

  2. hdu 5975 Aninteresting game

    Aninteresting game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  3. HDU 5976 数学,逆元

    1.HDU 5976 Detachment 2.题意:给一个正整数x,把x拆分成多个正整数的和,这些数不能有重复,要使这些数的积尽可能的大,输出积. 3.总结:首先我们要把数拆得尽可能小,这样积才会更 ...

  4. *HDU 2451 数学

    Simple Addition Expression Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  5. [ An Ac a Day ^_^ ] hdu 4565 数学推导+矩阵快速幂

    从今天开始就有各站网络赛了 今天是ccpc全国赛的网络赛 希望一切顺利 可以去一次吉大 希望还能去一次大连 题意: 很明确是让你求Sn=[a+sqrt(b)^n]%m 思路: 一开始以为是水题 暴力了 ...

  6. hdu 4506(数学,循环节+快速幂)

    小明系列故事——师兄帮帮忙 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  7. hdu 4432 数学杂题

    http://acm.hdu.edu.cn/showproblem.php?pid=4432 6分钟写的代码,一上午去调试,, 哎,一则题目没看懂就去写了,二则,哎,,恶心了.在坚持几天然后ACM退役 ...

  8. hdu 4811 数学 不难

    http://acm.hdu.edu.cn/showproblem.php? pid=4811 由于看到ball[0]>=2 && ball[1]>=2 && ...

  9. hdu 5288 数学 ****

    给一个序列 定义函数f(l ,r) 为区间[l ,r] 中 的数ai不是在这个区间其他任意数aj的倍数 求所有f(l,r)之和 通过预处理,记录 a[i] 的左右边界(所谓的左右边界时 在从 a[i] ...

随机推荐

  1. #Java学习之路——基础阶段(第十一篇)

    我的学习阶段是跟着CZBK黑马的双源课程,学习目标以及博客是为了审查自己的学习情况,毕竟看一遍,敲一遍,和自己归纳总结一遍有着很大的区别,在此期间我会参杂Java疯狂讲义(第四版)里面的内容. 前言: ...

  2. Python-网络爬虫模块-requests模块之请求

    Python原生库urllib库不太方便使用,本着"人生苦短, 我用Python"的珍惜生命精神, 基于urllib, 开发了一个对人类来说, 更好使用的一个库——requests ...

  3. 从壹开始前后端分离 [ vue + .netcore 补充教程 ] 三十║ Nuxt实战:动态路由+同构

    上期回顾 说接上文<二九║ Nuxt实战:异步实现数据双端渲染>,昨天咱们通过项目二的首页数据处理,简单了解到了 nuxt 异步数据获取的作用,以及亲身体验了几个重要文件夹的意义,整篇文章 ...

  4. load data infile

    mysql> load data local infile 'd:/lw.txt' into table user (code,password,channelId);Query OK, 478 ...

  5. 推荐一款好用的任务定时器:Quartz

    前言 官网:https://www.quartz-scheduler.net/ 一款开源的任务定时器. 日常有很多地方需要定时刷新的,比如微信开发中的微信API token,或者定时清理一下缓存数据等 ...

  6. jsom快速入门

    JSON JSON: JavaScript Object Notation(JavaScript 对象表示法) JSON 是存储和交换文本信息的语法,具有自我描述性.类似 XML, 但比 XML 更小 ...

  7. HTTP Get与Post的本质区别

    作者:Larry链接:https://zhuanlan.zhihu.com/p/22536382来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. GET和POST是HTT ...

  8. 从零开始学安全(四十)●上传文件MIME类型绕过漏洞防御

    MIME检测原理 服务端MIME类型检测是通过检查http包的Content-Type字段中的值来判断上传文件是否合法的. php示例代码: if($_FILES['userfile']['type' ...

  9. c#封装DBHelper类

    public enum EffentNextType { /// <summary> /// 对其他语句无任何影响 /// </summary> None, /// <s ...

  10. SpringMVC与Struts2的主要区别

    区别1: Struts2 的核心是基于一个Filter即StrutsPreparedAndExcuteFilterSpringMvc的核心是基于一个Servlet即DispatcherServlet( ...