#include <iostream>
#include <set>
#include <vector>
#include <fstream>
#include <algorithm>
#include <random>
using namespace std;
mt19937 rnd((long long)(new char));
//#define DEBUG
#define DEBUG2
const int M = 10007;
vector < int > adia[M];
bool viz[M];
void dfs(int nod) {
for (int i : adia[nod]) {
if (!viz[i]) {
viz[i] = 1;
dfs(i);
}
}
}
string a, b;
int n;
bool brut(int r1, int c1, int r2, int c2) {
for (int i = 1; i < M; ++i)
viz[i] = 0, adia[i].clear();
for (int i = 1; i <= n; ++i) {
if (a[i] == 'R')
for (int j = 1; j < n; ++j)
adia[(i - 1) * n + j].push_back((i - 1) * n + j + 1);
else
for (int j = 1; j < n; ++j)
adia[(i - 1) * n + j + 1].push_back((i - 1) * n + j);
}
for (int j = 1; j <= n; ++j) {
if (b[j] == 'D')
for (int i = 1; i < n; ++i)
adia[(i - 1) * n + j].push_back(i * n + j);
else
for (int i = 1; i < n; ++i)
adia[i * n + j].push_back((i - 1) * n + j);
}
viz[(r1 - 1) * n + c1] = 1;
dfs((r1 - 1) * n + c1);
return viz[(r2 - 1) * n + c2];
}
int calc()
{
ifstream fin("input.in");
ofstream fout("output.out");
int t;
fin >> t;
while (t--) {
int q;
set < int > l, r, u, d;
fin >> n >> q >> a >> b;
a = '*' + a;
b = '*' + b;
for (int i = 1; i <= n; ++i) {
if (a[i] == 'L')
l.insert(i);
if (a[i] == 'R')
r.insert(i);
}
for (int i = 1; i <= n; ++i) {
if (b[i] == 'U')
u.insert(i);
if (b[i] == 'D')
d.insert(i);
}
#ifdef DEBUG
fout << "L: ";
for (int i : l)
fout << i << ' ';
fout << '\n';
fout << "R: ";
for (int i : r)
fout << i << ' ';
fout << '\n';
fout << "U: ";
for (int i : u)
fout << i << ' ';
fout << '\n';
fout << "D: ";
for (int i : d)
fout << i << ' ';
fout << '\n';
#endif // DEBUG
while (q--) {
int type;
fin >> type;
if (type == 1) { /// Vom folosi doar extremitatile si ce ne intereseaza
#ifdef DEBUG
fout << a << ' ' << b << '\n';
#endif // DEBUG
for (int i = 1; i < M; ++i)
adia[i].clear(), viz[i] = 0;
int r1, c1, r2, c2;
fin >> r1 >> c1 >> r2 >> c2;
// bool ans = brut(r1, c1, r2, c2);
// fout << (ans ? "YES\n" : "NO\n");
// continue;
vector < pair < int, int > > nodes;
vector < pair < int, char > > rows, cols;
nodes.push_back({-1, -1});
if (l.size()) {
rows.push_back({*l.begin(), 'L'});
rows.push_back({*l.rbegin(), 'L'});
}
if (r.size()) {
rows.push_back({*r.begin(), 'R'});
rows.push_back({*r.rbegin(), 'R'});
}
rows.push_back({r1, a[r1]});
rows.push_back({r2, a[r2]});
sort(rows.begin(), rows.end());
rows.erase(unique(rows.begin(), rows.end()), rows.end());
if (u.size()) {
cols.push_back({*u.begin(), 'U'});
cols.push_back({*u.rbegin(), 'U'});
}
if (d.size()) {
cols.push_back({*d.begin(), 'D'});
cols.push_back({*d.rbegin(), 'D'});
}
cols.push_back({c1, b[c1]});
cols.push_back({c2, b[c2]});
sort(cols.begin(), cols.end());
cols.erase(unique(cols.begin(), cols.end()), cols.end());
for (auto i : rows)
for (auto j : cols)
nodes.push_back({i.first, j.first});
#ifdef DEBUG
for (int j = 1; j < nodes.size(); ++j) {
auto i = nodes[j];
fout << '(' << i.first << ", " << i.second << ')' << (j == nodes.size() - 1 ? ";" : ", ");
}
fout << '\n';
#endif // DEBUG
for (int i = 1; i < nodes.size(); ++i) {
for (int j = i + 1; j < nodes.size(); ++j) {
if (nodes[i] == nodes[j])
return 1;
if (nodes[i].first == nodes[j].first) {
if (nodes[i].second < nodes[j].second) {
if (a[nodes[i].first] == 'L')
adia[j].push_back(i);
else
adia[i].push_back(j);
}
else {
if (a[nodes[i].first] == 'R')
adia[j].push_back(i);
else
adia[i].push_back(j);
}
}
if (nodes[i].second == nodes[j].second) {
if (nodes[i].first < nodes[j].first) {
if (b[nodes[i].second] == 'U')
adia[j].push_back(i);
else
adia[i].push_back(j);
}
else {
if (b[nodes[i].second] == 'D')
adia[j].push_back(i);
else
adia[i].push_back(j);
}
}
}
}
int start(-1), finish(-1);
for (int i = 1; i < nodes.size(); ++i) {
if (nodes[i] == make_pair(r1, c1))
start = i;
if (nodes[i] == make_pair(r2, c2))
finish = i;
}
#ifdef DEBUG
fout << '\n' << "start: " << start << "\nfinish: " << finish << '\n';
for (int i = 1; i < nodes.size(); ++i) {
fout << i << " -> ";
for (int j = 0; j < adia[i].size(); ++j)
fout << adia[i][j] << (j != adia[i].size() - 1 ? ", " : "");
fout << ";\n";
}
#endif // DEBUG
if (start == -1 || finish == -1)
return 1;
viz[start] = 1;
dfs(start);
bool ans = viz[finish];
#ifdef DEBUG2
if (ans != brut(r1, c1, r2, c2))
return 1;
#endif // DEBUG2
fout << (ans ? "YES\n" : "NO\n");
}
if (type == 2) {
int row;
fin >> row;
if (a[row] == 'L')
l.erase(row), r.insert(row), a[row] = 'R';
else
r.erase(row), l.insert(row), a[row] = 'L';
}
if (type == 3) {
int col;
fin >> col;
if (b[col] == 'U')
u.erase(col), d.insert(col), b[col] = 'D';
else
d.erase(col), u.insert(col), b[col] = 'U';
}
}
}
return 0;
}
int main() {
int its(100);
while (1) {
int n(4), t(10);
ofstream fout("input.in");
string a, b;
for (int i = 1; i <= n; ++i)
a.push_back((char)(rnd() & 7 ? 'L' : 'R'));
for (int i = 1; i <= n; ++i)
b.push_back((char)(rnd() & 7 ? 'U' : 'D'));
vector < vector < int > > ind;
for (int j = 1; j <= t; ++j) {
ind.push_back({});
for (int i = 1; i <= 4; ++i)
ind.back().push_back(rnd() % n + 1);
}
fout << "1\n" << n << ' ' << t << "\n" << a << '\n' << b << "\n";
for (auto i : ind) {
fout << "1 ";
for (auto j : i)
fout << j << ' ';
fout << '\n';
}
fout << '\n';
if (calc()) {
cout << "WRONG:\n";
cout << "1\n" << n << ' ' << t << '\n' << a << '\n' << b << "\n";
for (auto i : ind) {
cout << "1 ";
for (auto j : i)
cout << j << ' ';
cout << '\n';
}
cout << '\n';
return 0;
}
else {
cout << "CORRECT\n";
// cout << "1\n" << n << ' ' << t << '\n' << a << '\n' << b << "\n";
// for (auto i : ind) {
// cout << "1 ";
// for (auto j : i)
// cout << j << ' ';
// cout << '\n';
// }
// cout << '\n';
}
}
return 0;
}
/**
3
4 4
RLRR
DDUD
1 1 4 2 1
1 1 4 1 2
1 3 2 2 2
1 3 2 3 4
10 20
LRRRLLRRRL
DDDUUDDDDU
1 5 4 10 10
1 5 4 8 7
1 2 6 1 10
1 10 10 10 10
1 4 10 10 10
1 10 1 1 4
1 7 5 3 7
1 9 6 10 10
1 2 9 4 4
1 5 10 6 7
1 3 4 10 10
1 5 4 8 7
1 2 6 1 1
1 10 1 10 10
1 4 1 1 10
1 10 1 1 4
1 3 5 3 7
1 9 6 1 10
1 2 9 7 4
1 2 10 6 7
4 4
LRRL
DDDD
1 1 4 3 1
1 1 4 4 4
3 4
1 1 4 4 4
*/