#P14445. [ICPC 2025 Xi'an Practice] Follow the Sequence

[ICPC 2025 Xi'an Practice] Follow the Sequence

题目描述

In a two-dimensional Cartesian coordinate system, you begin at the origin (0,0)(0,0).

You are given a string ss of length nn, representing a walking sequence. The ii-th character sis_i belongs to the set {U,D,L,R}\{\texttt{U},\texttt{D},\texttt{L},\texttt{R}\}.

An infinitely long string ss' is constructed by repeating ss indefinitely. Formally, si=s((i1)modn)+1s'_i = s_{((i - 1) \bmod n) + 1}.

Now, you process each character of ss' in order, moving according to the following rules:

  • If si=Us'_i = \texttt{U}, move to (x,y+1)(x, y+1).
  • If si=Ds'_i = \texttt{D}, move to (x,y1)(x, y-1).
  • If si=Ls'_i = \texttt{L}, move to (x1,y)(x-1, y).
  • If si=Rs'_i = \texttt{R}, move to (x+1,y)(x+1, y).

This process continues indefinitely. You are also given mm key points, where the ii-th key point has coordinates (pi,qi)(p_i, q_i). Your task is to determine the number of distinct\textbf{distinct} key points that are visited during the infinite walking process.

输入格式

The input consists of multiple test cases. The first line contains an integer tt (1t51051 \leq t \leq 5 \cdot 10^5), the number of test cases. For each test case:

  • The first line contains two integers nn and mm (1n,m51051 \le n, m \le 5 \cdot 10^5), where nn is the length of the string ss and mm is the number of key points.
  • The second line contains the string ss of length nn.
  • The next mm lines each contain two integers pip_i and qiq_i (109pi,qi109-10^9 \le p_i, q_i \le 10^9), representing the coordinates of a key point.

It is guaranteed that the sum of nn and the sum of mm over all test cases do not exceed 51055 \cdot 10^5.

输出格式

For each test case, output a single line containing a single integer, representing the number of distinct key points visited.

3
6 3
DUDUDU
0 0
1 0
0 -1
6 5
DUDULU
0 0
-1 0
0 -1
-1 -1
-1 1
5 5
ULUUL
-624531741 651883826
-1 2
-312566309 468849463
-212530129 633866239
672824982 -674189680
2
4
2

提示

In the first test case, only two key points (0,0)(0,0) and (0,1)(0,-1) are visited.

In the second test case, four key points (0,0),(0,1),(1,0)(0,0),(0,-1),(-1,0) and (1,1)(-1,1) are visited.