首页 技术 正文
技术 2022年11月16日
0 收藏 587 点赞 4,780 浏览 2112 个字

题目链接

Problem DescriptionLet’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 query Sample Input10 21 8 92 6 Sample Output92 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.

 题意:每次查询有两种操作           op1:求加入L~R的数时所消耗的单元           op2:求将x加入集合或移动到其它集合所消耗的单元(即由x引起消耗的单元) 思路:op1:每次加入一个数i 那么会移动[i-lowbit(i)+1 , i-1] ,总的消耗是i-(i-lowbit(i)+1) +1=lowbit(i) 所以每次加入一个数对应的消耗是2的幂次,那么求L~R即可以枚举幂次,即: ans+=(n/(1<<i)-n/(1<<(i+1)))*(1<<i)                  解释一下,n/(1<<i)-n/(1<<(i+1))表示长为2^i的消耗的数的个数,例如:n=10 , 包含长为2的数是2,6,10 为什么4,8不是,因为它们虽然是2的倍数,但更是4的倍数,包含更长的区间了,所以这部分要减去。        op2:由树状数组可知 [i-lowbit(i)+1 , i-1] 是以i为根节点对应的区间,如果假如的数能够移动i ,那么这个数对应的孩子区间一定包含i ,所以从x向上一直找父节点即可。 代码如下:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
LL lowbit(LL x)
{
return x&(-x);
}
LL query(LL x,LL n)
{
LL ans=;
while(x<=n)
{
ans++;
x+=lowbit(x);
}
return ans;
}
LL cal(LL x)
{
LL ans=;
LL tmp=;
for(LL i=; tmp<=x; i++)
ans+=(x/(tmp)-x/(tmp<<))*tmp,tmp<<=;
return ans;
}
int main()
{
LL n,q;
while(scanf("%lld%lld",&n,&q)!=EOF)
{
while(q--)
{
int op;
scanf("%d",&op);
if(op==)
{
LL x,y;
scanf("%lld%lld",&x,&y);
LL ans=cal(y)-cal(x-);
printf("%lld\n",ans);
}
else
{
LL x;
scanf("%lld",&x);
LL ans=query(x,n);
printf("%lld\n",ans);
}
}
}
return ;
}

    

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,121
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,593
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,438
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,209
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,845
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,930