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

HDU 1689


模板题。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int N, Q;
const int MAXN = 100100;
struct node {
	int l, r, m, val, book;
};
node T[4 * MAXN];
void build(int rt, int begin, int end) {
	T[rt].l = begin, T[rt].r = end, T[rt].m = (begin + end) >> 1, T[rt].book = 0;
	if(begin == end) {
		T[rt].val = 1;
		return;
	}
	build(rt << 1, begin, T[rt].m);
	build(rt << 1 | 1, T[rt].m + 1, end);
	T[rt].val = T[rt << 1].val + T[rt << 1 | 1].val;
}
void updata(int rt, int l, int r, int num) {
	if(l == T[rt].l && r == T[rt].r) {
		T[rt].book = num;
		T[rt].val = num * (T[rt].r - T[rt].l + 1);
		return;
	}
	if(T[rt].book) {
		T[rt << 1].book = T[rt].book;
		T[rt << 1 | 1].book = T[rt].book;
		T[rt << 1].val = T[rt].book * (T[rt << 1].r - T[rt << 1].l + 1);
		T[rt << 1 | 1].val = T[rt].book * (T[rt << 1 | 1].r - T[rt << 1 | 1].l + 1);
		T[rt].book = 0;
	}
	if(l > T[rt].m) updata(rt << 1 | 1, l, r, num);
	else if(r <= T[rt].m) updata(rt << 1, l, r, num);
	else {
		updata(rt << 1, l, T[rt].m, num);
		updata(rt << 1 | 1, T[rt].m + 1, r, num);
	}
	T[rt].val = T[rt << 1].val + T[rt << 1 | 1].val;
}
int main() {
	int t;
	while(~scanf("%d", &t)) {
		int k = 1;
		while(t--) {
			scanf("%d%d", &N, &Q);
			build(1, 1, N);
			int i;
			int x, y, z;
			for(i = 0; i < Q; i++) {
				scanf("%d %d %d", &x, &y, &z);
				updata(1, x, y, z);
			}
			printf("Case %d: The total value of the hook is %d.\n", k++, T[1].val);
		}
	}
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值