hdoj 1698 Just a hook!

Just a hook!

题目意思很清楚,就是对某个区间的数全部更改为某个值。只进行一次查询。用线段树来实现。由于要不断的更改,所以我们这里采用lazy思想,即标记延迟。在更改的时候,并不更改到每个节点,而只是更改到某个区间上,用一个变量记录更改量。当进行下次更改的时候,在更改。程序如下:

#include<stdio.h>

#define MAXN 100005

struct Node{
	int l ;
	int r ;
	int flag ;
	int nsum ;
}node[MAXN*4] ;

int n ;
int m ;

void work(int test) ;
void update(int l , int r , int val , int c) ;
void build(int l , int r , int c) ;
void pushdown(int root) ;
void pushup(int root) ;

int main(int argc , char * argv[]){
	int test  ;
	int cases ;
	
	scanf("%d" ,&test) ;
	cases = 1 ;
	
	while(cases <= test){
		work(cases) ;
		cases ++ ;
	}	
	
	return 0 ;
}

void work(int test){
	
	int x ;
	int y ;
	int z ;
	
	scanf("%d %d" , &n , &m) ;
	build(1 , n , 1) ;
	
	while(m--){
		scanf("%d %d %d" , &x , &y , &z) ;
		update(x , y , z , 1) ;
	}
	
	printf("Case %d: The total value of the hook is %d.\n" , test , node[1].nsum) ;
	
	return ;
}

void build(int l , int r , int c){
	
	node[c].l = l ;
	node[c].r = r ;
	node[c].flag = 0 ;
	node[c].nsum = l - r + 1 ;
	
	if(l == r){
		return ;
	}
	
	int  mid = (l + r) >> 1 ;
	
	build(l , mid , c<<1) ;
	build(mid + 1 , r , c<<1|1) ;
	
	return ;
}

void pushup(int root){
	node[root].nsum = node[root<<1].nsum + node[root<<1|1].nsum ;
}
void pushdown(int root){
	
	if(node[root].flag == 0)
		return ;	
		
	node[root<<1].nsum = node[root].flag * (node[root<<1].r - node[root<<1].l + 1) ;
	node[root<<1].flag = node[root].flag ;
	node[root<<1|1].nsum = node[root].flag * (node[root<<1|1].r - node[root<<1|1].l + 1) ;
	node[root<<1|1].flag = node[root].flag ;
	node[root].flag = 0 ;
	
	return ;
}

void update(int l , int r , int val ,int c){
	
	if(l<= node[c].l && node[c].r <= r){
		node[c].flag = val ;
		node[c].nsum = (node[c].r - node[c].l + 1) * val ;
		return ;
	}
	
	pushdown(c) ;
	
	int mid = (node[c].l + node[c].r)>>1 ;
	
	if(mid >= r){
		update(l , r , val , c<<1) ;
	}
	else if(mid < l){
		update(l , r , val , c<<1|1) ;
	}
	else{
		update(l , mid , val , c<<1);
		update(mid + 1 , r , val , c<<1|1);
	}
	
	pushup(c) ;
	
	return ;
} 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值