题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698
注意这个题目要输出的结果是整个区间的,因此不需要query函数,直接输出1节点的值。
#include<cstdio>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define root 1,n,1
const int maxn=100000+5;
int add[maxn<<2];
int S[maxn<<2];
void push_up(int rt)
{
S[rt]=S[rt<<1]+S[rt<<1|1];
}
void push_down(int rt,int dis)
{
if(add[rt])
{
add[rt<<1]=add[rt];
add[rt<<1|1]=add[rt];
S[rt<<1]=add[rt]*(dis-(dis>>1));
S[rt<<1|1]=add[rt]*(dis>>1);
add[rt]=0;
}
}
void build(int l,int r,int rt)
{
add[rt]=0;
S[rt]=1;
if(l==r)
return ;
int m=(l+r)>>1;
build(lson);
build(rson);
push_up(rt);
}
void update(int L,int R,int d,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
add[rt]=d;
S[rt]=(r-l+1)*d;
return ;
}
push_down(rt,r-l+1);
int m=(l+r)>>1;
if(L<=m)
update(L,R,d,lson);
if(m<R)
update(L,R,d,rson);
push_up(rt);
}
int main()
{
int t,i;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
int n,q;
scanf("%d%d",&n,&q);
build(root);
while(q--)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
update(x,y,z,root);
}
printf("Case %d: The total value of the hook is %d.\n",i,S[1]);
}
return 0;
}