luogu#P4898. [IOI 2018] seats 排座位

[IOI 2018] seats 排座位

Background

This is an interactive problem, but here you should submit a full program.

Problem Description

You are going to hold an international programming contest in a rectangular hall. The hall has HWHW seats (HH rows and WW columns). Rows are numbered from 00 to H1H - 1, and columns are numbered from 00 to W1W - 1. The seat in row rr and column cc is denoted by (r,c)(r, c). There are HWHW contestants, numbered from 00 to HW1HW - 1. You have prepared a seating plan: contestant i(0iHW1)i(0 \leq i \leq HW - 1) is assigned to seat (Ri,Ci)(R_i, C_i). The seating plan is a one-to-one correspondence between contestants and seats.

A set of seats SS in the hall is called rectangular if there exist integers r1,r2,c1r_1, r_2, c_1, and c2c_2 such that:

  • 0r1r2H10 \leq r_1 \leq r_2 \leq H - 1.
  • 0c1c2W10 \leq c_1 \leq c_2 \leq W - 1.
  • SS is exactly the set of all seats (r,c)(r, c) that satisfy r1rr2r_1 \leq r \leq r_2 and c1cc2c_1 \leq c \leq c_2.

If a rectangular set of seats contains k(1kHW)k(1 \leq k \leq HW) seats, and the contestant IDs assigned to these seats are exactly from 00 to k1k - 1, then this set is nice. The niceness of a seating plan is defined as the number of nice rectangular seat sets in the plan.

After you finish the seating plan, you will receive some requests to swap the seats of two contestants. Specifically, there are QQ such requests, numbered from 00 to Q1Q - 1 in time order. Request j(0jQ1)j(0 \leq j \leq Q - 1) wants to swap the seats of contestants AjA_j and BjB_j. You accept each request immediately and update the seating plan. After each update, your task is to compute the niceness of the current seating plan.

Input Format

The first line contains three positive integers H,W,QH, W, Q, as described above.

The next HWHW lines each contain two non-negative integers. In these HWHW lines, line ii contains Ri1,Ci1R_{i - 1}, C_{i - 1}, i.e., the row and column of the initial seat of contestant i1i - 1.

The next QQ lines each contain two non-negative integers. In these QQ lines, line jj contains Aj1,Bj1A_{j - 1}, B_{j - 1}, i.e., the IDs of the two contestants whose seats should be swapped in request j1j - 1.

Output Format

Output QQ lines. Each line contains one integer. The integer on line ii is the niceness of the current seating plan after processing swap request i1i - 1 in time order.

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

3
4

1 5 3
0 0
0 1
0 2
0 3
0 4
0 1
0 3
4 0

5
3
2

Hint

Constraints

  • 1H1 \leq H.
  • 1W1 \leq W.
  • HW1,000,000HW \leq 1, 000, 000.
  • 0RiH1(0iHW1)0 \leq R_i \leq H - 1(0 \leq i \leq HW - 1).
  • 0CiW1(0iHW1)0 \leq C_i \leq W - 1(0 \leq i \leq HW - 1).
  • $(R_i, C_i) \neq (R_j, C_j)(0 \leq i < j \leq HW - 1)$.
  • 1Q50,0001 \leq Q \leq 50, 000.
  • 0AjHW1(0jQ1)0 \leq A_j \leq HW - 1(0 \leq j \leq Q - 1).
  • 0BjHW1(0jQ1)0 \leq B_j \leq HW - 1(0 \leq j \leq Q - 1).
  • AjBj(0jQ1)A_j \neq B_j(0 \leq j \leq Q - 1).

Subtasks

    1. (5 points) HW100HW \leq 100, Q5,000Q \leq 5, 000.
    1. (6 points) HW10,000HW \leq 10, 000, Q5,000Q \leq 5, 000.
    1. (20 points) H1,000H \leq 1, 000, W1,000W \leq 1, 000, Q5,000Q \leq 5, 000.
    1. (6 points) Q5,000Q \leq 5, 000, AjBj10,000(0jQ1)|A_j - B_j| \leq 10, 000(0 \leq j \leq Q - 1).
    1. (33 points) H=1H = 1.
    1. (30 points) No additional constraints.

Translated by ChatGPT 5