#P2688. 大海战

大海战

Description

The game is played on a 1×n1 \times n board. Initially, GD has cc types of ships. Each ship has width 11, length cic_i, and there are tit_i ships of the ii-th type. GD must place all these ships on the board so that no two ships overlap (they may be adjacent).

Next, MW makes qq “attacks,” each time targeting a 1×11 \times 1 cell, and GD will tell him whether that attack “hit” a ship (or any part of it).

Puzzlingly, GD tells MW every time that he did not hit any ship, which is clearly unrealistic. Now MW reveals the entire gameplay to you. He wants to know the earliest time, after which of his attacks, one can conclude that GD must have lied at least once.

Input Format

There are multiple test cases within a single test file.

The first line contains an integer tt, the number of test cases.

For each test case:

  • The first line contains three integers, the board length nn, the number of ship types cc, and the number of attacks qq.
  • The next cc lines (lines 22 to (c+1)(c + 1)) each contain two integers. On line (i+1)(i + 1), the two integers are the length cic_i and the count tit_i of the ii-th type of ship.
  • Line (c+2)(c + 2) contains qq integers. The ii-th integer aia_i is the position targeted by MW’s ii-th attack.

Output Format

For each test case, output a single integer ansans, the earliest index such that after the ansans-th operation you can conclude GD has lied. In particular, if it is impossible from the very beginning to place all ships as required, output 00. If after all qq attacks you still cannot determine whether GD has lied, output 1-1.

3
12 2 2
1 1
2 5
6 8
5 1 2
3 1
1 5
11 3 0
2 2
3 1
5 1
2
-1
0

Hint

  • Sample Input/Output 1 Explanation:

    • For the first sample, there exists a layout {1,22,22,0,22,22,22}\{1,22,22,0,22,22,22\} (00 means empty) such that the first attack misses; no layout can make both attacks miss.
    • For the second sample, there exists a layout {0,333,0}\{0,333,0\} such that both attacks miss.
    • For the third sample, it is impossible to place all ships legally from the start.
  • Constraints:

    • For test point 1, n1000000000n \leq 1000000000, c100000c \leq 100000, q=0q = 0.
    • For test points 2–3, all tit_i are 11.
    • For test points 2–8, n400000n \leq 400000, c100c \leq 100, q=1q = 1.
    • For test point 9, n100n \leq 100, c=1c = 1, q100q \leq 100.
    • For test points 10–14, n200000n \leq 200000, c=1c = 1, q200000q \leq 200000.
    • For test points 15–16, n200n \leq 200, c=2c = 2, q200q \leq 200.
    • For test points 17–20, n4000n \leq 4000, c=2c = 2, q4000q \leq 4000.
    • For 100%100\% of the testdata, $1 \le t \le 5, n \ge 1, c \ge 1, q \ge 0, 1 \le a_i \le n, 0 \le c_i \le 10^5, 0 \le t_i \le 10^5$.
  • Note: Please be mindful of the impact of constant factors on performance.

Translated by ChatGPT 5