luogu#P5222. Game

Game

Background

Justin was playing with his board and pieces... Suddenly, he came up with a very interesting game.

Problem Description

The board can be seen as an N×MN \times M grid. Because Justin is too strong, he broke TT cells, meaning no piece can be placed on those cells.

The game Justin came up with works like this: At the beginning, you may choose some unbroken cells in the first column and place one piece on each of them. Then, you may perform the following operations in order any number of times:

  1. Choose one piece and move it up or down to an adjacent cell that is unbroken and does not contain any other piece.

  2. Move all pieces to the next column. If, after moving to the next column, any piece lands on a broken cell, then this operation cannot be performed.

Justin now gives you QQ queries. Each time you are given a kik_i, asking: what is the maximum number of pieces you can place in the first column such that, after some operations, you can move all pieces to the last column, and the total number of operation 1 performed by all pieces combined is at most kik_i.

(Thanks to ywwywwyww for finding an issue in the statement. It has now been fixed.)

Input Format

The first line contains four positive integers: NN, MM, TT, QQ.

The next TT lines each contain two positive integers xix_i, yiy_i, indicating a broken cell.

The next QQ lines each contain one positive integer kik_i, representing Justin's ii-th query.

Output Format

Output QQ lines, each containing a non-negative integer indicating the answer to each query.

3 5 2 3
1 2
2 4
0
1
2

1
2
2

5 1000 5 10
1 2
2 3
3 4
4 5
5 6
0
1
2
3
4
5
6
7
8
9

0
0
0
0
0
0
0
0
0
0

Hint

In the first sample:

When the limit is 00, you can place one piece at (3,1)(3,1), then keep performing operation 2.

When the limit is 11, you can place one piece at (3,1)(3,1) and another at (2,1)(2,1). After performing operation 2 twice, perform operation 1 once on the piece at (2,3)(2,3) to move it to (1,3)(1,3), then keep performing operation 2.

When the limit is 22, it is the same as above. Because if you place three pieces, you cannot perform operation 2.

In the second sample:

You can see it is completely blocked, so it is impossible to move to the last column.

Constraints:

Test Point ID NN\le MM\le TT\le QQ\le
11 10610^6 11 10510^5
262-6 1010 100100 1010 100100
7107-10 2020 5050 10310^3
111411-14 3030 10410^4 100100 10410^4
152015-20 5050 10610^6 10310^3 10510^5
$$1 \le x_i \le N \qquad 1\le y_i \le M \qquad 0 \le k_i \le 2^{31}-1$$

The testdata for test points 1111~2020 is randomly generated.

Translated by ChatGPT 5