#P6350. [PA 2011] Laser Pool

    ID: 7111 远端评测题 2000ms 125MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2011枚举快速傅里叶变换 FFTPA(波兰)

[PA 2011] Laser Pool

Problem Description

In an n×mn \times m grid (with the bottom-left corner at (1,1)(1,1) and the top-right corner at (m,n)(m,n)), there is a small ball (which can be treated as a point). Some rows and some columns contain laser beams. There are qq queries. Each query gives the ball’s initial position, direction, speed, and travel time (see the input format for details). You need to find how many times the ball touches a laser beam. If it touches an intersection point of a row-beam and a column-beam, it is counted only once.

When the ball touches the boundary, it reflects. That is, suppose the current change per second is that the xx coordinate increases by vxvx and the yy coordinate increases by vyvy. If it hits the top or bottom boundary, then vyvy becomes vy-vy. If it hits the left or right boundary, then vxvx becomes vx-vx. If this is still unclear, refer to the picture in the sample explanation.

Input Format

The first line contains two integers n,mn,m, representing the number of rows and columns of the grid.

The next line is a binary string of length nn. If the ii-th character is 11, then row ii has a laser beam; otherwise, it does not.

The next line is a binary string of length mm. If the ii-th character is 11, then column ii has a laser beam; otherwise, it does not.

The next line contains an integer qq, representing the number of queries.

The following qq lines each contain five integers x,y,vx,vy,tx,y,vx,vy,t, meaning that initially the ball is at (x,y)(x,y). Each second, xx increases by vxvx and yy increases by vyvy, and it moves for a total of tt seconds. It is guaranteed that vx,vyvx,vy are both 11 or 1-1.

Output Format

For each query, output one number per line, representing the number of times the ball touches a laser beam.

4 6
1010
010110
1
5 2 1 1 8
6

Hint

Constraints: 1n,m1051 \leq n,m \leq 10^5, 1q1041 \leq q \leq 10^4, 1t1091 \leq t \leq 10^9. It is guaranteed that the ball’s initial position is inside the grid and not on the boundary, and vx,vyvx,vy are both 11 or 1-1.

Translated by ChatGPT 5