This page is still under construction.

Parts of this page are still being built. What you see may change.

Laser Pool

Time limit5sMemory limit256 MB

Summary
A ball bounces elastically around a grid of lit horizontal and vertical laser beams; count how many distinct lit beams it touches during t time units, including the start.
Level

Hard8 of 10

Topics
Math, Simulation, Number theory, Implementation
Solved
No attempts yet

Problem

The people of Byteotia love a game called laser pool. The table is an n×mn \times m rectangle. Its rails are 14\tfrac{1}{4} wide and hold n+mn + m pairs of laser transmitters. When every transmitter is switched on, the table is covered by nn horizontal and mm vertical laser beams, arranged so that the ii-th horizontal beam (for 1≤i≤n1 \le i \le n) meets the jj-th vertical beam (for 1≤j≤m1 \le j \le m) at the point (j−12, i−12)\left(j - \tfrac{1}{2},\ i - \tfrac{1}{2}\right). Each transmitter can be turned on or off independently.

The game uses a ball of diameter 12\tfrac{1}{2}. Whenever the ball touches at least one lit laser beam, a single hit signal is shown; touching several beams at the very same instant still counts as one signal.

At the start the ball is centered at (x−12, y−12)\left(x - \tfrac{1}{2},\ y - \tfrac{1}{2}\right). It is struck so that its initial velocity vector is (xv,yv)(x_v, y_v), and it then rolls without friction for tt units of time. Every collision with a rail is perfectly elastic. How many times is the hit signal shown, counting the initial moment of the ball's motion as well?

Input

The first line contains two integers nn and mm (3≤n,m≤100 0003 \le n, m \le 100\,000), the dimensions of the table.

The second line is a string of nn characters, each either 00 or 11. Its ii-th character is the state of the ii-th horizontal transmitter, where 00 means off and 11 means on.

The third line is a string of mm characters describing the vertical transmitters in the same way.

The fourth line contains an integer kk (1≤k≤10 0001 \le k \le 10\,000), the number of queries. Each of the next kk lines contains five integers xx, yy, xvx_v, yvy_v, tt (1<x<m1 < x < m, 1<y<n1 < y < n, xv,yv∈{−1,1}x_v, y_v \in \{-1, 1\}, 1≤t≤1091 \le t \le 10^9): the starting position of the ball, its velocity, and how long it rolls.

Output

Print exactly kk lines. The ii-th line must contain one integer: the number of times the hit signal is shown for the ii-th query.

Hint

Examples1

  1. Example 1

    Input
    4 6
    1010
    010110
    1
    5 2 1 1 8
    
    Expected output
    6