#P4436. Treasure Hunt Game

    ID: 17682 Type: Default 1000ms 256MiB

Treasure Hunt Game

Treasure Hunt Game

In this problem, there are \(n\) rooms arranged in a linear fashion and numbered \(1,2,\ldots,n\). Adjacent rooms are separated by doors. Some doors are locked and can only be opened if you have the corresponding key. For each locked door, there is exactly one key and its location is given. Specifically, if the door between room \(d\) and room \(d+1\) is locked, then its unique key can be found in a certain room \(r\) (\(1 \le r \le n\)).

After providing the key locations, \(p\) instructions are given. The \(i\)th instruction tells you to start at room \(S_i\) and try to reach room \(T_i\). However, sometimes the instructions may lead to a dead end if you cannot pick up a required key before reaching a locked door.

Your task is to determine for each instruction whether there exists a valid path from \(S_i\) to \(T_i\) such that when encountering a locked door you have already visited the room that contains its key. Note that the path is unique because the rooms are arranged in a line. When traveling from room \(S\) to room \(T\), you must pass through every door between them.

If \(S \le T\) (i.e. moving rightwards), then for every locked door with index \(d\) where \(S \le d \le T-1\) (door between room \(d\) and \(d+1\)), you can unlock it only if its key is located in one of the rooms visited before reaching the door, i.e. the key must be in a room \(r\) satisfying \(S \le r \le d\). Similarly, if \(S > T\) (moving leftwards), then for every locked door with index \(d\) where \(T \le d \le S-1\) (door between room \(d\) and room \(d+1\)), when traveling left the door is encountered in reverse order. In this case, you can unlock the door only if its key is located in a room \(r\) satisfying \(d+1 \le r \le S\).

Output Yes if a valid path exists for the given instruction, otherwise output No.

inputFormat

The first line contains three integers \(n\), \(m\), and \(p\) where \(n\) is the number of rooms, \(m\) is the number of locked doors, and \(p\) is the number of instructions.

The next \(m\) lines each contain two integers \(d\) and \(r\). Here, \(d\) (with \(1 \le d < n\)) indicates that the door between room \(d\) and room \(d+1\) is locked, and \(r\) (\(1 \le r \le n\)) is the room containing its unique key.

The following \(p\) lines each contain two integers \(S\) and \(T\) (\(1 \le S, T \le n\)), representing an instruction to move from room \(S\) to room \(T\).

outputFormat

For each instruction, output a single line with Yes if there exists a valid path from \(S\) to \(T\); otherwise, output No.

sample

10 2 4
3 2
7 8
1 5
1 10
10 5
10 1
Yes

No Yes No

</p>