Universal Cup Judging System

Universal Cup

時間限制: 3.0 s 記憶體限制: 1024 MB 總分: 100 难度: [顯示] 可 Hack ✓
统计

Minecraft の新しいアップデートで、クモはついに、長らく待ち望んでいた、自分で巣を編む能力を手に入れました。しかし、ゲームの精神を保つため、開発者は糸を厳密に水平または厳密に垂直に張ることだけを許しました。

クモには大きさ $W \times H$ の長方形の領域があります。この領域を、頂点が $(0, 0)$、$(W, 0)$、$(W, H)$、$(0, H)$ である長方形と考えます。この長方形の境界上には、クモが必ず巣を張らなければならない場所として、$n$ 個の点が指定されています。

クモは水平および垂直な糸の線分を任意の本数だけ張ることができます。線分は任意であり、交差したり、長方形の境界に沿ったり、空中に浮いたりしてもかまいません。クモは、指定されたすべての点が同じ連結成分に属するように巣を編まなければなりません。言い換えると、張った線分だけに沿って移動することで、任意の指定点から他の任意の指定点へ到達できなければなりません。

当然、開発者は巣のテクスチャを節約したいと考えています。そのため、張るすべての糸の長さの合計として可能な最小値を求めてください。

入力

最初の行には、テストケース数を表す整数 $t$ ($1 \le t \le 2 \cdot 10^5$) が与えられます。その後にテストケースが続きます。

各テストケースの最初の行には、指定点の個数と長方形の大きさを表す 3 つの整数 $n, W, H$ ($1 \le n \le 10^6$、$1 \le W, H \le 10^9$) が与えられます。

続く $n$ 行には、指定点の座標を表す整数の組 $x_i, y_i$ ($0 \le x_i \le W$、$0 \le y_i \le H$) が与えられます。

すべての点は互いに異なり、長方形の境界上にあることが保証されます。また、点は左下の頂点 $(0, 0)$ を起点として、長方形の境界に沿って反時計回りの順に与えられることが保証されます。すべてのテストケースにわたる $n$ の合計は $10^6$ を超えません。

出力

各テストケースについて、張ることのできる糸の長さの合計として可能な最小値を 1 つ出力してください。

入出力例

入力 1

3
1 2 1
1 0
2 1 2
0 0
1 2
3 2 2
1 0
2 1
0 1

出力 1

0
3
3

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.