Under construction

Still being built. Things may change

Space-A

Interview

Time limit1sMemory limit1024 MB

Summary
Given a fixed string of R, U, X moves and a list of target points, count how many points the robot can reach by picking a subsequence of moves.
Level

Easy3 of 10

Topics
Math, String
Solved
No attempts yet

Problem

Seonu is in charge of Space-A, the core space exploration project of Ajuguk. Seonu uses an exploration robot to explore a newly discovered unknown planet.

The area to explore on the planet can be represented as a two-dimensional plane. The robot starts at (1,1)(1, 1). The robot can make three kinds of moves.

  • R: moves one cell in the direction in which the xx coordinate increases.
  • U: moves one cell in the direction in which the yy coordinate increases.
  • X: moves one cell diagonally, increasing both the xx and yy coordinates.

The order of the robot's moves is fixed in advance. Seonu picks some of the fixed moves at will and moves the robot with them. For example, suppose the fixed order is URURR. If Seonu picks the first, third, and fourth moves, the robot moves in the order UUR.

Given the points Seonu must explore on the planet, find how many of them can be explored by choosing the robot's moves appropriately.

Note that the task is to count the points reachable from the starting point, not the points the robot passes through on the way. The robot's starting position can always be explored.

Input

The first line contains the number of robot moves NN. 1≤N≤100 0001\leq N \leq 100\,000

The second line contains the fixed order of the robot's moves as a string of length NN.

The third line contains the number of points KK Seonu wants to explore with the robot. 1≤K≤100 0001\leq K \leq 100\,000

The next KK lines each contain the xx and yy coordinates of a point to explore, separated by a space. 1≤x,y≤500 0001\leq x, y \leq 500\,000

No coordinate pair is given more than once.

Output

Print the number of points to explore, among the given points, that the robot can reach by choosing its moves appropriately.

Examples2

  1. Example 1

    Input
    5
    UUXRX
    5
    1 3
    2 4
    3 2
    4 3
    4 5
    
    Expected output
    5
    
  2. Example 2

    Input
    5
    UUXRX
    5
    1 4
    2 5
    3 1
    4 2
    5 3
    
    Expected output
    0