作为一名10年dota老玩家,看见屠夫心头一热,想起当年熬夜开黑打dota的日子,然而现在多么苦逼。。。
本题是线段树的模板题,这里的区间更新需要使用延迟标记,不然会超时。每次做这些线段树的时候总会因为一两点细节而错好几次。可能还是自己基础不扎实,写的代码有点少。在阅读别人的程序时也在不断地加深自己对线段树的理解,也学到了好多编码上的细节。主要体现在向下传递延迟标记的函数:
void pushdown(int root,int length)
{
if(tag[root])
{
tag[root<<1]=tag[root<<1|1]=tag[root];
ans[root<<1]=tag[root]*(length-(length>>1));
ans[root<<1|1]=tag[root]*(length>>1);
tag[root]=0;
}
}
这个函数中,首先是判断了当前结点的延迟标记是不是0,若不是0就不用操作了。这点一起我从来没有注意过。第二是这个函数传进来的参数是当前节点所代表的区间的长度,以前我都是传进来区间的起点和终点,然后再对这段区间的左右子区间分别操作。但显然传入区间长度后的操作更加简便。本题完整代码如下:
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=100005;
int ans[maxn<<2],tag[maxn<<2];
void build(int root,int l,int r)
{
tag[root]=0;
if(l==r)
{
ans[root]=1;
return;
}
int mid=(l+r)>>1;
build(root<<1,l,mid);
build(root<<1|1,mid+1,r);
ans[root]=ans[root<<1]+ans[root<<1|1];
}
void pushdown(int root,int length)
{
if(tag[root])
{
tag[root<<1]=tag[root<<1|1]=tag[root];
ans[root<<1]=tag[root]*(length-(length>>1));
ans[root<<1|1]=tag[root]*(length>>1);
tag[root]=0;
}
}
void update(int root,int l,int r,int u_l,int u_r,int val)
{
if(u_l<=l && r<=u_r)
{
tag[root]=val;
ans[root]=(r-l+1)*val;
return;
}
pushdown(root,r-l+1);
int mid=(l+r)>>1;
if(u_l<=mid)
update(root<<1,l,mid,u_l,u_r,val);
if(u_r>mid)
update(root<<1|1,mid+1,r,u_l,u_r,val);
ans[root]=ans[root<<1]+ans[root<<1|1];
}
int main()
{
int t,n,q,x,y,z,casecount;
while(cin>>t)
{
casecount=0;
while(t--)
{
scanf("%d%d",&n,&q);
build(1,1,n);
while(q--)
{
scanf("%d%d%d",&x,&y,&z);
update(1,1,n,x,y,z);
}
printf("Case %d: The total value of the hook is %d.\n",++casecount,ans[1]);
}
}
return 0;
}