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

Description


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;
}

UVA - 11983 Weird Advertisement (线段树求并面积),布布扣,bubuko.com

时间: 2024-08-05 07:03:42

UVA - 11983 Weird Advertisement (线段树求并面积)的相关文章

UVA 11983 Weird Advertisement --线段树求矩形问题

题意:给出n个矩形,求矩形中被覆盖K次以上的面积的和. 解法:整体与求矩形面积并差不多,不过在更新pushup改变len的时候,要有一层循环,来更新tree[rt].len[i],其中tree[rt].len[i]表示覆盖次数大于等于i的线段长度,以便求面积,最后只要每次都用tree[1].len[K]来算面积即可. 代码: #include <iostream> #include <cmath> #include <iostream> #include <cst

UVA 11983 - Weird Advertisement(线段树)

UVA 11983 - Weird Advertisement 题目链接 题意:给定几个矩形,之后,求覆盖k次以上的点 思路:先把坐标离散化掉,然后把每个矩形x2,y1加一,这样就把求点转化为求面积,然后每个矩形拆分成上下两个线段,按y排序之后,从下往上每访问一条线段(第一条除外),答案就加上目前整条线段上次数大于等于k的长度乘上这条线段和上一条线段的高度差,然后再用这条线段,根据它是矩形下面还是上面的线段去更新整条线段,维护线段大于等于k的长度这步利用线段树 代码: #include <cst

UVA 11983 Weird Advertisement 线段树+离散化+扫描线

有点像HDU 3642的强化版.给你N个矩形的坐标,问题平面上被k个不同的矩形覆盖的面积是多少. 当初HDU 3642 是直接一个一个手写的,这里的k虽然说只有10,合并过成一个一个手写也是相当蛋疼的,不过仔细想一下,不难推出一般性的关系,然后直接用循环搞就好了.不过我还是因为有个地方忘记初始化WA了2发,真是弱o(╯□╰)o 注意每个房子代表一个点,而我们要把他转化成线段,对坐标进行一些简单的变换即可. #include <cstdio> #include <cstring> #

uva 11983 - Weird Advertisement(线段树)

题目链接:uva 11983 - Weird Advertisement 题目大意:给定n个矩形,问说有多少区域的面积被覆盖k次以上. 解题思路:将每个矩形差分成两条线段,一段为添加覆盖值1,一段为减少覆盖值1,同时记录两段的高度(横坐标).然后对纵坐标离散化建立线段树,然后对线段按照高度排序,维护整段区间中覆盖度大于K的长度,乘上高度上的范围即可. #include <cstdio> #include <cstring> #include <vector> #incl

UVA 11983 Weird Advertisement(线段树求矩形并的面积)

UVA 11983 题目大意是说给你N个矩形,让你求被覆盖k次以上的点的总个数(x,y<1e9) 首先这个题有一个转化,吧每个矩形的x2,y2+1这样就转化为了求N个矩形被覆盖k次以上的区域的面积 由于坐标很大,首先考虑的就是将坐标离散化,然后建立线段树tree[][K],表示x的某个区间被覆盖了K次(大于K次算K次)的实际长度,在计算时把矩形转化为一系列横线段(就是说将一个矩形拆开为两条线段,下面的标记为1,上面的标记为-1(这里的标记很有技巧)),然后将这些线段按照y值的从小到达排序(y值相

POJ&#183;1151 Atlantis&#183;线段树求矩形面积并

题目在这:http://poj.org/problem?id=1151 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the is

uva 11983 Weird Advertisement 扫描线

Weird Advertisement Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=18802 Description 2DPlaneLand is a land just like a huge 2D plane. The range of X axis is 0 to 109 and the range ofY axis

POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并

题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线段树维护两个值:cover和len,cover表示该线段区间目前被覆盖的线段数目,len表示当前已覆盖的线段长度(化为离散前的真值),每次加入一条线段,将其y_low,y_high之间的区间染上line[i].cover,再以tree[1].len乘以接下来的线段的x坐标减去当前x坐标,即计算了一部

线段树求逆序数方法 HDU1394&amp;&amp;POJ2299

为什么线段树可以求逆序数? 给一个简单的序列 9 5 3 他的逆序数是3 首先要求一个逆序数有两种方式:可以从头开始往后找比当前元素小的值,也可以从后往前找比当前元素大的值,有几个逆序数就是几. 线段树就是应用从后往前找较大值得个数.(一边更新一边查) 当前个数是 n = 10 元素   9  5   3 9先加入线段树,T[9]+=1:查从T[9]到T[10]比9大的值,没有sum = 0: 5 加入线段树,T[5] += 1,查从T[5]到T[10]比5大的值,有一个9,sum +=1: 3