• Codeforces_768_B_(二分)


    B. Code For 1
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On his arrival, Sam tells him that he wants to go to Oldtown to train at the Citadel to become a maester, so he can return and take the deceased Aemon's place as maester of Castle Black. Jon agrees to Sam's proposal and Sam sets off his journey to the Citadel. However becoming a trainee at the Citadel is not a cakewalk and hence the maesters at the Citadel gave Sam a problem to test his eligibility.

    Initially Sam has a list with a single element n. Then he has to perform certain operations on this list. In each operation Sam must remove any element x, such that x > 1, from the list and insert at the same position  sequentially. He must continue with these operations until all the elements in the list are either 0 or 1.

    Now the masters want the total number of 1s in the range l to r (1-indexed). Sam wants to become a maester but unfortunately he cannot solve this problem. Can you help Sam to pass the eligibility test?

    Input

    The first line contains three integers nlr (0 ≤ n < 250, 0 ≤ r - l ≤ 105, r ≥ 1, l ≥ 1) – initial element and the range lto r.

    It is guaranteed that r is not greater than the length of the final list.

    Output

    Output the total number of 1s in the range l to r in the final sequence.

    Examples
    input
    7 2 5
    output
    4
    input
    10 3 10
    output
    5
    Note

    Consider first example:

    Elements on positions from 2-nd to 5-th in list is [1, 1, 1, 1]. The number of ones is 4.

    For the second example:

    Elements on positions from 3-rd to 10-th in list is [1, 1, 1, 0, 1, 0, 1, 0]. The number of ones is 5.

    题意:将一个数列中大于1的数x,经过一个操作,在其位置上用   代替x,直至将整个数列化作01序列。

    也是悲剧,比赛时把n的规模看成了10^50,卡了好久。。。

     直接用dfs会超时,因为n的规模是2^50。看题解,用了类似线段树的区间查询,二分区间查询。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    #define LL long long
    
    LL  getLen(LL num)
    {
        if(num==0)
            return 0;
        if(num==1)
            return 1;
        return getLen(num/2)*2+1;
    }
    
    int query(LL nu,LL L,LL R,LL l,LL r)
    {
        if(R<l||L>r||nu==0)
            return 0;
        if(nu==1)
            return 1;
        LL mid=l+getLen(nu/2);
        return query(nu/2,L,R,l,mid-1)+query(nu%2,L,R,mid,mid)+query(nu/2,L,R,mid+1,r);
    }
    
    int main()
    {
        //cout<<getLen(7)<<endl;
        LL n,l,r;
        scanf("%I64d%I64d%I64d",&n,&l,&r);
        LL len=getLen(n);
        //cout<<len<<endl;
        int res=query(n,l,r,1,len);
        printf("%d
    ",res);
        return 0;
    }
  • 相关阅读:
    asp.net 进行发送邮箱验证
    获取微信签名,并保存在xml文件中
    webform获取微信用户的授权
    [转载]将json字符串转换成json对象
    使用authentication进行身份验证,与Forms表单登陆
    解决在IE下LABEL中IMG图片无法选中RADIO的几个方法
    php网页切图/js切图
    最近新装系统windows8.1+Mac。。。还没装驱动就遇到一堆问题。。。
    百度地图api根据定位获取附近商家(只获取屏幕内)
    ios ZBar扫二维码奇奇怪怪的错误
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/6424192.html
Copyright © 2020-2023  润新知