UVA - 11983 Weird Advertisement (线段树求并面积)

Description

Download as PDF

G

Weird Advertisement

Renat Mullakhanov (rem), one of the most talented programmers in the world, passed away on March 11, 2011. This is very sad news for all of us. His team went to ACM ICPC World Finals - 2004, placed 4th and won gold medals. He really was a great programmer. May he rest in peace. This problem is dedicated to him.

2DPlaneLand is a land just like a huge 2D plane. The range of X axis is 0 to 109 and the range of Y axis is also 0 to 109. People built houses only in integer co-ordinates and there is exactly one house in each integer co-ordinate.

Now UseAndSmile Soap Company is launching a new soap. That's why they want to advertise this product as much as possible. So, they selected n persons for this task. Each person will be given a rectangular region. He will advertise the product to all the houses that lie in his region. Each rectangular region is identified by 4 integers x1, y1, x2 and y2. That means this person will advertise in all the houses whose x co-ordinate is between x1 and x2 (inclusive) and y co-ordinate is between y1 and y2 (inclusive).

Now after a while they realized that some houses are being advertised by more than one person. So, they want to find the number of houses that are advertised by at least k persons. Since you are one of the best programmers in the city; they asked you to solve this problem.

Input

Input starts with an integer T (≤ 13), denoting the number of test cases.

Each case starts with a line containing two integers n (1 ≤ n ≤ 30000), k (1 ≤ k ≤ 10). Each of the next n lines will contain 4 integers x1, y1, x2, y2 (0 ≤ x1, y1, x2, y2 ≤ 109, x1 < x2, y1 < y2) denoting a rectangular region for a person.

Output

For each case, print the case number and the total number of houses that are advertised by at least k people.

Sample Input

Output for Sample Input

2
2 1
0 0 4 4
1 1 2 5
2 2
0 0 4 4
1 1 2 5

Case 1: 27

Case 2: 8

 

题意:给你n个矩形,求覆盖大于等于k的点数

思路:将右上角的点x,y都加一就能转换成求并面积了,还是上面的边标记1,下面的边标记-1,传统的线段树求并面积,容易错的地方是在更新的时候,引用胡浩大大的一句话:这里线段树的一个结点并非是线段的一个端点,而是该端点和下一个端点间的线段,所以题目中r+1,r-1的地方可以自己好好的琢磨一下

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#define lson(x) ((x) << 1)
#define rson(x) ((x) << 1 | 1)
#define ll long long
using namespace std;
const int maxn = 60005;
const int M = 160010;

int k;
struct Seg {
	int l, r, h, s;
	Seg() {}
	Seg(int a, int b, int c, int d) {
		l = a, r = b, h = c, s = d;
	}
	bool operator <(const Seg &tmp) const {
		if (h == tmp.h)
			return s > tmp.s;
		return h < tmp.h;
	}
} p[maxn<<2];
int sum[11][M<<2];
int cover[M<<2];
int w[M<<2];;

void pushup(int pos, int l, int r) {
	for (int i = 0; i <= k; i++)	
		sum[i][pos] = 0;
	if (cover[pos] >= k) {
		sum[k][pos] = w[r+1] - w[l];
		return;
	}
	if (r == l) {
		sum[cover[pos]][pos] = w[r+1] - w[l];
		return;
	}
	int tmp = 0;
	for (int i = 0; i <= k; i++) {
		if (i + cover[pos] < k)
			sum[i+cover[pos]][pos] = sum[i][lson(pos)] + sum[i][rson(pos)];
		else sum[k][pos] += sum[i][lson(pos)] + sum[i][rson(pos)];
	}
	for (int i = 1; i <= k; i++)
		tmp += sum[i][pos];
	sum[0][pos] = w[r+1] - w[l] - tmp;
}

void update(int L, int R, int l, int r, int pos, int op) {
	if (L <= l && R >= r) {
		cover[pos] += op;
		pushup(pos, l, r);
		return;
	}
	int m = l + r >> 1;
	if (L <= m)
		update(L, R, l, m, lson(pos), op);
	if (R > m)
		update(L, R, m+1, r, rson(pos), op);
	pushup(pos, l, r);
}

int search(int x, int r) {
	int l = 0;
	while (l <= r) {
		int m = l + r >> 1;
		if (w[m] == x)
			return m;
		else if (w[m] < x)
			l  = m + 1;
		else r = m - 1;
	}
	return -1;
}

void build(int l, int r, int pos) {
	sum[0][pos] = w[r+1] - w[l];
	if (l == r)
		return;
	int m = l + r >> 1;
	build(l, m, lson(pos));
	build(m+1, r, rson(pos));
}

int main() {
	int t, n, m, cas = 1;
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d", &n, &k);
		memset(cover, 0, sizeof(cover));
		memset(sum, 0, sizeof(sum));
		int tot = 0;
		int a, b, c, d;
		for (int i = 0; i < n; i++) {
			scanf("%d%d%d%d", &a, &b, &c, &d);
			c++, d++;
			p[tot] = Seg(a, c, b, 1);
			w[tot++] = a;
			p[tot] = Seg(a, c, d, -1);
			w[tot++] = c;
		}
		if (k > n) {
			printf("Case %d: 0\n", cas++);
			continue;
		}
		sort(w, w+tot);
		sort(p, p+tot);
		int cnt = unique(w, w+tot) - w;
		build(0, cnt-1, 1);
		ll ans = 0;
		for (int i = 0; i < tot-1; i++) {
			int l = search(p[i].l, cnt-1);
			int r = search(p[i].r, cnt-1)-1;
			update(l, r, 0, cnt-1, 1, p[i].s);
			ans += (ll)(sum[k][1] * (ll)(p[i+1].h - p[i].h));
		}
		printf("Case %d: ", cas++);
		printf("%lld\n", ans);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"text/plain;charset=UTF-8" 是一个Content-Type的标识,用于指定请或响应的内容类型。在引用和中提到的问题中,"text/plain;charset=UTF-8" 表示请中的内容类型不受支持或响应中的内容类型不正确。 通常,"text/plain;charset=UTF-8" 表示纯文本内容,使用UTF-8编码。如果您在使用Postman发送POST请时遇到此错误消息,可能是因为您没有正确设置请的头部信息或请体的内容类型。 要解决此问题,您可以尝试以下几种方法: 1. 在Postman中,确保您设置了正确的请头部信息。您可以在请头中添加"Content-Type: text/plain;charset=UTF-8",以指定请的内容类型。 2. 检查您的请体内容是否符合"text/plain;charset=UTF-8" 的格式要。确保您正在发送的是纯文本,并使用UTF-8编码。 3. 如果您正在与某个API进行通信,请参考API文档,查看它所需的正确的内容类型。 需要注意的是,具体的解决方法可能因您的具体情况而有所不同。如果以上方法无法解决您的问题,建议您查看Postman的文档或联系API的提供方以获取更具体的帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Content type ‘text/plain;charset=UTF-8‘ not supported](https://blog.csdn.net/SmartYG/article/details/124824940)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [weird-text](https://download.csdn.net/download/weixin_42097450/15884138)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [RestTemplate远程调用,响应结果中的响应头为content-type:text/plain;charset=utf-8,且数据类型转换失败](https://blog.csdn.net/Staba/article/details/124442200)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值