luogu#P12073. [OOI 2025] Alice, Bob, and two arrays.

[OOI 2025] Alice, Bob, and two arrays.

题目描述

There is an array aa of length NN and an array bb of length MM. All numbers in the arrays are integers and lie in the range from 1 to kk. There is also an initially empty array cc.

Alice and Bob play the following game on these arrays: the players take turns, and on their turn, a player must append a number to the end of the array cc such that cc remains a subsequence of both aa and bb. The player who cannot make a move loses. Alice goes first.

They will play the game qq times. For the ii-th game, they will choose two numbers xix_i and yiy_i (0xi<N,0yi<M0 \le x_i < N, 0 \le y_i < M), then remove the first xix_i elements from array aa and the first yiy_i elements from array bb, and then play the game on the resulting arrays. After each deletion operation and before the next one, the arrays aa and bb are restored to their initial state, meaning that the numbers deleted from the arrays in one game may not be deleted in subsequent games. Also, the array cc is cleared between games.

The guys have their quirks, so they always choose xix_i and yiy_i in such a way that after the deletions, the remaining parts of arrays aa and bb start with the same value.

Alice really wants to win, so she asks you to determine for each game whether she can win, assuming both players play optimally.

Note that the arrays can be very long, so they are provided in a special format. Each array is given as a sequence of segments of equal numbers. Array aa consists of nn such segments, and array bb consists of mm such segments. Each segment is defined by its length and the number that occupies that segment.

输入格式

The first line contains six integers N,n,M,m,k,qN, n, M, m, k, q ($1 \le N, M \le 10^9, 1 \le n, m, k \le 1600, 1 \le q \le 10^6$) — the length of the first array, the number of segments in the first array, the length of the second array, the number of segments in the second array, the number limit, and the number of games, respectively.

The next nn lines contain two integers lial_i^a and viav_i^a (1liaN,1viak1 \le l_i^a \le N, 1 \le v_i^a \le k) — the length of the segment and the number written in that segment. These numbers define array aa: the first l1al_1^a numbers of array aa are equal to v1av_1^a, the next l2al_2^a numbers are equal to v2av_2^a, ..., the last lnal_n^a numbers are equal to vnav_n^a.

The next mm lines contain two integers libl_i^b and vibv_i^b (1libN,1vibk1 \le l_i^b \le N, 1 \le v_i^b \le k) — the length of the segment and the number written in that segment. These numbers define array bb. The format is similar to array aa. It is guaranteed that $\sum l_i^a = N, \sum l_i^b = M, v_i^a \ne v_{i+1}^a$, and vibvi+1bv_i^b \ne v_{i+1}^b.

The next qq lines contain pairs of integers xix_i and yiy_i (0xi<N,0yi<M0 \le x_i < N, 0 \le y_i < M) — descriptions of the games.

For each game ii, it is guaranteed that if we remove the first xix_i elements from aa and the first yiy_i elements from bb, the remaining parts of the arrays will start with the same value.

输出格式

For each of the qq games, print "Yes" if Alice wins with optimal strategy, and "No" if Bob wins.

5 1 5 1 1 9
5 1
5 1
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
Yes
No
Yes
No
No
Yes
Yes
Yes
Yes
7 3 7 3 2 12
2 1
3 2
2 1
2 2
3 1
2 2
0 2
0 3
0 4
1 2
1 3
1 4
2 5
2 6
3 5
3 6
4 5
4 6
Yes
No
Yes
Yes
No
Yes
No
Yes
No
Yes
Yes
Yes

提示

Note

In the first example, the arrays look like this: a=(1,1,1,1,1)a = (1, 1, 1, 1, 1) and b=(1,1,1,1,1)b = (1, 1, 1, 1, 1).

  • In the first query, x=0,y=0x = 0, y = 0, so the game will be played on the arrays a=(1,1,1,1,1)a = (1, 1, 1, 1, 1) and b=(1,1,1,1,1)b = (1, 1, 1, 1, 1). In this case, players can only append the number 1 to the array cc, so after 5 moves the game will end, and Bob will lose because he won't be able to make a move.
  • In the second query, x=0,y=1x = 0, y = 1, so the game will be played on the arrays a=(1,1,1,1,1)a = (1, 1, 1, 1, 1) and b=(1,1,1,1)b = (1, 1, 1, 1). In this case, the game will end after 4 moves, and Alice will lose.
  • In the last query, x=2,y=2x = 2, y = 2, so the game will be played on the arrays a=(1,1,1)a = (1, 1, 1) and b=(1,1,1)b = (1, 1, 1). In this case, Bob will lose.

In the second example, a=(1,1,2,2,2,1,1)a = (1, 1, 2, 2, 2, 1, 1), b=(2,2,1,1,1,2,2)b = (2, 2, 1, 1, 1, 2, 2).

  • In the first query, x=0x = 0 and y=2y = 2, so the game will be played on the arrays a=(1,1,2,2,2,1,1)a = (1, 1, 2, 2, 2, 1, 1) and b=(1,1,1,2,2)b = (1, 1, 1, 2, 2). If Alice appends the number 2 to the array cc, Bob will also append the number 2, and then no moves will be left, so Alice will lose. Therefore, Alice must first append the number 1 to cc. After this, for similar reasons, if Bob appends the number 2 to the array, he will lose. So, he is forced to append the number 1, and the array cc becomes (1,1)(1, 1). Then Alice again appends the number 1 to the array cc, and Bob has no more moves left, so Alice wins.
  • In the second query, x=0x = 0 and y=3y = 3, so the game will be played on the arrays a=(1,1,2,2,2,1,1)a = (1, 1, 2, 2, 2, 1, 1) and b=(1,1,1,2,2)b = (1, 1, 1, 2, 2). Following the reasoning in the previous example, Alice cannot append the number 2 to the array cc, because then she will lose. But if Alice appends the number 1, then Bob will also append the number 1, and after that, Alice will lose for similar reasons. Therefore, Bob wins in this case.