扫描线(线段树)

AcWing247. 亚特兰蒂斯

Code

const int N = 1e4 + 4, M = 1e6 + 6;
int n, num;

struct seg
{
	double x, y1, y2;
	int k;

	bool operator < (seg a)const
	{
		return x < a.x;
	}
}s[N * 2];

double yy[N * 2];
int fd(double x)
{
	return lower_bound(yy + 1, yy + num + 1, x) - yy;
}

struct node
{
	int l, r;
	double len;
	int cnt;
	node()
	{
		len = cnt = 0;
	}

#define tp t[p]
#define tl t[p << 1]
#define tr t[p << 1 | 1]
}t[N * 8];

void build(int p, int l, int r)
{
	tp.l = l, tp.r = r;
	if (l == r) return;//注意return
	int mid = l + r >> 1;
	build(p << 1, l, mid), build(p << 1 | 1, mid + 1, r);
}

void up(int p)
{
	if (tp.cnt) tp.len = yy[tp.r + 1] - yy[tp.l];//注意下标
	else if (tp.l == tp.r) tp.len = 0;
	else tp.len = tl.len + tr.len;
}

void change(int p, int l, int r, int k)
{
	if (l <= tp.l && tp.r <= r)
	{
		tp.cnt += k;
		up(p);
		return;
	}
	int mid = tp.l + tp.r >> 1;
	if (l <= mid) change(p << 1, l, r, k);
	if (r > mid) change(p << 1 | 1, l, r, k);
	up(p);
}

int main()
{
	IOS;
	int T = 1;
	while (cin >> n && n)
	{
		cout << "Test case #" << T++ << endl;
		for (int i = 1, j = 0; i <= n; i++)
		{
			double x, y, x1, y1;
			cin >> x >> y >> x1 >> y1;
			yy[++j] = y; yy[++j] = y1;
			s[i * 2 - 1] = { x, y, y1, 1 };
			s[i * 2] = { x1, y, y1, -1 };
		}

		sort(yy + 1, yy + n * 2 + 1);
		num = unique(yy + 1, yy + n * 2 + 1) - yy - 1;
		sort(s + 1, s + n * 2 + 1);

		build(1, 1, num);

		double ans = 0;
		change(1, fd(s[1].y1), fd(s[1].y2) - 1, s[1].k);//注意下标
		for (int i = 2; i <= n * 2; i++)
		{
			ans += t[1].len * (s[i].x - s[i - 1].x);
			change(1, fd(s[i].y1), fd(s[i].y2) - 1, s[i].k);//注意下标
		}

		cout << "Total explored area: ";
	    cout << fixed << setprecision(2) << ans << endl << endl;
	}


	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

to cling

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值