8071: Fenced In

内存限制:256 MB 时间限制:1 S
题面:传统 评测方式:文本比较 上传者:
提交:23 通过:7

题目描述

农夫约翰意识到他的很多奶牛都患上了一种奇怪的广场恐惧症(害怕大面积的空地)。

为了尽量减少奶牛们对放牧的恐惧,约翰通过建造垂直(南北)和水平(东西)方向的围栏,将自己的整片田地分割成了若干较小的区域。

整片田地可以看作是一个矩形,矩形的一对对角坐标为 (0,0) (A,B)

约翰在 n 个不同的位置 a1,a2,…,an 处建了 n 个垂直围栏,每个围栏从 (ai,0) 延伸至 (ai,B)

他还在 m 个不同的位置 b1,b2,…,bm 处建了 m 个水平围栏,每个围栏从 (0,bi) 延伸至 (A,bi)

每个垂直围栏都会穿过每个水平围栏,将整片田地划分成了 (n+1)(m+1) 个区域。

不幸的是,约翰忘记了在围栏上装门,这就导致了奶牛们无法离开自己所在的封闭区域。

约翰想要通过拆掉一些围栏来解决这一问题。

他每次都会选取一对相邻区域,并将分隔这对相邻区域的围栏拆除,从而使得奶牛能在这对相邻区域之间穿行。

约翰的目标是通过拆除掉若干段围栏,使得奶牛可以实现在整片田地中任意行走,到达任意区域。

例如,约翰装好围栏后,田地如下:

+---+--+

|   |  |

+---+--+

|   |  | 

|   |  |

+---+--+

拆除一些围栏段后,变为:

+---+--+

|      | 

+---+  + 

|      | 

|      |

+---+--+

请帮助约翰确定他为达到目标必须拆除的围栏的最小总长度。

输入格式

第一行包含四个整数 A,B,n,m

接下来 n 行包含 a1,…,an

再接下来 m 行包含 b1,…,bm

输出格式

输出约翰必须拆除的围栏的最小总长度。

注意,答案可能超过标准的 32 位整数型能够表示的范围,所以你可能需要使用更大的数据类型,例如C/C++中的“long long”

数据范围

1≤A,B≤109,

0≤n,m≤2000,

0<ai<A,

0<bi<B


Farmer John has realized that many of his cows are strangely agoraphobic (being fearful of large open spaces). To try and make them less afraid of grazing, he partitions his large field into a number of smaller regions by building vertical (north-south) and horizontal (east-west) fences. The large field is a rectangle with corner points at (0,0) and (A,B) . FJ builds n n vertical fences (0≤n≤2000 ) at distinct locations  a1…an ( 0<ai<A ); each fence runs from  (ai,0) to  (ai,B) . He also builds m m horizontal fences ( 0≤m≤2000 ) at locations   b1…bm (0<bi<B ); each such fence runs from(0,bi) to(A,bi) . Each vertical fence crosses through each horizontal fence, subdividing the large field into a total of  (n+1)(m+1) regions. Unfortunately, FJ completely forgot to build gates into his fences, making it impossible for cows to leave their enclosing region and travel around the entire field! He wants to remedy this situation by removing pieces of some of his fences to allow cows to travel between adjacent regions. He wants to select certain pairs of adjacent regions and remove the entire length of fence separating them; afterwards, he wants cows to be able to wander through these openings so they can travel anywhere in his larger field. For example, FJ might take a fence pattern looking like this:

+---+--+
|   |  |
+---+--+
|   |  |  
|   |  |
+---+--+
and open it up like so:
+---+--+
|      |  
+---+  +  
|      |  
|      |
+---+--+
Please help FJ determine the minimum total length of fencing he must remove to accomplish his goal.

INPUT FORMAT (file fencedin.in):

The first line of input contains  A B n , and m ( 1≤A,B≤1,000,000,000 ). The next n n lines contain  a1…an , and the next  m lines after that contain  b1…bm .

OUTPUT FORMAT (file fencedin.out):

Please write the minimum length of fencing FJ must remove. Note that this might be too large to fit into a standard 32-bit integer, so you may need to use 64-bit integer types (e.g., "long long" in C/C++).

SAMPLE INPUT:

15 15 5 2
2
5
10
6
4
11
3

SAMPLE OUTPUT:

44
Problem credits: Brian Dean

输入样例 复制


输出样例 复制