IOI '18 P2 - Seats

View as PDF

Submit solution


Points: 40 (partial)
Time limit: 3.0s
Memory limit: 256M

Problem type
Allowed languages
C++

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 H1 and the columns are numbered from 0 through W1. The seat in row r and column c is denoted by (r,c). You invited HW contestants, numbered from 0 through HW1. You also made a seating chart, which assigns the contestant i (0iHW1) 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:

  • 0r1r2H1
  • 0c1c2W1
  • S is exactly the set of all seats (r,c) such that r1rr2 and c1cc2.

A rectangular set consisting of k seats (1kHW), is beautiful if the contestants whose assigned seats are in the set have numbers from 0 through k1. 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 Q1 in chronological order. The request j (0jQ1) 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.

Implementation Details

You should implement the following procedure and function:

Copy
void give_initial_chart(int H, int W, std::vector<int> R, std::vector<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.
Copy
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.

Example

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.

034125

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

534120

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.

The files sample-01-in and sample-01-out in the zipped attachment package correspond to this example. Other sample inputs/outputs are also available in the package.

Constraints

  • 1H
  • 1W
  • HW1000000
  • 0RiH1 (0iHW1)
  • 0CiW1 (0iHW1)
  • (Ri,Ci)(Rj,Cj) (0i<jHW1)
  • 1Q50000
  • 0aHW1 for any call to swap_seats
  • 0bHW1 for any call to swap_seats
  • ab for any call to swap_seats

Subtasks

  1. (5 points) HW100, Q5000
  2. (6 points) HW10000, Q5000
  3. (20 points) H1000, W1000, Q5000
  4. (6 points) Q5000, |ab|10000 for any call to swap_seats
  5. (33 points) H=1
  6. (30 points) No additional constraints

Sample Grader

The sample grader reads the input in the following format:

  • line 1: H W Q
  • line 2+i (0iHW1): Ri Ci
  • line 2+HW+j (0jQ1): 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 (0jQ1): the return value of swap_seats for the request j.

Attachment Package

The sample grader along with sample test cases are available here.


Comments

There are no comments at the moment.