HDU Just a hook

更新一段区间内所有点的值

#include<stdio.h>
int N,Q;
const int MAX=100000;
struct line
{
    int left;
    int right;
    int Value;
};
line lines[MAX<<2];
void buildTree(int le,int rig,int root)
{
    lines[root].left=le;
    lines[root].right=rig;
    lines[root].Value=1;
    if(le==rig)
    {
        return ;
    }
    int mid=(lines[root].left+lines[root].right)>>1;
    buildTree(le,mid,root<<1);
    buildTree(mid+1,rig,(root<<1)+1);
}

void update(int le,int rig,int v,int root)
{
     if(lines[root].Value==v)
     {
         return ;
     }
     if(lines[root].left==le&&lines[root].right==rig)
     {
         lines[root].Value=v;
         return ;
     }
     if(lines[root].Value!=-1)
     {
         lines[root<<1].Value=lines[root].Value;
         lines[(root<<1)+1].Value=lines[root].Value;
     }
     lines[root].Value=-1;
     int mid=(lines[root].left+lines[root].right)>>1;
     if(rig<=mid)
     {
         update(le,rig,v,root<<1);
     }
     else if(mid<le)
     {
         update(le,rig,v,(root<<1)+1);
     }else
     {
         update(le,mid,v,root<<1);
         update(mid+1,rig,v,(root<<1)+1);
     }

}
int getSum(int le,int rig,int root)
{
    if(lines[root].Value>0)
    {
        return (rig-le+1)*lines[root].Value;
    }else
    {
        int mid=(lines[root].left+lines[root].right)>>1;
        return getSum(le,mid,root<<1)+getSum(mid+1,rig,(root<<1)+1);
    }
}
int main()
{
    int T;
    int text=0;
    scanf("%d",&T);
    while(T--)
    {
        text++;
        int x,y,z;

        scanf("%d%d",&N,&Q);
        buildTree(1,N,1);

        /*for(int j=1;j<=20;j++)
        {
            printf("%d\n",lines[j].Value);
        }*/
        for(int i=1;i<=Q;i++)
        {
           scanf("%d%d%d",&x,&y,&z);
           update(x,y,z,1);
        }
        printf("Case %d: The total value of the hook is %d.\n",text,getSum(1,N,1));
    }
    return 0;
}

posted on 2011-06-02 19:13  lonelycatcher  阅读(219)  评论(0编辑  收藏  举报

导航