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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值