QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 512 MB Total points: 100

#5433. 绝对差值

Statistics

Alice 和 Bob 各自拥有一个实数集合,这两个集合都是若干个不相交闭区间的并集。他们将各自从自己的集合中独立地均匀随机选取一个实数,你需要计算这两个实数之差的绝对值的期望。

更正式地说,给定一个实数集合 $S = \bigcup [l, r]$,从集合 $S$ 中均匀随机选取一个实数 $x$ 意味着对于 $S$ 中任意两个长度相等的区间 $[l_1, r_1], [l_2, r_2] \subseteq S$,即 $r_1 - l_1 = r_2 - l_2$,都有 $P(x \in [l_1, r_1]) = P(x \in [l_2, r_2])$。

输入格式

第一行包含两个整数 $n$ 和 $m$ ($1 \le n, m \le 10^5$),分别表示构成 Alice 集合和 Bob 集合的区间数量。

接下来的 $n + m$ 行,每行包含两个整数 $l$ 和 $r$ ($-10^9 \le l \le r \le 10^9$),描述一个闭区间 $[l, r]$。前 $n$ 个区间构成 Alice 的集合,后 $m$ 个区间构成 Bob 的集合。注意,当 $l = r$ 时,区间 $[l, r]$ 为退化区间,仅包含一个实数。

保证构成每个人集合的区间两两不相交。

输出格式

输出一个实数,表示 Alice 和 Bob 分别选取的两个实数之差的绝对值的期望。

如果你的答案的绝对误差或相对误差不超过 $10^{-9}$,则视为正确。形式化地说,假设你的输出为 $a$,标准答案为 $b$,当且仅当 $\frac{|a-b|}{\max(1, |b|)} \le 10^{-9}$ 时,你的输出被接受。

样例

输入 1

1 1
0 1
0 1

输出 1

0.333333333333333

输入 2

1 1
0 1
1 1

输出 2

0.5

说明

在第一个样例中,Alice 和 Bob 都可以从 $[0, 1]$ 中选取任意实数,绝对差的期望为 $\int_0^1 \int_0^1 |x - y| \, dx \, dy = \frac{1}{3}$。

在第二个样例中,Alice 可以从 $[0, 1]$ 中选取任意实数,而 Bob 只能选取 $1$,因此绝对差的期望为 $\int_0^1 |x - 1| \, dx = \frac{1}{2}$。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.