雕刻时光

just do it……nothing impossible
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

线段树的结点更新

Posted on 2012-04-21 21:13  huhuuu  阅读(153)  评论(0编辑  收藏  举报

http://acm.hdu.edu.cn/showproblem.php?pid=4217

 

View Code
#include<stdio.h>

struct data
{
    int l,r;
    int sum;
}node[1962144];

void build(int ll,int rr,int n)
{
    node[n].l=ll;
    node[n].r=rr;
    node[n].sum=rr-ll+1;
    if(ll==rr)return ;
    int mid=(ll+rr)>>1;
    build(ll,mid,n+n);
    build(mid+1,rr,n+n+1);
}

int point;
void search(int n,int temp)
{
    if(node[n].l==node[n].r){
        point=node[n].l;
        node[n].sum--;
        return;
    }
    
    if(temp<=node[n+n].sum)
    {
        search(n+n,temp);
    }else
    {
        search(n+n+1,temp-node[n+n].sum);
    }
    node[n].sum--;
}

int main()
{
    int t,T=0;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);

        build(1,n,1);

        int i,temp;
        __int64 all=0;
        for(i=1;i<=m;i++)
        {
            scanf("%d",&temp);
            search(1,temp);
            all+=point;
        }
        printf("Case %d: ",++T);
        printf("%I64d\n",all);
    }

    return 0;
}