Just a Hook(线段树,成段更新)

题目传送门

Just a Hook

题目大意

T组数据
每组有一个长度为n的序列,给你m次操作,每次操作给你x,y,z三个数,操作:将x到y的数字变为z
m次操作完成后求序列的总和

思路

很明显的线段树题目,维护区间和即可
注意一下更新区间的方法即可:
对于修改不能直接更新到底部,要成段更新

AC Code

#include<bits/stdc++.h>
using namespace std;
#define debug(a) cout<<#a<<"="<<a<<endl;
const int N=1e5 +9;
int T, n, m, x, y ,z;
struct segtree{
    int l, r;
	int sum, tag;
}tr[N<<2];
inline int read()
{
    int ans=0;
    char last=' ',ch=getchar();
    while(ch<'0'||ch>'9') last=ch,ch=getchar();
    while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    if(last=='-') ans=-ans;
    return ans;
}
inline int lc(int p) {return p<<1;}
inline int rc(int p) {return p<<1|1;}
inline void push_up(int p)  {tr[p].sum=tr[lc(p)].sum+tr[rc(p)].sum;}
inline void build(int p, int l, int r){
    tr[p].l=l, tr[p].r=r;
	tr[p].tag=tr[p].sum=0;
	if(l==r)	{tr[p].sum=1; return ;}
	int mid=(l+r)>>1;
	build(lc(p), l, mid);
	build(rc(p), mid+1, r);
    push_up(p);
}
inline void f(int p, int k){
	tr[p].sum=k*(tr[p].r-tr[p].l+1);
	tr[p].tag=k;
}
inline void push_down(int p){
	f(lc(p), tr[p].tag);
	f(rc(p), tr[p].tag);
	tr[p].tag=0;
}

inline void updata(int p, int l, int r, int x, int y, int k){
	if(x>r || y<l)	return ;
	if(l<=x && y<=r){
		tr[p].sum=k*(y-x+1);
		tr[p].tag=k;
		return ;
	}
	if(tr[p].tag) push_down(p);
	int mid=(x+y)>>1;
	updata(lc(p), l, r, x, mid, k);
	updata(rc(p), l, r, mid+1, y, k);
	push_up(p);
}
int main(){
	T=read();
	int k=0;
	while(T--){
		n=read(); m=read();
        memset(tr, 0, sizeof(tr));
		build(1,1,n);
		for(int i=1; i<=m; i++){
			x=read(); y=read(); z=read();
			updata(1,x,y,1,n,z);
		}
		printf("Case %d: The total value of the hook is %d.\n", ++k, tr[1].sum); 
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值