HDU 1698 Just a Hook

还得好好理解一下...为什么注释部分去掉 不能AC ? 知道的麻烦指导一下!

  
    
#include < stdio.h >
#define L(x) ((x) << 1)
#define R(x) ((x) << 1 | 1)
const int MAXN = 200000 ;
struct SegTree{
int l,r;
int metal;
int value;
bool lazy;
int getMid(){
return ( l + r ) >> 1 ;
}
int getDis(){
return (r - l + 1 );
}
}tree[MAXN
<< 2 ];
void bulid( int left, int right, int t){
tree[t].l
= left;
tree[t].r
= right;
tree[t].metal
= 1 ;
tree[t].lazy
= false ;
if (left == right){
tree[t].value
= 1 ;
return ;
}
int mid = tree[t].getMid();
bulid(left, mid, L(t));
bulid(mid
+ 1 , right, R(t));
tree[t].value
= tree[L(t)].value + tree[R(t)].value;
}
void update( int left, int right, int metal, int t){
// if(tree[t].metal == metal)
// return;
if (left <= tree[t].l && right >= tree[t].r ){
tree[t].metal
= metal;
tree[t].value
= tree[t].metal * tree[t].getDis();
tree[t].lazy
= true ;
return ;
}
if ( tree[t].lazy ){
tree[L(t)].metal
= tree[R(t)].metal = tree[t].metal;
tree[L(t)].value
= tree[L(t)].getDis() * tree[L(t)].metal;
tree[R(t)].value
= tree[R(t)].getDis() * tree[R(t)].metal;
tree[L(t)].lazy
= tree[R(t)].lazy = true ;
tree[t].lazy
= false ;
}
int mid = tree[t].getMid();
if ( right <= mid ){
update(left, right, metal, L(t));
}
else if ( left > mid ){
update(left, right, metal, R(t));
}
else {
update(left, mid, metal, L(t));
update(mid
+ 1 , right, metal, R(t));
}
tree[t].value
= tree[L(t)].value + tree[R(t)].value;
}
int main(){
int cas, n, q;
scanf(
" %d " , & cas);
for ( int i = 1 ; i <= cas; ++ i){
scanf(
" %d " , & n);
bulid(
1 , n, 1 );
scanf(
" %d " , & q);
while ( q -- ){
int x, y, z;
scanf(
" %d%d%d " , & x, & y, & z);
update(x, y, z,
1 );
}
printf(
" Case %d: The total value of the hook is %d.\n " ,i,tree[ 1 ].value);
}
return 0 ;
}

你可能感兴趣的:(HDU)