HDU 1542 Atlantis(矩形面积并)

HDU 1542 Atlantis

题目链接

题意:给定一些矩形,求面积并

思路:利用扫描线,因为这题矩形个数不多,直接暴力扫就能够了。假设数据大。就要用线段树

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

const int N = 205;
const int M = 100005;
const double eps = 1e-8;

int n, vis[N], hn;
double hash[N];

struct Line {
	double l, r, y;
	int flag;
	Line() {}
	Line(double l, double r, double y, int flag) {
		this->l = l;
		this->r = r;
		this->y = y;
		this->flag = flag;
	}
} line[N];

bool cmp(Line a, Line b) {
	return a.y < b.y;
}

int get(double x) {
	return lower_bound(hash, hash + hn, x) - hash;
}

int main() {
	int cas = 0;
	while (~scanf("%d", &n) && n) {
		double x1, x2, y1, y2; hn = 0;
		memset(vis, 0, sizeof(vis));
		for (int i = 0; i < n; i++) {
			scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
			line[i * 2] = Line(x1, x2, y1, 1);
			line[i * 2 + 1] = Line(x1, x2, y2, -1);
			hash[hn++] = x1; hash[hn++] = x2;
		}
		n *= 2;
		sort(line, line + n, cmp);
		sort(hash, hash + hn);
		hn = 1;
		for (int i = 1; i < n; i++) {
			if (fabs(hash[i] - hash[i - 1]) < eps) continue;
			hash[hn++] = hash[i];
		}
		double ans = 0;
		for (int i = 0; i < n; i++) {
			int l = get(line[i].l), r = get(line[i].r);
			double len = 0;
			for (int j = 0; j < hn - 1; j++) if (vis[j] > 0) len += (hash[j + 1] - hash[j]);
			if (i) ans += len * (line[i].y - line[i - 1].y);
			for (int j = l; j < r; j++) vis[j] += line[i].flag;
		}
		printf("Test case #%d\n", ++cas);
		printf("Total explored area: %.2lf\n\n", ans);
	}
	return 0;
}
时间: 2024-10-28 23:49:58

HDU 1542 Atlantis(矩形面积并)的相关文章

hdu 1542 Atlantis(矩形面积并)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8806    Accepted Submission(s): 3788 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

HDU 1542 —— Atlantis 【矩形面积并:扫描线】

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=14795 #include <iostream> #include <cstring> #include <string> #include <algorithm> #define left rt<<1 #define right rt<<1|1 using namespace std; const int MAX

HDU 1542 Atlantis (线段树求矩阵覆盖面积)

题意:给你n个矩阵求覆盖面积. 思路:看了别人的结题报告 给定一个矩形的左下角坐标和右上角坐标分别为:(x1,y1).(x2,y2),对这样的一个矩形,我们构造两条线段,一条定位在x1,它在y坐标的区间是[y1,y2],并且给定一个cover域值为1:另一条线段定位在x2,区间一样是[y1,y2],给定它一个cover值为-1.根据这样的方法对每个矩形都构造两个线段,最后将所有的线段根据所定位的x从左到右进行排序 #include <iostream> #include <stdio.h

hdu 1542 Atlantis(线段树&amp;扫描线&amp;面积并)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6386    Accepted Submission(s): 2814 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

HDU 1542 Atlantis 线段树+离散化+扫描线

题意:给出一些矩形的最上角坐标和右下角坐标,求这些矩形的面积并. NotOnlySuccess 线段树专辑中扫描线模板题,弱智的我对着大大的代码看了一下午才搞懂. 具体见思路见注释=.= #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #define lson rt<<1,l,mid #define rson rt<<1|1,mid

HDU 1542 Atlantis(线段树扫描线)

http://acm.hdu.edu.cn/showproblem.php?pid=1542 Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6788    Accepted Submission(s): 2970 Problem Description There are several ancient Greek

hdu 1542 Atlantis(线段树)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6899    Accepted Submission(s): 3022 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

线段树+扫描线+离散化解poj1151 hdu 1542 ( Atlantis )

受此链接很大启发才明白扫描线: http://www.cnblogs.com/scau20110726/archive/2013/04/12/3016765.html 我的代码如下: #include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #i

HDU 1542 Atlantis (求矩形面积并)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) 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