HDU5975 Aninteresting game(树状数组)

Aninteresting game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 537    Accepted Submission(s): 216


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.
 

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
 

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 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.
 

Source

两种操作:第一种给出区间,对于区间内每一个数字把他的[i-lowbit(i)+1,i-1]范围内的数字个数求和。显然对于每个数字来说是lowbit(i)。
1 2 3 4 5 6 7 8 9.......
1 2 1 4 1 2 1 8 1.......
对于奇数来说,lowbit为1
对于偶数,其一定可以整除lowbit(相当于将除去低位0的数字向左移动)
所以就可以通过上面的性质计算和
(n-n/2) (n/2-n/4) (n/4-n/8)....
再乘以对应的值即可

#include <stdio.h>
using namespace std;
long long int query(long long int x)
{
    long long int ans=0;
    long long int tmp=1;
    for(long long int i=0;tmp<=x;i++)
    {
        ans+=(x/tmp-x/(tmp<<1))*tmp;
        tmp=tmp<<1;
    }
    return ans;
}
int main()
{
    long long int n,q;
    while(scanf("%lld %lld",&n,&q)!=EOF)
    {
        int sym;
        long long int x,y;
        long long int ans=0;
        for(int k=1;k<=q;k++)
        {
            scanf("%d",&sym);
            if(sym==1)
            {
                scanf("%lld %lld",&x,&y);
                printf("%lld\n",query(y)-query(x-1));
            }
            if(sym==2)
            {
                scanf("%lld",&x);
                ans=0;
                while(x<=n)
                {
                    ans++;
                    x+=x&(-x);
                }
                printf("%lld\n",ans);
            }
        }
    }
    return 0;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值