HDU 1542(Atlantis)

#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
const int MAXN = 205;

struct Line 
{
	double x;
	double y1, y2;
	int flag;
}line[MAXN];

double y[MAXN];
double wide[MAXN * 4];
int num[MAXN * 4];

int cmp(Line a, Line b) 
{
	return a.x < b.x;
}

void build(int l, int r, int rt) 
{
	wide[rt] = 0;
	num[rt] = 0;
	if (l == r)
		return;
	int m = (l + r) / 2;
	build(l, m, rt << 1);
	build(m + 1, r, rt << 1 | 1);
}

void pushdown(int l, int r, int rt)
{
	if (num[rt] > 0)
		wide[rt] = y[r + 1] - y[l];
	else if (l == r)
		wide[rt] = 0;
	else
		wide[rt] = wide[rt * 2] + wide[rt * 2 + 1];
}

void update(int a, int b, int c, int l, int r, int rt)
{
	if (a <= l && b >= r) 
	{
		num[rt] += c;
		pushdown(l, r, rt);
		return;
	}
	int m = (l + r) / 2;
	if (a <= m)
		update(a, b, c, l, m, rt << 1);
	if (b > m)
		update(a, b, c, m + 1, r, rt << 1 | 1);
	pushdown(l, r, rt);
}

void addLine(double x, double y1, double y2, int flag, int cnt)
{
	line[cnt].x = x;
	line[cnt].y1 = y1;
	line[cnt].y2 = y2;
	line[cnt].flag = flag;
}

int main()
{
	int Case = 1;
	int n;
	while (cin >> n)
	{
		if (n == 0)
			break;

		double x1, x2, y1, y2;
		int cnt = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> x1 >> y1 >> x2 >> y2;
			addLine(x1, y1, y2, 1, cnt);
			y[cnt++] = y1;
			addLine(x2, y1, y2, -1, cnt);
			y[cnt++] = y2;
		}

		sort(line, line + cnt, cmp);
		sort(y, y + cnt);

		int uniqueN = unique(y, y + cnt) - y;
		build(0, uniqueN - 1, 1);
		double ans = 0;
		for (int i = 0; i < cnt - 1; i++)
		{
			int left = lower_bound(y, y + uniqueN, line[i].y1) - y;
			int right = lower_bound(y, y + uniqueN, line[i].y2) - y - 1;
			update(left, right, line[i].flag, 0, uniqueN - 1, 1);
			ans += wide[1] * (line[i + 1].x - line[i].x);
		}

		cout << "Test case #" << Case++ << endl;
		cout << "Total explored area: " << fixed << setprecision(2) << ans << endl;
		cout << endl;
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章