University of Toronto ACM-ICPC Tryouts 2012

F: Light Cyclin’

Having been sucked into your father's secret computer through a projector in the back of his arcade (or something), you find yourself in the wonderful world of Tron! Here, you play games all day, and if you ever lose, you die.

One such game involves you and an opponent driving around a flat grid on light cycles, which leave behind a permanent trail of...light...wherever they go. This grid can be modeled with the Cartesian plane, and is enclosed by a rectangle of impenetrable walls which ensure that the x-coordinate of each light cycle is always between 1 and 1012, while its y-coordinate is between 1 and 106 (inclusive). Light cycles always stay on the grid lines, and move at a speed of 1 square per second.

A match lasts S (1 ≤ S ≤ 10100) seconds. You start at coordinates (XA, YA) and follow a set of NA (1 ≤ NA ≤ 105) instructions, with your i-th instruction consisting of moving LAi squares in the direction given by the character DAi (with "U", "D", "L", and "R" representing up, down, left, and right, respectively). Similarly, your opponent starts at coordinates (XB, YB) and follows a set of NB (1 ≤ NB ≤ 105) instructions, with their ith instruction described by LBi and DBi. Of course, neither player's instructions will ever take them beyond the boundaries of the walls, and it will take each player exactly S seconds to execute their instructions. Additionally, for each player, no instruction will have an equal or opposite direction to that of their previous instruction. Finally, if a grid point is ever visited more than once throughout the course of the match, it is guaranteed that one of the path segments intersecting there is passing directly through vertically, while the other is passing directly through horizontally (as such, this cannot happen at either player's starting or ending points).

Whenever both light cycles reach the same grid point at the same time, or a light cycle hits an existing trail of light (in other words, a grid point which either light cycle had previously passed through), a collision occurs. Because you're just playing a practice match for now, neither player dies when this occurs, and, in fact, the collision is not counted in favour of either you or your opponent. Instead, for T (1 ≤ T ≤ 20) scenarios as described above, you're simply interested in the number of collisions that will occur throughout each match.


Line 1: 1 integer, T
For each scenario:
Line 1: 1 integer, S
Next line: 3 integers, XA, YA, and NA
Next NA lines: 1 character, DAi, and 1 integer, LAi, for i = 1..NA
Next line: 3 integers, XB, YB, and NB
Next NB lines: 1 character, DBi, and 1 integer, LBi, for i = 1..NB


For each scenario:
1 integer: The total number of collisions that will occur.

Sample Input

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

Sample Output


Explanation of Sample

The following diagram illustrates the paths of the light cycles (yours drawn in solid lines, and your opponent's drawn in dotted ones), as well as all of the collision points (indicated with large dots):

All Submissions
Best Solutions

Point Value: 20
Time Limit: 10.00s
Memory Limit: 64M
Added: Oct 02, 2012
Author: SourSpinach

Languages Allowed:

Comments (Search)

It's quiet in here...