Aninteresting game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 542 Accepted Submission(s): 220
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 2 1 8 9 2 6
Sample Output
9 2Hintlowbit(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.
Source
题意:
操作1是求消耗的体力,根据题意来算就是从l到r的Lowbit(i)的求和。
操作2是求数X在N中的在树状数组中的父亲节点有多少。
思路:
对于操作2很好实现。而在算操作1时,直接累加会超时。发现Lowbit(i)是为2的k次幂。
所以只要算出来每个2^i(i=1,2,....)有多少个然后相加起来就行。
从1-n中n/2是二进制末尾为0的个数,n-n/2为二进制末尾为1的个数。发现了这一点后通过不停的左移动就可以算出来每个2^i(i=1,2,....)有多少个。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
ll lowbit(ll i)
{
return i&(-i);
}
ll cal(ll n)
{
ll ans=0,x;
for(ll i=0;(1ll<<i)<=n;i++)
{
x=(1ll<<i);
ans+=(n/x-n/(x*2))*(1ll<<i);
}
return ans;
}
ll cal2(ll n,ll i)
{
ll ans=0;
while(i<=n)
{
ans++;
i+=lowbit(i);
}
return ans;
}
int main()
{
ll n;
int q;
while(~scanf("%lld%d",&n,&q))
{
int o;
ll l,r;
for(int i=1;i<=q;i++)
{
scanf("%d",&o);
if(o==1)
{
scanf("%lld%lld",&l,&r);
printf("%lld\n",cal(r)-cal(l-1));
}
else
{
ll x;
scanf("%lld",&x);
printf("%lld\n",cal2(n,x));
}
}
}
return 0;
}