Hdu 1498(线段树)

Just a Hook



这是一道延迟标记的题目,刚入门,还不是特别了解。

但是 push_down 函数很重要,mark 以后再好好思考思考



#include <iostream>
#include <stdio.h>
using namespace std;
const int N=100005;
struct node
{  int col,sum;
}tree[N<<2];
void push_up(int rt)
{  tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum ; }
void build(int l,int r,int rt)
{  tree[rt].col=0;
   tree[rt].sum=1;
   if(l==r)
      return ;
   int m=(l+r)>>1;
   build(l,m,rt<<1);
   build(m+1,r,rt<<1|1);
   push_up(rt);
}
void push_down(int rt,int m)  //m 为长度。
{
   if(tree[rt].col )  //0 表示铜色,不需要更新。
   {  tree[rt<<1].col=tree[rt<<1|1].col=tree[rt].col;
      tree[rt<<1].sum=(m-(m>>1) )*tree[rt].col;
      tree[rt<<1|1].sum=(m>>1)   *tree[rt].col;
      tree[rt].col=0;  //更新后改变标记,即不需要更新了
    }
}
void updata(int l,int r,int rt,int L,int R,int c)
{
   if(L<=l&&R>=r)
   {   tree[rt].col=c;
       tree[rt].sum=(r-l+1)*c ;
       return ;
   }
   push_down(rt,r-l+1);
   int m=(l+r)>>1;
   if(L<=m)
      updata(l,m,rt<<1,L,R,c);
   if(R>m)
      updata(m+1,r,rt<<1|1,L,R,c);
   push_up(rt);
}
int main()
{    int T,n,m;
     int a,b,c;
     scanf("%d",&T);
     for(int i=1;i<=T;i++)
     {   scanf("%d%d",&n,&m);
         build(1,n,1);
         while(m--)
         {  scanf("%d%d%d",&a,&b,&c);
            updata(1,n,1,a,b,c);
         }
        printf("Case %d: The total value of the hook is %d.\n",i,tree[1].sum);
     }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值