#P12704. Retribution

Retribution

Background

Problem Description

You are given an n×mn \times m chessboard. Each grid point is labeled with a character $c_{i,j}\in\{\texttt{U},\texttt{D},\texttt{L},\texttt{R}\}$, representing up, down, left, and right. From a grid point, you may move to an adjacent grid point, but the moving direction must not be the same as the direction indicated by the character on the current grid point, and you must not move outside the board.

There are qq queries. Each query gives two grid points s,ts,t and asks whether it is possible to reach tt from ss by making several moves.

Input Format

To avoid oversized input, this problem uses a special input method. Please write and submit using C++11 or above.

The first line contains four positive integers n,m,q,seedn,m,q,seed, where seedseed is an input parameter.

The next nn lines each contain mm characters describing the board.

mt19937_64 R;
inline void init(int seed){
    R=mt19937_64(seed);
}
inline int get(int l,int r){
    uniform_int_distribution<int> distribution(l,r);
    return distribution(R);
}

You need to include this code in your program, and call init(seed); during initialization.

Then, for qq times, call int a=get(1,n),b=get(1,m),x=get(1,n),y=get(1,m);, where a,b,x,ya,b,x,y describe the positions of s,ts,t as (a,b),(x,y)(a,b),(x,y), representing one query.

Output Format

Output one line containing qq characters. The ii-th character is 00 if in the ii-th query ss cannot reach tt, and 11 otherwise.

2 3 6 0
RLD
RLU
2 1 1 3
2 2 1 3
2 3 1 1
2 3 1 2
1 2 2 3
1 3 2 3
010111
3 3 5 0
DRU
ULU
DRD
1 1 1 3
1 3 1 1
3 1 2 1
2 3 2 1
1 1 1 1
01111
2 3 6 10000001
RLD
RLU
111001

Hint

Retribution - Kry.exe & nm-y (Insane 16.2)

Sample Explanation

For readability, in Sample 1 and Sample 2, the queries are given directly instead of using the special input method.

For the first query of Sample 1, the grid point (2,1)(2,1) cannot move out of column 11.

For the fourth query of Sample 1, the grid point (2,3)(2,3) can move to (2,2)(2,2) and then to (1,2)(1,2).

For the sixth query of Sample 1, the grid point (1,3)(1,3) can move to (1,2),(2,2),(2,3)(1,2),(2,2),(2,3) in order.

Constraints

This problem uses bundled testdata.

Subtask\text{Subtask} nn\le mm\le qq\le Score
11 100100 10310^3 1010
22 33 500500 2×1052\times10^5 2020
33 300300
44 15001500 10610^6 5050

For all testdata, it is guaranteed that 1n,m1.5×1031\le n,m\le 1.5\times10^3, 1q1061\le q\le 10^6, $c_{i,j}\in\{\texttt{U},\texttt{D},\texttt{L},\texttt{R}\}$, 1a,xn1\le a,x\le n, 1b,ym1\le b,y\le m, 0s<2310\le s<2^{31}.

Subtask Dependencies

All reasonable subtask dependencies are enabled for this problem.

Translated by ChatGPT 5