QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#578843#5433. Absolute DifferencezzyNorthPoleWA 0ms1612kbC++144.1kb2024-09-20 21:51:092024-09-20 21:51:09

Judging History

你现在查看的是最新测评结果

  • [2024-09-20 21:51:09]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:1612kb
  • [2024-09-20 21:51:09]
  • 提交

answer

#include <cstdio>
#include <algorithm>

const int N = 1e5 + 5;
typedef long long LL;
std::pair <int, int> a[N], b[N];
LL sqr(int x) {
	return 1ll * x * x;
}
LL calc(int p, int q, int s, int t) {
	return (sqr(t) - sqr(s)) * (q - p) - (sqr(q) - sqr(p)) * (t - s);
}
int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	LL lena = 0, lenb = 0;
	for (int i = 1; i <= n; ++i) {
		scanf("%d%d", &a[i].first, &a[i].second);
		lena += a[i].second - a[i].first;
	}
	for (int i = 1; i <= m; ++i) {
		scanf("%d%d", &b[i].first, &b[i].second);
		lenb += b[i].second - b[i].first;
	}
	std::sort(a + 1, a + n + 1), std::sort(b + 1, b + m + 1);
	if (lena && lenb) {
		// forward
		LL cnt_1 = 0, cnt_2 = 0, res = 0;
		long double ans = 0;
		for (int i = 1, j = 0; i <= n; ++i) {
			int s = a[i].first, t = a[i].second;
			res += (sqr(t) - sqr(s)) * cnt_1 - cnt_2 * (t - s);
			while (j + 1 <= m && b[j + 1].second <= t) {
				++j;
				int p = b[j].first, q = b[j].second;
				cnt_1 += q - p;
				cnt_2 += sqr(q) - sqr(p);
				if (q <= s) res += calc(p, q, s, t);
				else {
					// q > s
					if (p <= s) res += calc(p, s, s, t) + calc(s, q, q, t);
					else res += calc(s, p, p, q) + calc(p, q, q, t);
				}
			}
			if (j + 1 <= m && b[j + 1].second > t && b[j + 1].first <= s) {
				res += calc(b[j + 1].first, s, s, t);
			}
		}
		ans += (long double) res / 2.0;
		cnt_1 = 0, cnt_2 = 0, res = 0;
		for (int i = n, j = m + 1; i > 0; --i) {
			int s = a[i].first, t = a[i].second;
			res += (sqr(t) - sqr(s)) * cnt_1 - cnt_2 * (t - s);
			while (j - 1 > 0 && b[j - 1].first >= a[i].first) {
				--j;
				int p = b[j].first, q = b[j].second;
				cnt_1 += q - p;
				cnt_2 += sqr(q) - sqr(p);
				if (t <= p) res += calc(s, t, p, q);
				else {
					if (t <= q) res += calc(s, p, p, q) + calc(p, t, t, q); 
				}
			}
			if (j - 1 > 0 && b[j - 1].second > t && b[j - 1].first <= s) {
				res += calc(s, t, t, b[j - 1].second);
			}
		}
		ans += (long double) res / 2.0;
		res = 0;
		for (int i = 1, j = 0; i <= n; ++i) {
			int s = a[i].first, t = a[i].second;
			while (j + 1 <= m && b[j + 1].second <= t) {
				++j;
				int p = b[j].first, q = b[j].second;
				if (q <= s) continue;
				else {
					if (p <= s) res += sqr(q - s) * (q - s);
					else res += sqr(q - p) * (q - p);
				}
			}
			if (j + 1 <= m && b[j + 1].second > t && b[j + 1].first <= s) {
				res += sqr(t - s) * (t - s);
			}
		}
		ans += (long double) res / 3.0;
		ans /= (long double) lena;
		ans /= (long double) lenb;
		printf("%.12Lf", ans);
	}
	else if (!lena && !lenb) {
		LL cnt = 0, res = 0;
		long double ans = 0;
		for (int i = 1, j = 0; i <= n; ++i) {
			while (j + 1 <= m && b[j + 1].first <= a[i].first) ++j, cnt += b[j].first;
			res += 1ll * j * a[i].first - cnt;
		}
		cnt = 0;
		for (int i = n, j = m + 1; i > 0; --i) {
			while (j - 1 > 0 && b[j - 1].first >= a[i].first) --j, cnt += b[j].first;
			res += cnt - 1ll * (m - j + 1) * a[i].first;
		}
		ans = (long double) res / (double) n / (double) m;
		printf("%.12Lf", ans);
	}
	else {
		if (!lena) {
			for (int i = 1; i <= std::max(n, m); ++i) std::swap(a[i], b[i]);
			std::swap(n, m);
			std::swap(lena, lenb);
		}
		LL cnt = 0, res = 0;
		long double ans = 0;
		for (int i = 1, j = 0; i <= n; ++i) {
			int s = a[i].first, t = a[i].second;
			res += (sqr(t) - sqr(s)) * j - cnt * (t - s);
			while (j + 1 <= m && b[j + 1].second <= t) {
				++j;
				int p = b[j].first;
				cnt += 2ll * p;
				if (p <= s) res += (sqr(t) - sqr(s)) - 2ll * p * (t - s);
				else {
					res += (sqr(t) - sqr(p)) - 2ll * p * (t - p);
					res += 2ll * p * (p - s) - (sqr(p) - sqr(s));
				}
			}
		}
		ans += (long double) res / 2.0;
		cnt = 0, res = 0;
		for (int i = n, j = m + 1; i > 0; --i) {
			int s = a[i].first, t = a[i].second;
			res += cnt * (t - s) - (sqr(t) - sqr(s)) * (m + 1 - j);
			while (j - 1 > 0 && b[j - 1].second > t) {
				--j;
				int p = b[j].first;
				cnt += 2ll * p;
				if (p >= t) res += 2ll * p * (t - s) - (sqr(t) - sqr(s));
			}
		}
		ans += (long double) res / 2.0;
		ans /= (double) m;
		ans /= (long double) lena;
		printf("%.12Lf", ans);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 1584kb

input:

1 1
0 1
0 1

output:

0.333333333333

result:

ok found '0.333333333', expected '0.333333333', error '0.000000000'

Test #2:

score: 0
Accepted
time: 0ms
memory: 1612kb

input:

1 1
0 1
1 1

output:

0.500000000000

result:

ok found '0.500000000', expected '0.500000000', error '0.000000000'

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 1592kb

input:

1 1
-1000000000 1000000000
-1000000000 1000000000

output:

-0.008913199869

result:

wrong answer 1st numbers differ - expected: '666666666.6666666', found: '-0.0089132', error = '1.0000000'