#P7745. Robot Manhattan Path Sum

    ID: 20932 Type: Default 1000ms 256MiB

Robot Manhattan Path Sum

Robot Manhattan Path Sum

A robot is placed on a 2D coordinate system at the origin (0,0)(0,0). It receives a sequence of commands represented by the characters S, J, I, and Z. These instructions indicate movements in the following way:

  • S : move from (x,y)(x,y) to (x,y+1)(x, y+1),
  • J : move from (x,y)(x,y) to (x,y1)(x, y-1),
  • I : move from (x,y)(x,y) to (x+1,y)(x+1, y), and
  • Z : move from (x,y)(x,y) to (x1,y)(x-1, y).

There are nn fixed control points on the track. Each control point measures the Manhattan distance to the robot. The Manhattan distance between two points (x1,y1)(x_1,y_1) and (x2,y2)(x_2,y_2) is given by:

d((x1,y1),(x2,y2))=x1x2+y1y2.d((x_1,y_1),(x_2,y_2)) = |x_1-x_2| + |y_1-y_2|.

After executing each command, compute and output the sum of Manhattan distances from the robot's current position to all control points.

Note: The input starts with two integers nn and mm, where nn is the number of control points and mm is the number of commands. Then follow nn lines each containing two integers representing the coordinates of a control point. The final line contains a string of mm characters (each being one of S, J, I, or Z) representing the sequence of commands.

inputFormat

The first line contains two integers nn and mm.

The next nn lines each contain two integers xx and yy, representing the coordinates of a control point.

The last line contains a string of length mm consisting only of the characters S, J, I, and Z indicating the robot's commands.

outputFormat

Output mm lines. The ii-th line should contain the sum of Manhattan distances from the robot’s current position (after executing the first ii commands) to all control points.

sample

2 3
1 0
0 1
SIJ
2

2 2

</p>