#abc273d. D - LRUD Instructions

D - LRUD Instructions

Score : 400400 points

问题描述

存在一个具有 HH 行(水平方向)和 WW 列(垂直方向)的网格。(i,j)(i, j) 表示从上数第 ii 行、从左数第 jj 列的方格。

NN 个方格,(r1,c1),(r2,c2),,(rN,cN)(r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N),设置了墙壁。

高桥初始位于方格 (rs,cs)(r_\mathrm{s}, c_\mathrm{s}) 处。

给高桥提供了 QQ 条指令。对于 i=1,2,,Qi = 1, 2, \ldots, Q,第 ii 条指令由一对字符 did_i 和正整数 lil_i 表示。其中,did_i 可以为 LRUD,分别代表左、右、上、下四个方向。

在给定第 ii 个方向的情况下,高桥将按照以下动作重复执行 lil_i 次:

若当前所在方格在 did_i 所代表的方向上有一个没有墙壁的相邻方格,则移动到该方格;否则不做任何操作。

对于 i=1,2,,Qi = 1, 2, \ldots, Q,请输出在高桥按照前 ii 条指令行动后所处的方格坐标。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There is a grid with HH horizontal rows and WW vertical columns. (i,j)(i, j) denotes the square at the ii-th row from the top and jj-th column from the left.
NN squares, (r1,c1),(r2,c2),,(rN,cN)(r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N), have walls.

Takahashi is initially at square (rs,cs)(r_\mathrm{s}, c_\mathrm{s}).

QQ instructions are given to Takahashi. For i=1,2,,Qi = 1, 2, \ldots, Q, the ii-th instruction is represented by a pair of a character did_i and a positive integer lil_i. did_i is one of L, R, U, and D, representing the directions of left, right, up, and down, respectively.

Given the ii-th direction, Takahashi repeats the following action lil_i times:

If a square without a wall is adjacent to the current square in the direction represented by did_i, move to that square; otherwise, do nothing.

For i=1,2,,Qi = 1, 2, \ldots, Q, print the square where Takahashi will be after he follows the first ii instructions.

Constraints

  • 2H,W1092 \leq H, W \leq 10^9
  • 1rsH1 \leq r_\mathrm{s} \leq H
  • 1csW1 \leq c_\mathrm{s} \leq W
  • 0N2×1050 \leq N \leq 2 \times 10^5
  • 1riH1 \leq r_i \leq H
  • 1ciW1 \leq c_i \leq W
  • ij(ri,ci)(rj,cj)i \neq j \Rightarrow (r_i, c_i) \neq (r_j, c_j)
  • (rs,cs)(ri,ci)(r_\mathrm{s}, c_\mathrm{s}) \neq (r_i, c_i) for all i=1,2,,Ni = 1, 2, \ldots, N.
  • 1Q2×1051 \leq Q \leq 2 \times 10^5
  • did_i is one of the characters L, R, U, and D.
  • 1li1091 \leq l_i \leq 10^9
  • All values in the input other than did_i are integers.

Input

The input is given from Standard Input in the following format:

HH WW rsr_\mathrm{s} csc_\mathrm{s}

NN

r1r_1 c1c_1

r2r_2 c2c_2

\vdots

rNr_N cNc_N

QQ

d1d_1 l1l_1

d2d_2 l2l_2

\vdots

dQd_Q lQl_Q

Output

Print QQ lines. For i=1,2,,Qi = 1, 2, \ldots, Q, the ii-th line should contain the square (Ri,Ci)(R_i, C_i) where Takahashi will be after he follows the first ii instructions, in the following format:

R1R_1 C1C_1

R2R_2 C2C_2

\vdots

RQR_Q CQC_Q

Sample Input 1

5 5 4 4
3
5 3
2 2
1 4
4
L 2
U 3
L 2
R 4

Sample Output 1

4 2
3 2
3 1
3 5

The given grid and the initial position of Takahashi are as follows, where # denotes a square with a wall, T a square where Takahashi is, and . the other squares:

...#.
.#...
.....
...T.
..#..

Given the 11-st instruction, Takahashi moves 22 squares to the left, ending up in square (4,2)(4, 2) as follows:

...#.
.#...
.....
.T...
..#..

Given the 22-nd instruction, Takahashi first moves 11 square upward, then he "does nothing" twice because the adjacent square in his direction has a wall. As a result, he ends up in square (3,2)(3, 2) as follows:

...#.
.#...
.T...
.....
..#..

Given the 33-rd instruction, Takahashi first moves 11 square to the left, then he "does nothing" once because there is no square in his direction. As a result, he ends up in square (3,1)(3, 1) as follows:

...#.
.#...
T....
.....
..#..

Given the 44-th instruction, Takahashi moves 44 squares to the right, ending up in square (3,5)(3, 5) as follows:

...#.
.#...
....T
.....
..#..

Sample Input 2

6 6 6 3
7
3 1
4 3
2 6
3 4
5 5
1 1
3 2
10
D 3
U 3
L 2
D 2
U 3
D 3
U 3
R 3
L 3
D 1

Sample Output 2

6 3
5 3
5 1
6 1
4 1
6 1
4 1
4 2
4 1
5 1

update @ 2024/3/10 11:30:22