列表

详情


NC230850. A Bite of Teyvat

描述

Xiangling, one of the greatest chef in Teyvat, is preparing for the Moonchase banquet. Xiangling has bought n round plates and her friend and companion Guoba will help place these n plates on the table in a line. The i-th plate placed has radius r_i and the center of this plate locates at (x_i, 0) on the table.

However, Paimon the emergency food has been tired of waiting for the banquet a long time and begins finding the total area covered by the plates on the table after each placement.


Pixiv ID: 93526437

输入描述

The first line contains an integer n , indicating the number of plates Xiangling has bought.

Then follow n lines, the i-th of which contains two integers x_i and r_i , indicating that the i-th plate placed by Guoba has radius r_i and the center of this plate locates at (x_i, 0) on the table.

输出描述

Output n lines, the i-th of which contains a real number, indicating the total area covered by the plates on the table after Guoba places the first i-th plates.

Your answer is acceptable if its absolute or relative error does not exceed . Formally speaking, suppose that your output is x and the jury's answer is y, your output is accepted if and only if .

示例1

输入:

4
0 1
2 1
3 1
1 1

输出:

3.141592653589793
6.283185307179586
8.196408262160623
8.881261518532902

说明:

In the sample case:
1.The total area covered by the first plate is \pi;

2.The total area covered by the first two plates is 2\pi;

3.The total area covered by the first three plates is \frac{14\pi+3\sqrt{3}}{6};

4.The total area covered by all the four plates is \frac{4\pi+3\sqrt{3}}{2}.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 330ms, 内存消耗: 10580K, 提交时间: 2021-11-22 11:47:06

#include <bits/stdc++.h>
using namespace std;

typedef long long s64;
s64 sqr(int x) {
	return s64(x) * x;
}
const double PI = acos(-1);
const int N = 1e5 + 5;
int x[N], r[N];
double la[N], ra[N];
int q[N], pos[N];
set<int> S;
double ans;
double area(int i) {
	if (la[i] <= ra[i]) return 0;
	return sqr(r[i]) * (la[i] - ra[i] + (sin(2 * ra[i]) - sin(2 * la[i])) / 2) / 2;
}
void check(int pl, int pr) {
	int d = x[pr] - x[pl];
	int r1 = r[pl], r2 = r[pr];
	if (d >= r1 + r2) return;
	ans -= area(pl) + area(pr);
	if (r2 >= r1 + d) {
		ra[pl] = PI;
	} else if (r1 >= r2 + d) {
		ra[pr] = PI;
	} else {
		double lx = (d + double(sqr(r1) - sqr(r2)) / d) / 2;
		ra[pl] = max(ra[pl], acos(lx / r1));
		la[pr] = min(la[pr], acos((-d + lx) / r2));
	}
	ans += area(pl) + area(pr);
}
void add(int i) {
	auto it = S.insert(pos[i]).first;
	la[i] = PI;
	ra[i] = 0;
	ans += area(i);
	while (it != S.begin()) {
		auto it0 = it;
		--it0;
		int pl = q[*it0];
		check(pl, i);
		if (la[i] <= ra[i]) return;
		if (la[pl] > ra[pl]) break;
		S.erase(it0);
	}
	while (1) {
		auto it1 = it;
		++it1;
		if (it1 == S.end()) break;
		int pr = q[*it1];
		check(i, pr);
		if (la[i] <= ra[i]) return;
		if (la[pr] > ra[pr]) break;
		S.erase(it1);
	}
}

int main() {
#ifdef kcz
	freopen("1.in", "r", stdin); //freopen("1.out","w",stdout);
#endif
	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		scanf("%d%d", x + i, r + i);
		q[i] = i;
	}
	sort(q + 1, q + n + 1, [&](int a, int b) { return x[a] < x[b]; });
	for (int i = 1; i <= n; ++i) pos[q[i]] = i;
	for (int i = 1; i <= n; ++i) {
		add(i);
		printf("%.9f\n", ans * 2);
	}
}

上一题