HDU-5975 Aninteresting game(树状数组原理)
来源:互联网 发布:2013office软件下载 编辑:程序博客网 时间:2024/06/02 03:17
Aninteresting game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 267 Accepted Submission(s): 112
Problem Description
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.
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.
Input
There 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
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
Output
For each query, please output one line containing your answer for this query
Sample Input
10 21 8 92 6
Sample Output
92Hintlowbit(i) =i&(-i).It means the size of the lowest nonzero bits in binary of i. For example, 610=1102, lowbit(6) =102= 210When 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.
题意:从1~n开始插入数字,如果插入x的花费为[x-lowbit(x)+1,x-1]内已插入的数字的数量+1,定义lowbit(x)为x的二进制中为1的最低位
现在给出2种询问:①1 L R:插入[L,R]的花费;②2 x:求x对所有数的贡献
题解:模拟树状数组,具体解析见代码注释
#include<bits/stdc++.h>using namespace std;typedef long long LL;LL f[64];/*****x-1-(x-lowbit(x)+1)+1+1=lowbit(x),因此每次加入一个数的花费为x的最低位,即是一个2的次幂如果要放入1~n,只要枚举2^k,求1~n中有多少个数的最低位是2^k设最低位是2^k的数有cnt[k]个,f[k]=2^k则cnt[k]=n/f[k]ans+=cnt[k]*f[k]-cnt[k+1]*f[k] 举例解释一下:假设n=10,以2为最低位的是2,6,10,但不包括4,8,因为4,8是以2^2和2^3为最低位的*****/LL solve(LL x){ if(x<=0) return 0; LL ret=0; for(int i=0;f[i]<=x;i++) { LL cnt=x/f[i]; ret+=cnt*f[i]; if(i>0) ret-=cnt*f[i-1]; } return ret;}int main(){ int m,op; LL n,l,r,x; for(int i=0;i<63;i++) f[i]=1ll<<i; //freopen("in.txt","r",stdin); while(~scanf("%lld%d",&n,&m)){ while(m--){ scanf("%d",&op); if(op==1){ scanf("%lld%lld",&l,&r); printf("%lld\n",solve(r)-solve(l-1)); } else { /***** 画个树状数组观察得到:如果y对[x-lowbit(x)+1,x-1]有贡献,那么y的区间一定属于x的区间 例如:3的区间为[3,3],8的区间为[1,8],那么3是对[1,7]有贡献 因此只要往上找包含y的区间即可 *****/ scanf("%lld",&x); int ans=0; while(x<=n){ ans++; x+=x&(-x); } printf("%d\n",ans); } } } return 0;}
阅读全文
0 0
- HDU 5975 Aninteresting game(树状数组原理题)
- HDU-5975 Aninteresting game(树状数组原理)
- HDU 5975 Aninteresting game(树状数组)
- HDU 5975 Aninteresting game (树状数组)
- HDU 5975 && 2016ICPC大连 E: Aninteresting game(树状数组原理)
- HDU 5975 Aninteresting game ( lowbit理解 )
- hdu 5372 Segment Game(树状数组)
- 【HDU 5372】Segment Game(树状数组)
- HDU 5372 Segment Game(树状数组)
- [HDU 5372] Segment Game (树状数组)
- hdu 4605-Magic Ball Game(树状数组)
- HDU 4630 No Pain No Game(离线 + 树状数组)
- hdu 4605 Magic Ball Game(离线+树状数组)
- hdu 5372 Segment Game (树状数组+离散化)
- 树状数组,离散化,突破口(Segment Game,HDU 5372)
- HDU 4605 Magic Ball Game 树状数组
- HDU 4605 Magic Ball Game 树状数组
- Magic Ball Game - HDU 4605 树状数组
- oracle数据库定时任务
- UML之用例图
- 洛谷 P2024 食物链
- 【C++】随机生成数组->冒泡排序->二分查找
- kamailio dispatch负载均衡
- HDU-5975 Aninteresting game(树状数组原理)
- hibernate-6-事务初步了解
- Linux C 网络编程
- AngularJS项目实战(总结)
- Android 探秘系列——四大组件
- HIbernate级联操作cascade以及inverse管理关系
- jquery判断对象是否存在
- JAVA中StringBuffer类常用方法详解
- Mockplus推出真正无限制终身版,做原型就要一辈子!