【集训DAY4】矩形【线段树】

48 篇文章 0 订阅
24 篇文章 0 订阅

在这里插入图片描述

思路:

我们以x轴为加入时间,y轴为坐标轴建立线段树
在x处加a[i],然后在x+w处减去a[i]。

c o d e code code

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

int n, w, h;
int s[100010], tot, cnt;

struct node {
	int x, y1, y2, w;
}q[100010];

struct abc {
	int maxx, l, r, flag;
}tree[400010];

bool cmp(node x, node y) {
	return (x.x < y.x) || (x.x == y.x && y.w < 0);
}

void build(int k, int l, int r) {
	tree[k].l = s[l], tree[k].r = s[r];
	if(r - l == 1) return;
	int mid = l + r >> 1;
	build(k << 1, l, mid);
	build(k << 1 | 1, mid, r);
	tree[k].maxx = max(tree[k << 1].maxx, tree[k << 1 | 1].maxx);
}

void update(int k, int x, int y, int w) {
	if(x <= tree[k].l && tree[k].r <= y) {
		tree[k].flag += w;
		tree[k].maxx += w;
		return ;
	}
	if(x < tree[k << 1].r) update(k << 1, x, min(y, tree[k << 1].r), w);
	if(y > tree[k << 1 | 1].l) update(k << 1 | 1, max(x, tree[k << 1 | 1].l), y, w);
	tree[k].maxx = max(tree[k << 1].maxx, tree[k << 1 | 1].maxx) + tree[k].flag;
}

int main() {
	scanf("%d%d%d", &n, &w, &h);
	for(int i = 1; i <= n; i ++) {
		int x, y, z;
		scanf("%d%d%d", &x, &y, &z);
		q[++ tot].x = x, q[tot].y1 = y, q[tot].y2 = y + h, q[tot].w = z;
		q[++ tot].x = x + w, q[tot].y1 = y, q[tot].y2 = y + h, q[tot].w = -z;
		s[++ cnt] = y, s[++ cnt] = y + h;
	}
	sort(s + 1, s + 1 + cnt);
	cnt = unique(s + 1, s + cnt) - s - 1;
	sort(q + 1, q + 1 + tot, cmp);
	build(1, 1, cnt);
	int ans = 0;
	for(int i = 1; i <= tot; i ++) {
		update(1, q[i].y1, q[i].y2, q[i].w);
		ans = max(ans, tree[1].maxx);
	}
	printf("%d", ans);
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值