题目意思很清楚,就是对某个区间的数全部更改为某个值。只进行一次查询。用线段树来实现。由于要不断的更改,所以我们这里采用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 ;
}