Universal Cup Judging System

Universal Cup

Time Limit: 2.0 s Memory Limit: 1024 MB Total points: 100 Hackable ✓
Statistics

$xy$ 平面上に、すべての辺が $x$ 軸または $y$ 軸に平行な単純 $N$ 角形(自己交差や穴のない多角形)が与えられます。 この多角形の境界上には $M$ 個の演説場所があります。

多角形の境界上のみを移動できるとき、すべての演説場所をちょうど一度ずつ訪れるために必要な最小の総移動距離を求めてください。 移動の開始点と終了点は、多角形の境界上であれば任意に選ぶことができます。

入力

1行目には、多角形の頂点数 $N$ と演説場所の数 $M$ が含まれます。 $(4 \le N \le 10^5, 1 \le M \le 10^5)$

$(i + 1)$ 行目には、単純 $N$ 角形の $i$ 番目の頂点の座標 $(x_i, y_i)$ が含まれます。 $(1 \le i \le N, |x_i|, |y_i| \le 10^5)$ 多角形の辺は、$i$ 番目の頂点と $(i + 1)$ 番目の頂点を結びます。言い換えると、$x_i = x_{i+1}$ または $y_i = y_{i+1}$ のいずれか一方が成り立ちます。($(N + 1)$ 番目の頂点は1番目の頂点を指します。) どの頂点も辺上にはありません。(つまり、180度の角を持つ頂点はありません。)

$(j + 1 + N)$ 行目には、$j$ 番目の演説場所の座標 $(p_j, q_j)$ が含まれます。 $(1 \le j \le M, |p_j|, |q_j| \le 10^5)$ これらの点はすべて異なり、与えられた多角形の境界上にある。(頂点を含みます。)

すべての入力値は整数です。

出力

答えを出力してください。

入出力例

入力 1

4 3
0 0
3 0
3 2
0 2
0 0
3 0
3 2

出力 1

5

注記

最初の例では、長方形の3つの頂点上に点があり、$(0, 0) \to (3, 0) \to (3, 2)$ の順で訪れると、最短の総移動距離は $3 + 2 = 5$ となります。

入出力例 1 の図

入力 2

6 4
0 0
3 0
3 1
2 1
2 2
0 2
3 0
0 2
2 1
1 2

出力 2

5

入出力例 2 の図

入力 3

10 10
-12 -11
-12 9
3 9
3 1
-3 1
-3 -1
8 -1
8 -10
-5 -10
-5 -11
3 1
3 7
7 -1
-3 0
-4 -10
-12 8
-10 -11
-3 9
-12 -10
8 -7

出力 3

74

入出力例 3 の図

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#1525EditorialOpen题解jiangly2026-04-15 16:03:41View

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.