hdu 4217

题目

区间内第k小数,求其和。

代码如下:

#include<iostream>
#include<cstdio>
using namespace std;

const int maxn= 262147;

struct Tree
{
     int l,r;
     int sum;
}tree[maxn<<2];

inline void PushUp(int rt)
{
    tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}

inline void build(int l,int r,int rt)
{
    tree[rt].l=l;
    tree[rt].r=r;
    if(l==r){
        tree[rt].sum=1;
        return ;
    }
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
    PushUp(rt);
}


inline int query(int pos,int rt)
{
    int l,r;
    l=tree[rt].l;
    r=tree[rt].r;
    if(l==r){
        tree[rt].sum=0;
        return l;
    }
    int m=(l+r)>>1;
    int ans;
    if(pos<=tree[rt<<1].sum)
       ans=query(pos,rt<<1);
    else
       ans=query(pos-tree[rt<<1].sum,rt<<1|1);
    PushUp(rt);
    return ans;
}

int main()
{
    int T,n,k,ncase=1;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&k);
        build(1,n,1);
        long long ans=0;
        int a;
        while(k--){
            scanf("%d",&a);
            ans+=query(a,1);
        }
       cout<<"Case "<<ncase++<<": "<<ans<<endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值