P16623 [CodeChef] TOVERLP
提高+
题目描述

Read problems statements in [Hindi], [Mandarin Chinese], [Vietnamese], and [Bengali] as well.

You are given two sets of segments on a line, $A$ and $B$. Set $A$ contains $N$ segments (numbered $1$ through $N$); for each valid $i$, the $i$-th of these segments is $S_{A,i} = [L_{A,i}, R_{A,i}]$. Set $B$ contains $M$ segments (numbered $1$ through $M$); for each valid $i$, the $i$-th of these segments is $S_{B,i} = [L_{B,i}, R_{B,i}]$.

Find $\sum_{i=1}^N \sum_{j=1}^M |S_{A,i} \cap S_{B,j}|$, where $|S_{A,i} \cap S_{B,j}|$ denotes the length of the intersection of the $i$-th segment from set $A$ and the $j$-th segment from set $B$. Note that the length of a segment $[l, r]$ is $r-l$.

------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
$N$ lines follow. For each valid $i$, the $i$-th of these lines contains two space-separated integers $L_{A,i}$ and $R_{A,i}$.
$M$ more lines follow. For each valid $i$, the $i$-th of these lines contains two space-separated integers $L_{B,i}$ and $R_{B,i}$.

------ Output ------
For each test case, print a single line containing one integer ― the value of the sum.

------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N, M ≤ 10^{5}$
$1 ≤ L_{A,i} < R_{A,i} ≤ 10^{8}$ for each valid $i$
$1 ≤ L_{B,i} < R_{B,i} ≤ 10^{8}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$
the sum of $M$ over all test cases does not exceed $2 \cdot 10^{5}$

------ Example Input ------

3
2 2
1 2
3 4
1 3
2 4
1 1
1 2
3 4
2 1
5 6
6 8
5 8

------ Example Output ------

2
0
3

------ Explanation ------
Example case 1: The intersection of $S_{A,1}$ and $S_{B,1}$ is the segment $[1, 2]$. The intersection of $S_{A,2}$ and $S_{B,2}$ is $[3, 4]$. Both remaining intersections have lengths $0$, so the sum is $1 + 1 = 2$.

Example case 2: The only two segments have an empty intersection, which has length $0$.

Example case 3: The segment $[5, 8]$ is covered by both sets of segments, so the sum is $3$.

输入格式

见题面

输出格式

见题面

样例输入
3 2 2 1 2 3 4 1 3 2 4 1 1 1 2 3 4 2 1 5 6 6 8 5 8
样例输出
2 0 3