CF1569D.Inconvenient Pairs

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

The city has nn vertical and mm horizontal streets that goes across the whole city, i. e. the ii-th vertical streets goes from (xi,0)(x_i, 0) to (xi,106)(x_i, 10^6) and the jj-th horizontal street goes from (0,yj)(0, y_j) to (106,yj)(10^6, y_j).

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

There are kk persons staying on the streets: the pp-th person at point (xp,yp)(x_p, y_p) (so either xpx_p equal to some xix_i or ypy_p equal to some yjy_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)(x, y) and (y,x)(y, x) are the same pair).

Let's recall that Manhattan distance between points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) is ∣x1−x2∣+∣y1−y2∣|x_1 - x_2| + |y_1 - y_2|.

存在一座城市,其形状可表示为一个正方形网格,左下角和右上角的坐标分别为 (0,0)(0, 0) 和 (106,106)(10^6, 10^6)。

该城市共有 nn 条纵向街道和 mm 条横向街道,贯穿整座城市:第 ii 条纵向街道从 (xi,0)(x_i, 0) 延伸至 (xi,106)(x_i, 10^6);第 jj 条横向街道从 (0,yj)(0, y_j) 延伸至 (106,yj)(10^6, y_j)。

所有街道均为双向通行。城市的边界本身也视为街道。

有 kk 个人位于街道上:第 pp 个人位于点 (xp,yp)(x_p, y_p)(即满足 xp=xix_p = x_i(对某个 ii)或 yp=yjy_p = y_j(对某个 jj),或两者同时成立)。

我们称一对人构成不便对(inconvenient pair),当且仅当他们之间仅沿街道行走的最短路径长度严格大于他们之间的曼哈顿距离。

请计算不便对的人数(注意:(x,y)(x, y) 与 (y,x)(y, x) 视为同一对)。

回顾:两点 (x1,y1)(x_1, y_1) 与 (x2,y2)(x_2, y_2) 之间的曼哈顿距离定义为 ∣x1−x2∣+∣y1−y2∣|x_1 - x_2| + |y_1 - y_2|。

输入格式

The first line contains a single integer tt (1≤t≤10001 \le t \le 1000) — the number of test cases.

The first line of each test case contains three integers nn, mm and kk (2≤n,m≤2⋅1052 \le n, m \le 2 \cdot 10^5; 2≤k≤3⋅1052 \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 nn integers x1,x2,…,xnx_1, x_2, \dots, x_n (0=x1<x2<⋯<xn−1<xn=1060 = x_1 \lt x_2 \lt \dots \lt x_{n - 1} \lt x_n = 10^6) — the xx-coordinates of vertical streets.

The third line contains mm integers y1,y2,…,ymy_1, y_2, \dots, y_m (0=y1<y2<⋯<ym−1<ym=1060 = y_1 \lt y_2 \lt \dots \lt y_{m - 1} \lt y_m = 10^6) — the yy-coordinates of horizontal streets.

Next kk lines contains description of people. The pp-th line contains two integers xpx_p and ypy_p (0≤xp,yp≤1060 \le x_p, y_p \le 10^6; xp∈x1,…,xnx_p \in {x_1, \dots, x_n} or yp∈y1,…,ymy_p \in {y_1, \dots, y_m}) — the coordinates of the pp-th person. All points are distinct.

It guaranteed that sum of nn doesn't exceed 2⋅1052 \cdot 10^5, sum of mm doesn't exceed 2⋅1052 \cdot 10^5 and sum of kk doesn't exceed 3⋅1053 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000)—— 测试用例的数量。

每个测试用例的第一行包含三个整数 nn、mm 和 kk(2≤n,m≤2⋅1052 \le n, m \le 2 \cdot 10^5;2≤k≤3⋅1052 \le k \le 3 \cdot 10^5)—— 分别表示竖直街道和水平街道的数量,以及人员数量。

每个测试用例的第二行包含 nn 个整数 x1,x2,…,xnx_1, x_2, \dots, x_n(0=x1<x2<⋯<xn−1<xn=1060 = x_1 \lt x_2 \lt \dots \lt x_{n - 1} \lt x_n = 10^6)—— 表示竖直街道的 xx 坐标。

第三行包含 mm 个整数 y1,y2,…,ymy_1, y_2, \dots, y_m(0=y1<y2<⋯<ym−1<ym=1060 = y_1 \lt y_2 \lt \dots \lt y_{m - 1} \lt y_m = 10^6)—— 表示水平街道的 yy 坐标。

接下来的 kk 行描述人员信息。第 pp 行包含两个整数 xpx_p 和 ypy_p(0≤xp,yp≤1060 \le x_p, y_p \le 10^6;且满足 xp∈{x1,…,xn}x_p \in \{x_1, \dots, x_n\} 或 yp∈{y1,…,ym}y_p \in \{y_1, \dots, y_m\})—— 表示第 pp 个人的坐标。所有点互不相同。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5,mm 的总和不超过 2⋅1052 \cdot 10^5,kk 的总和不超过 3⋅1053 \cdot 10^5。

输出格式

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

对于每个测试用例,输出不方便的点对的数量。

输入输出样例

  • 输入#1

    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

    输出#1

    2
    5

说明/提示

The second test case is pictured below:

For example, points 33 and 44 form an inconvenient pair, since the shortest path between them (shown red and equal to 77) is greater than its Manhattan distance (equal to 55).

Points 33 and 55 also form an inconvenient pair: the shortest path equal to 10000011000001 (shown green) is greater than the Manhattan distance equal to 999999999999.

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

第二个测试用例如下图所示:

例如,点 33 和点 44 构成一对“不便点对”,因为它们之间的最短路径(图中以红色标出,长度为 77)大于其曼哈顿距离(等于 55)。

点 33 和点 55 同样构成一对“不便点对”:其最短路径长度为 10000011000001(图中以绿色标出),大于其曼哈顿距离 999999999999。

但点 55 和点 99 并不构成“不便点对”,因为它们之间的最短路径(图中以紫色标出)等于其曼哈顿距离。

输入解题思路,AI测评打分。不知道怎么写?

首页