시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
3 초 512 MB 13 9 9 69.231%

문제

You are going to hold an international programming contest in a rectangular hall, which has HW seats arranged in H rows and W columns. The rows are numbered from 0 through H - 1 and the columns are numbered from 0 through W - 1. The seat in row r and column c is denoted by (r, c). You invited HW contestants, numbered from 0 through HW - 1. You also made a seating chart, which assigns the contestant i (0 ≤ iHW - 1) to the seat (Ri, Ci). The chart assigns exactly one contestant to each seat.

A set of seats in the hall S is said to be rectangular if there are integers r1, r2, c1, and c2 satisfying the following conditions:

  • 0 ≤ r1r2H - 1.
  • 0 ≤ c1c2W - 1.
  • S is exactly the set of all seats (r, c) such that r1rr2 and c1cc2.

A rectangular set consisting of k seats (1 ≤ kHW) is beautiful if the contestants whose assigned seats are in the set have numbers from 0 through k - 1. The beauty of a seating chart is the number of beautiful rectangular sets of seats in the chart.

After preparing your seating chart, you receive several requests to swap two seats assigned to two contestants. More precisely, there are Q such requests numbered from 0 through Q - 1 in chronological order. The request j (0 ≤ jQ - 1) is to swap the seats assigned to contestants Aj and Bj. You accept each request immediately and update the chart. After each update, your goal is to compute the beauty of the current seating chart.

구현

You should implement the following procedure and function:

give_initial_chart(int H, int W, int[] R, int[] C)
  • H, W: the number of rows and the number of columns.
  • R, C: arrays of length HW representing the initial seating chart.
  • This procedure is called exactly once, and before any call to swap_seats.
int swap_seats(int a, int b)
  • This function describes a request to swap two seats.
  • a, b: contestants whose seats are to be swapped.
  • This function is called Q times.
  • This function should return the beauty of the seating chart after the swap.

예시

Let H = 2, W = 3, R = [0, 1, 1, 0, 0, 1], C = [0, 0, 1, 1, 2, 2], and Q = 2.

The grader first calls give_initial_chart(2, 3, [0, 1, 1, 0, 0, 1], [0, 0, 1, 1, 2, 2]).

At first, the seating chart is as follows.

0 3 4
1 2 5

Let's say the grader calls swap_seats(0, 5). After the request 0, the seating chart is as follows.

5 3 4
1 2 0

The sets of seats corresponding to the contestants {0}, {0, 1, 2}, and {0, 1, 2, 3, 4, 5} are rectangular and beautiful. Thus, the beauty of this seating chart is 3, and swap_seats should return 3.

Let's say the grader calls swap_seats(0, 5) again. After the request 1, the seating chart goes back to the initial state. The sets of seats corresponding to the contestants {0}. {0, 1}, {0, 1, 2, 3}, and {0, 1, 2, 3, 4, 5} are rectangular and beautiful. Hence, the beauty of this seating chart is 4, and swap_seats should return 4.

제한

  • 1 ≤ H
  • 1 ≤ W
  • HW ≤ 1,000,000
  • 0 ≤ RiH - 1 (0 ≤ iHW - 1)
  • 0 ≤ CiW - 1 (0 ≤ iHW - 1)
  • (Ri, Ci) ≠ (Rj, Cj) (0 ≤ i < jHW - 1)
  • 1 ≤ Q ≤ 50,000
  • 0 ≤ aHW - 1 for any call to swap_seats
  • 0 ≤ bHW - 1 for any call to swap_seats
  • ab for any call to swap_seats

서브태스크

번호 배점 조건
1 5

HW ≤ 100, Q ≤ 500

2 6

HW ≤ 10,000, Q ≤ 5,000

3 20

H ≤ 1,000, W ≤ 1,000, Q ≤ 5,000

4 6

Q ≤ 5,000, |a - b| ≤ 10,000 for any call to swap_seats

5 33

H = 1

6 30

No additional constraints

샘플 그레이더

The sample grader reads the input in the following format:

  • line 1: H W Q
  • line 2 + i (0 ≤ iHW - 1): Ri Ci
  • line 2 + HW + j (0 ≤ jQ - 1): Aj Bj

Here, Aj and Bj are parameters for the call to swap_seats for the request j.

The sample grader prints your answers in the following format:

  • line 1 + j (0 ≤ jQ - 1) : the return value of swap_seats for the request j

제출할 수 있는 언어

C++17, C++14, C++20, C++14 (Clang), C++17 (Clang), C++20 (Clang)

채점 및 기타 정보

  • 예제는 채점하지 않는다.