Chinaunix首页 | 论坛 | 博客
  • 博客访问: 198222
  • 博文数量: 67
  • 博客积分: 2970
  • 博客等级: 少校
  • 技术积分: 685
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-23 11:36
文章分类

全部博文(67)

文章存档

2012年(2)

2011年(19)

2010年(46)

我的朋友

分类: C/C++

2010-09-23 17:20:38


#include<stdio.h>

const int MAX=100000;
int tree[MAX*3];

void update(int node,int l,int r,int lx,int rx,int newValue)    
{
    if(l==lx&&r==rx)
    {
        tree[node]=newValue;
        return ;
    }

    if(tree[node]>0)        //向下传递新值

    {
        tree[node*2]=tree[node*2+1]=tree[node];
        tree[node]=0;
    }

    int mid=(l+r)/2;
    if(rx<=mid) update(node*2,l,mid,lx,rx,newValue);
    else if(lx>mid) update(node*2+1,mid+1,r,lx,rx,newValue);
    else
    {
        update(node*2,l,mid,lx,mid,newValue);
        update(node*2+1,mid+1,r,mid+1,rx,newValue);
    }
}

int inquire(int node,int l,int r)
{
    if(tree[node]>0)
    {
        return tree[node]*(r-l+1);
    }

    int mid=(l+r)/2;
    return inquire(node*2,l,mid)+inquire(node*2+1,mid+1,r);
}

int main()
{
    int cases;
    int count=0;
    scanf("%d",&cases);

    while(cases--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        tree[1]=1;                //最初时所有的位置值均为1

        int from,to,value;
        while(m--)
        {
            scanf("%d%d%d",&from,&to,&value);

            if(from>to){int temp=from,from=to,to=temp;}
            update(1,1,n,from,to,value);
        }
        printf("Case %d: The total value of the hook is %d.\n",++count,inquire(1,1,n));
    }
    return 0;
}



阅读(877) | 评论(0) | 转发(0) |
0

上一篇:pku1083 Moving Tables

下一篇:pku2795 Billboard

给主人留下些什么吧!~~