Just a Hook(区间修改)

HDU 1689

题意:给定序列,默认值为1。m次操作,每次操作把区间 [x,y] 的值改为 z,
求操作完序列的和。

标签:区间修改

分析: 模板题,但是注意多组输入数据,每次要重置 lazy tag

#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <cstdio>
#include <algorithm>
#define INF 0x3f3f3f3f
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int mod=1e9+7;
const int maxn=2e5+10;

struct node {
	ll l,r,k,lz;
} tree[maxn<<2];
ll n,m;

void push_up(ll p){
	tree[p].k=tree[p<<1].k+tree[(p<<1)|1].k;
}

void push_down(ll p){
	if (tree[p].lz){
		tree[p<<1].k=tree[p].lz*(tree[p<<1].r-tree[p<<1].l+1);
		tree[(p<<1)|1].k=tree[p].lz*(tree[(p<<1)|1].r-tree[(p<<1)|1].l+1);
		tree[p<<1].lz=tree[p].lz;
		tree[(p<<1)|1].lz=tree[p].lz;
		tree[p].lz=0;
	}
}

void build(ll p, ll l, ll r) {
	tree[p].l=l;
	tree[p].r=r;
	tree[p].lz=0;	//重置 lazy tag
	if (l==r) {
		tree[p].k=1;
		return;
	}
	ll m=(l+r)>>1;
	build(p<<1,l,m),build((p<<1)|1,m+1,r);
	push_up(p);
}

void update(ll p, ll x, ll y, ll z){
	if (x<=tree[p].l && y>=tree[p].r){
		tree[p].k=z*(tree[p].r-tree[p].l+1);
		tree[p].lz=z;
		return;
	}
	push_down(p);
	ll m=(tree[p].l+tree[p].r)>>1;
	if (x<=m) update(p<<1,x,y,z);
	if (y>m) update((p<<1)|1,x,y,z);
	push_up(p);
}

ll query(ll p, ll x, ll y) {
	if (x<=tree[p].l && y>=tree[p].r) return tree[p].k;
	push_down(p);
	ll m=(tree[p].l+tree[p].r)>>1;
	ll ans=0;
	if (x<=m) ans+=query(p<<1,x,y);
	if (y>m) ans+=query((p<<1)|1,x,y);
	return ans;
}

int main() {

	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int T;
	cin>>T;
	int ca=1;
	while (T--){
		cin>>n>>m;
		build(1,1,n);
		ll X,Y,Z;
		while (m--){			
			cin>>X>>Y>>Z;
			update(1,X,Y,Z);
		}
		cout<<"Case "<<ca++<<": The total value of the hook is "<<query(1,1,n)<<"."<<endl;
	}
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值