Inconvenient Pairs
Contest: Educational Codeforces Round 113 (Rated for Div. 2)
Difficulty rating: 1900
Tags: binary search, data structures, implementation, sortings, two pointers

There is a city that can be represented as a square grid with corner points in $$$(0, 0)$$$ and $$$(10^6, 10^6)$$$.

The city has $$$n$$$ vertical and $$$m$$$ horizontal streets that goes across the whole city, i. e. the $$$i$$$-th vertical streets goes from $$$(x_i, 0)$$$ to $$$(x_i, 10^6)$$$ and the $$$j$$$-th horizontal street goes from $$$(0, y_j)$$$ to $$$(10^6, y_j)$$$.

All streets are bidirectional. Borders of the city are streets as well.

There are $$$k$$$ persons staying on the streets: the $$$p$$$-th person at point $$$(x_p, y_p)$$$ (so either $$$x_p$$$ equal to some $$$x_i$$$ or $$$y_p$$$ equal to some $$$y_j$$$, or both).

Let's say that a pair of persons form an inconvenient pair if the shortest path from one person to another going only by streets is strictly greater than the Manhattan distance between them.

Calculate the number of inconvenient pairs of persons (pairs $$$(x, y)$$$ and $$$(y, x)$$$ are the same pair).

Let's recall that Manhattan distance between points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.

The first line of each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n, m \le 2 \cdot 10^5$$$; $$$2 \le k \le 3 \cdot 10^5$$$) — the number of vertical and horizontal streets and the number of persons.

The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$0 = x_1 < x_2 < \dots < x_{n - 1} < x_n = 10^6$$$) — the $$$x$$$-coordinates of vertical streets.

The third line contains $$$m$$$ integers $$$y_1, y_2, \dots, y_m$$$ ($$$0 = y_1 < y_2 < \dots < y_{m - 1} < y_m = 10^6$$$) — the $$$y$$$-coordinates of horizontal streets.

Next $$$k$$$ lines contains description of people. The $$$p$$$-th line contains two integers $$$x_p$$$ and $$$y_p$$$ ($$$0 \le x_p, y_p \le 10^6$$$; $$$x_p \in \{x_1, \dots, x_n\}$$$ or $$$y_p \in \{y_1, \dots, y_m\}$$$) — the coordinates of the $$$p$$$-th person. All points are distinct.

It guaranteed that sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$, sum of $$$m$$$ doesn't exceed $$$2 \cdot 10^5$$$ and sum of $$$k$$$ doesn't exceed $$$3 \cdot 10^5$$$.

Output format:
For each test case, print the number of inconvenient pairs.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
The second test case is pictured below:

For example, points $$$3$$$ and $$$4$$$ form an inconvenient pair, since the shortest path between them (shown red and equal to $$$7$$$) is greater than its Manhattan distance (equal to $$$5$$$).

Points $$$3$$$ and $$$5$$$ also form an inconvenient pair: the shortest path equal to $$$1000001$$$ (shown green) is greater than the Manhattan distance equal to $$$999999$$$.

But points $$$5$$$ and $$$9$$$ don't form an inconvenient pair, since the shortest path (shown purple) is equal to its Manhattan distance.

Example input:
2
2 2 4
0 1000000
0 1000000
1 0
1000000 1
999999 1000000
0 999999
5 4 9
0 1 2 6 1000000
0 4 8 1000000
4 4
2 5
2 2
6 3
1000000 1
3 8
5 8
8 8
6 8

Example output:
2
5

Editorial:
Firstly, let's look at some point $$$(x_i, y_i)$$$. Let's find closest to it vertical and horizontal lines. We will name the closest vertical lines from left and right as $$$lx$$$ and $$$rx$$$ (and $$$ly$$$ and $$$ry$$$ as closest horizontal lines). So, $$$lx \le x \le rx$$$ and $$$ly \le y \le ry$$$ (we can also note that either $$$lx = rx$$$ or $$$ly = ry$$$).
Now, let's note that if for some other point $$$j$$$ $$$(x_j, y_j)$$$ either $$$lx < x_j < rx$$$ or $$$ly < y_j < ry$$$ then to reach $$$j$$$ from $$$i$$$ we must go reach either $$$lx$$$ or $$$rx$$$ (or, $$$ly$$$ or $$$ry$$$), so the shortest distance will be strictly greater than the Manhattan distance.
If neither $$$lx < x_j < rx$$$ nor $$$ly < y_j < ry$$$, then we can show that it's always possible to find the shortest path equal to the Manhattan distance. As a result, for each point $$$(x_i, y_i)$$$ we should find the number of points $$$(x_j, y_j)$$$ such that $$$j < i$$$ and $$$lx < x_j < rx$$$ or $$$ly < y_j < ry$$$. The exception here is when $$$j$$$ lies on the same line as $$$i$$$, so we should not count such points.
We can note that since either $$$lx = rx$$$ or $$$ly = ry$$$ there is no such point $$$j$$$ that $$$lx < x_j < rx$$$ and $$$ly < y_j < ry$$$ simultaneously, so we can calculate the pairs by $$$x$$$ and $$$y$$$ coordinates independently.
Let's focus on $$$y$$$ coordinates (to calculate for $$$x$$$ coordinates, we can just swap all coordinates). Let's sort all points by $$$x$$$ coordinate. To get rid of the case when points $$$i$$$ and $$$j$$$ lies on the same vertical street, we can group them by $$$x$$$ coordinate and process by group (since we sorted by $$$x$$$, groups are just segments). There are no problems with the case when points lie on the same horizontal street, since then $$$ly = ry$$$ and there are no other $$$y_j$$$ with $$$ly < y_j < ry$$$.
If we store for each horizontal line $$$y_j$$$ the number of point inside the interval $$$(y_i, y_{i + 1})$$$ then, when we need for point $$$i$$$ calculate the number of points $$$j$$$ with $$$j < i$$$ and $$$ly < y_j < ry$$$, we can just ask for value assigned to $$$ly$$$, because $$$ly$$$ and $$$ry$$$ are consecutive elements in the array $$$y$$$.
So, we go through each group two times: first collecting answer, then updating values in appropriate $$$ly$$$-s. Note, that we can calculate $$$ly$$$ and $$$ry$$$ with binary search (using built-in functions).
The resulting complexity is $$$O(n + m + k (\log{k} + \log{n} + \log{m}))$$$.