HDU 1698 --- Just a Hook:线段树(区间更新)

Just a Hook
思路

线段树区间修改。

代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<map>
#include<queue>
#include<string>
#include<vector>
#include<utility>
#include<algorithm>
using namespace std;
const int MAXN=1e5;
int sum[MAXN<<2],arr[MAXN],add[MAXN<<2];
inline int read(){
	int s=0,w=1;
	char ch=getchar();
	for(;ch>'9' || ch<'0';ch=getchar()){
		if(ch=='-')	w=-1;
	}
	for(;ch<='9' && ch>='0';ch=getchar()){
		s=(s<<3)+(s<<1)+(ch^'0');
	}
	return s*w;
}

void pushUp(int rt){
	sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void pushDown(int rt,int ln,int rn){
	add[rt<<1]=add[rt];
	add[rt<<1|1]=add[rt];
	sum[rt<<1]=add[rt]*ln;
	sum[rt<<1|1]=add[rt]*rn;
	add[rt]=0;
}
void build(int rt,int l,int r){
	add[rt]=0;
	if(l==r){
		sum[rt]=1;
		return;
	}
	int m=(l+r)>>1;
	build(rt<<1,l,m);
	build(rt<<1|1,m+1,r);
	pushUp(rt);
}
void update(int rt,int l,int r,int L,int R,int C){
	if(L<=l && r<=R){
		sum[rt]=C*(r-l+1);
		add[rt]=C;
		return;
	}
	int m=(l+r)>>1;
	if(add[rt]!=0)	pushDown(rt,m-l+1,r-m);
	if(L<=m)	update(rt<<1,l,m,L,R,C);
	if(m<R)		update(rt<<1|1,m+1,r,L,R,C);
	pushUp(rt);
}
int query(int rt,int l,int r,int L,int R){
	if(L<=l && r<=R){
		return sum[rt];
	}
	int m=(l+r)>>1;
	if(add[rt]!=0)	pushDown(rt,m-l+1,r-m);
	int ans=0;
	if(L<=m)	ans+=query(rt<<1,l,m,L,R);
	if(m<R)		ans+=query(rt<<1|1,m+1,r,L,R);
	return ans;
}
int main(){
	int t;
	t=read();
	for(int pos=1;pos<=t;pos++){
		int n,m;
		n=read();
		build(1,1,n);
		m=read();
		while(m--){
			int L,R,C;
			L=read();R=read();C=read();
			update(1,1,n,L,R,C);
		}
		printf("Case %d: The total value of the hook is %d.\n",pos,query(1,1,n,1,n));
	}
		
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星空皓月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值