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);
}
}