#include #include #include #include #include #include #include using i64 = std::int64_t; constexpr std::pair DIRS4[] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } }; template using priority_queue = std::priority_queue, Comp>; signed main() { int n; std::cin >> n; std::vector h(n * n); std::vector s(n); std::vector a(n * n), b(n * n); for(auto& v : h) std::cin >> v; for(auto& v : s) std::cin >> v; for(const auto i : std::views::iota(0, n - 1)) { for(const auto j : std::views::iota(0, n)) { std::cin >> a[i * n + j]; } } for(const auto i : std::views::iota(0, n)) { for(const auto j : std::views::iota(0, n - 1)) { std::cin >> b[i * n + j]; } } auto parity = [&](const auto p) { return ((p / n) + p % n) % 2; }; std::vector sup(n * n, -(1UL << 62)); for(const auto t : std::views::iota(0, 2)) { auto cost = [&](const auto p, const auto q, const auto k) { const auto r = std::min(p, q); const auto w = k % 2 == 0 ? a[r] : b[r]; const auto x = h[p] + h[q]; return w * std::abs(x) + ((parity(p) + t) % 2 * 2 - 1) * x; }; priority_queue, std::greater<>> que; std::vector dist(n * n, 1UL << 62); for(const auto i : std::views::iota(0, n * n)) { if(s[i / n][i % n] == '=') { que.emplace(dist[i] = 0, i); } if(parity(i) == t) { if(s[i / n][i % n] == '+') que.emplace(dist[i] = 0, i); } else { if(s[i / n][i % n] == '-') que.emplace(dist[i] = 0, i); } } while(!que.empty()) { const auto [ d, v ] = que.top(); que.pop(); if(d > dist[v]) continue; const auto i = v / n, j = v % n; for(const auto k : std::views::iota(0, 4)) { const auto ni = i + DIRS4[k].first, nj = j + DIRS4[k].second; if(ni < 0 || nj < 0 || ni >= n || nj >= n) continue; const auto nv = ni * n + nj; const auto nd = d + cost(v, nv, k); if(nd >= dist[nv]) continue; que.emplace(dist[nv] = nd, nv); } } for(const auto i : std::views::iota(0, n * n)) { const auto v = h[i] + ((parity(i) + t) % 2 * 2 - 1) * dist[i]; if(sup[i] < v) sup[i] = v; } } int q; std::cin >> q; for(const auto _ : std::views::iota(0, q)) { int r, c; std::cin >> r >> c; --r, --c; i64 e; std::cin >> e; std::cout << (sup[r * n + c] >= e ? "Yes" : "No") << "\n"; } }