#line 1 "library/Template/template.hpp" #include using namespace std; #define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--) #define ALL(v) (v).begin(), (v).end() #define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end()) #define SZ(v) (int)v.size() #define MIN(v) *min_element(ALL(v)) #define MAX(v) *max_element(ALL(v)) #define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin()) #define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin()) using uint = unsigned int; using ll = long long int; using ull = unsigned long long; using i128 = __int128_t; using u128 = __uint128_t; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; template S SUM(const vector &a) { return accumulate(ALL(a), S(0)); } template S POW(S a, T b) { S ret = 1, base = a; for (;;) { if (b & 1) ret *= base; b >>= 1; if (b == 0) break; base *= base; } return ret; } template inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template T ceil(T x, U y) { assert(y != 0); if (y < 0) x = -x, y = -y; return (x > 0 ? (x + y - 1) / y : x / y); } template T floor(T x, U y) { assert(y != 0); if (y < 0) x = -x, y = -y; return (x > 0 ? x / y : (x - y + 1) / y); } template int popcnt(T x) { return __builtin_popcountll(x); } template int topbit(T x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } template int lowbit(T x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } template ostream &operator<<(ostream &os, const pair &p) { os << "P(" << p.first << ", " << p.second << ")"; return os; } template ostream &operator<<(ostream &os, const vector &vec) { os << "{"; for (int i = 0; i < vec.size(); i++) { os << vec[i] << (i + 1 == vec.size() ? "" : ", "); } os << "}"; return os; } template ostream &operator<<(ostream &os, const map &map_var) { os << "{"; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << "(" << itr->first << ", " << itr->second << ")"; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } template ostream &operator<<(ostream &os, const set &set_var) { os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } #ifdef LOCAL #define debug 1 #define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__) #else #define debug 0 #define show(...) true #endif template void _show(int i, T name) { cerr << '\n'; } template void _show(int i, const T1 &a, const T2 &b, const T3 &...c) { for (; a[i] != ',' && a[i] != '\0'; i++) cerr << a[i]; cerr << ":" << b << " "; _show(i + 1, a, c...); } #line 2 "library/Utility/fastio.hpp" #include namespace fastio { static constexpr uint32_t SZ = 1 << 17; char ibuf[SZ]; char obuf[SZ]; char out[100]; // pointer of ibuf, obuf uint32_t pil = 0, pir = 0, por = 0; struct Pre { char num[10000][4]; constexpr Pre() : num() { for (int i = 0; i < 10000; i++) { int n = i; for (int j = 3; j >= 0; j--) { num[i][j] = n % 10 | '0'; n /= 10; } } } } constexpr pre; inline void load() { memmove(ibuf, ibuf + pil, pir - pil); pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin); pil = 0; if (pir < SZ) ibuf[pir++] = '\n'; } inline void flush() { fwrite(obuf, 1, por, stdout); por = 0; } void rd(char &c) { do { if (pil + 1 > pir) load(); c = ibuf[pil++]; } while (isspace(c)); } void rd(string &x) { x.clear(); char c; do { if (pil + 1 > pir) load(); c = ibuf[pil++]; } while (isspace(c)); do { x += c; if (pil == pir) load(); c = ibuf[pil++]; } while (!isspace(c)); } template void rd_real(T &x) { string s; rd(s); x = stod(s); } template void rd_integer(T &x) { if (pil + 100 > pir) load(); char c; do c = ibuf[pil++]; while (c < '-'); bool minus = 0; if constexpr (is_signed::value || is_same_v) { if (c == '-') { minus = 1, c = ibuf[pil++]; } } x = 0; while ('0' <= c) { x = x * 10 + (c & 15), c = ibuf[pil++]; } if constexpr (is_signed::value || is_same_v) { if (minus) x = -x; } } void rd(int &x) { rd_integer(x); } void rd(ll &x) { rd_integer(x); } void rd(i128 &x) { rd_integer(x); } void rd(uint &x) { rd_integer(x); } void rd(ull &x) { rd_integer(x); } void rd(u128 &x) { rd_integer(x); } void rd(double &x) { rd_real(x); } void rd(long double &x) { rd_real(x); } template void rd(pair &p) { return rd(p.first), rd(p.second); } template void rd_tuple(T &t) { if constexpr (N < std::tuple_size::value) { auto &x = std::get(t); rd(x); rd_tuple(t); } } template void rd(tuple &tpl) { rd_tuple(tpl); } template void rd(array &x) { for (auto &d : x) rd(d); } template void rd(vector &x) { for (auto &d : x) rd(d); } void read() {} template void read(H &h, T &...t) { rd(h), read(t...); } void wt(const char c) { if (por == SZ) flush(); obuf[por++] = c; } void wt(const string s) { for (char c : s) wt(c); } void wt(const char *s) { size_t len = strlen(s); for (size_t i = 0; i < len; i++) wt(s[i]); } template void wt_integer(T x) { if (por > SZ - 100) flush(); if (x < 0) { obuf[por++] = '-', x = -x; } int outi; for (outi = 96; x >= 10000; outi -= 4) { memcpy(out + outi, pre.num[x % 10000], 4); x /= 10000; } if (x >= 1000) { memcpy(obuf + por, pre.num[x], 4); por += 4; } else if (x >= 100) { memcpy(obuf + por, pre.num[x] + 1, 3); por += 3; } else if (x >= 10) { int q = (x * 103) >> 10; obuf[por] = q | '0'; obuf[por + 1] = (x - q * 10) | '0'; por += 2; } else obuf[por++] = x | '0'; memcpy(obuf + por, out + outi + 4, 96 - outi); por += 96 - outi; } template void wt_real(T x) { ostringstream oss; oss << fixed << setprecision(15) << double(x); string s = oss.str(); wt(s); } void wt(int x) { wt_integer(x); } void wt(ll x) { wt_integer(x); } void wt(i128 x) { wt_integer(x); } void wt(uint x) { wt_integer(x); } void wt(ull x) { wt_integer(x); } void wt(u128 x) { wt_integer(x); } void wt(double x) { wt_real(x); } void wt(long double x) { wt_real(x); } template void wt(const pair val) { wt(val.first); wt(' '); wt(val.second); } template void wt_tuple(const T t) { if constexpr (N < std::tuple_size::value) { if constexpr (N > 0) { wt(' '); } const auto x = std::get(t); wt(x); wt_tuple(t); } } template void wt(tuple tpl) { wt_tuple(tpl); } template void wt(const array val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) wt(' '); wt(val[i]); } } template void wt(const vector val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) wt(' '); wt(val[i]); } } void print() { wt('\n'); } template void print(Head &&head, Tail &&...tail) { wt(head); if (sizeof...(Tail)) wt(' '); print(forward(tail)...); } void __attribute__((destructor)) _d() { flush(); } } // namespace fastio using fastio::flush; using fastio::print; using fastio::read; inline void first(bool i = true) { print(i ? "first" : "second"); } inline void Alice(bool i = true) { print(i ? "Alice" : "Bob"); } inline void Takahashi(bool i = true) { print(i ? "Takahashi" : "Aoki"); } inline void yes(bool i = true) { print(i ? "yes" : "no"); } inline void Yes(bool i = true) { print(i ? "Yes" : "No"); } inline void No() { print("No"); } inline void YES(bool i = true) { print(i ? "YES" : "NO"); } inline void NO() { print("NO"); } inline void Yay(bool i = true) { print(i ? "Yay!" : ":("); } inline void Possible(bool i = true) { print(i ? "Possible" : "Impossible"); } inline void POSSIBLE(bool i = true) { print(i ? "POSSIBLE" : "IMPOSSIBLE"); } /** * @brief Fast IO */ #line 2 "library/Utility/random.hpp" namespace Random { mt19937_64 randgen(chrono::steady_clock::now().time_since_epoch().count()); using u64 = unsigned long long; u64 get() { return randgen(); } template T get(T L) { // [0,L] return get() % (L + 1); } template T get(T L, T R) { // [L,R] return get(R - L) + L; } double uniform() { return double(get(1000000000)) / 1000000000; } string str(int n) { string ret; rep(i, 0, n) ret += get('a', 'z'); return ret; } template void shuffle(Iter first, Iter last) { if (first == last) return; int len = 1; for (auto it = first + 1; it != last; it++) { len++; int j = get(0, len - 1); if (j != len - 1) iter_swap(it, first + j); } } template vector select(int n, T L, T R) { // [L,R] if (n * 2 >= R - L + 1) { vector ret(R - L + 1); iota(ALL(ret), L); shuffle(ALL(ret)); ret.resize(n); return ret; } else { unordered_set used; vector ret; while (SZ(used) < n) { T x = get(L, R); if (!used.count(x)) { used.insert(x); ret.push_back(x); } } return ret; } } void relabel(int n, vector> &es) { shuffle(ALL(es)); vector ord(n); iota(ALL(ord), 0); shuffle(ALL(ord)); for (auto &[u, v] : es) u = ord[u], v = ord[v]; } template vector> genGraph(int n, int m) { vector> cand, es; rep(u, 0, n) rep(v, 0, n) { if (!self and u == v) continue; if (!directed and u > v) continue; cand.push_back({u, v}); } if (m == -1) m = get(SZ(cand)); // chmin(m, SZ(cand)); vector ord; if (multi) rep(_, 0, m) ord.push_back(get(SZ(cand) - 1)); else { ord = select(m, 0, SZ(cand) - 1); } for (auto &i : ord) es.push_back(cand[i]); relabel(n, es); return es; } vector> genTree(int n) { vector> es; rep(i, 1, n) es.push_back({get(i - 1), i}); relabel(n, es); return es; } }; // namespace Random /** * @brief Random */ #line 4 "sol.cpp" #line 2 "library/DataStructure/bit.hpp" template struct BIT { int n; T all = 0; vector val; BIT(int _n = 0) : n(_n), val(_n + 10) {} void clear() { val.assign(n + 10, 0); all = T(); } void add(int i, T x) { for (i++; i <= n; i += (i & -i)) val[i] = val[i] + x; all += x; } T sum(int i) { i = clamp(i, 0, n); T res = 0; for (; i; i -= (i & -i)) res += val[i]; return res; } // [L,R) T sum(int L, int R) { if (L > R) return T(0); return sum(R) - sum(L); } int lower_bound(T x) { int ret = 0, len = 1; while (2 * len <= n) len <<= 1; for (; len >= 1; len >>= 1) { if (ret + len <= n and val[ret + len] < x) { ret += len; x -= val[ret]; } } return ret; } }; /** * @brief Binary Indexed Tree */ #line 3 "library/DataStructure/2dbit.hpp" templatestruct BIT2D{ int n; vector> d; vector xs; vector> ys; vector> p; BIT2D(){} void push(ll x,ll y){p.push_back({x,y});} void init(){ rep(i,0,p.size())xs.push_back(p[i].first); sort(ALL(xs)); xs.erase(unique(ALL(xs)),xs.end()); n=xs.size()+1; ys.resize(n); rep(j,0,p.size()){ int s=upper_bound(ALL(xs),p[j].first)-xs.begin(); for(int i=s;i(ys[i].size()+2); } } void add(ll x,ll y,T z=1){ int s=upper_bound(ALL(xs),x)-xs.begin(); for(int i=s;i struct StaticRectangleSum { struct P { XY x, y; T w; }; struct Rect { XY l, d, r, u; }; struct Q { XY x, d, u; int id, inv; }; vector

plus; vector que; StaticRectangleSum() {} void add(XY x, XY y, T w) { plus.push_back({x, y, w}); } void query(XY l, XY d, XY r, XY u) { que.push_back({l, d, r, u}); } vector run() { ll n = plus.size(), q = que.size(); sort(ALL(plus), [](P &p, P &q) { return p.y < q.y; }); vector ys; rep(i, 0, n) ys.push_back(plus[i].y); ys.erase(unique(ALL(ys)), ys.end()); rep(i, 0, n) plus[i].y = lower_bound(ALL(ys), plus[i].y) - ys.begin(); vector qs; rep(i, 0, q) { auto &[l, d, r, u] = que[i]; d = lower_bound(ALL(ys), d) - ys.begin(); u = lower_bound(ALL(ys), u) - ys.begin(); qs.push_back({l, d, u, i, -1}); qs.push_back({r, d, u, i, 1}); } sort(ALL(plus), [](P &p, P &q) { return p.x < q.x; }); sort(ALL(qs), [](Q &p, Q &q) { return p.x < q.x; }); vector res(q); int k = 0; BIT bit(ys.size()); for (auto &q : qs) { while (k < n and plus[k].x < q.x) { bit.add(plus[k].y, plus[k].w); k++; } res[q.id] += bit.sum(q.d, q.u) * q.inv; } return res; } }; /** * @brief Static Rectangle Sum */ #line 3 "library/DataStructure/dynamicrectsum.hpp" template struct DynamicPointAddRectangleSum { using Point = tuple; using Query = tuple; vector> que; DynamicPointAddRectangleSum() {} void add(XY x, XY y, T w) { que.push_back(Point{x, y, w}); } void query(XY L, XY D, XY R, XY U) { que.push_back(Query{L, D, R, U}); } vector run() { int Q = SZ(que); vector isq(Q); vector pos(Q); int ptr = 0; rep(i, 0, Q) if (holds_alternative(que[i])) { isq[i] = 1; pos[i] = ptr++; } vector ret(ptr); auto rec = [&](auto &rec, int L, int R) -> void { if (L + 1 >= R) return; int mid = (L + R) >> 1; rec(rec, L, mid); rec(rec, mid, R); StaticRectangleSum buf; rep(k, L, mid) if (!isq[k]) { XY x, y; T w; tie(x, y, w) = get(que[k]); buf.add(x, y, w); } rep(k, mid, R) if (isq[k]) { XY L, D, R, U; tie(L, D, R, U) = get(que[k]); buf.query(L, D, R, U); } auto sub = buf.run(); ptr = 0; rep(k, mid, R) if (isq[k]) { ret[pos[k]] += sub[ptr++]; } }; rec(rec, 0, Q); return ret; } }; /** * @brief Dynamic Point Add Rectangle Sum */ #line 7 "sol.cpp" // #include "Math/miller.hpp" #line 2 "library/Math/comb.hpp" template T Inv(ll n) { static int md; static vector buf({0, 1}); if (md != T::get_mod()) { md = T::get_mod(); buf = vector({0, 1}); } assert(n > 0); n %= md; while (SZ(buf) <= n) { int k = SZ(buf), q = (md + k - 1) / k; buf.push_back(buf[k * q - md] * q); } return buf[n]; } template T Fact(ll n, bool inv = 0) { static int md; static vector buf({1, 1}), ibuf({1, 1}); if (md != T::get_mod()) { md = T::get_mod(); buf = ibuf = vector({1, 1}); } assert(n >= 0 and n < md); while (SZ(buf) <= n) { buf.push_back(buf.back() * SZ(buf)); ibuf.push_back(ibuf.back() * Inv(SZ(ibuf))); } return inv ? ibuf[n] : buf[n]; } template T nPr(int n, int r, bool inv = 0) { if (n < 0 || n < r || r < 0) return 0; return Fact(n, inv) * Fact(n - r, inv ^ 1); } template T nCr(int n, int r, bool inv = 0) { if (n < 0 || n < r || r < 0) return 0; return Fact(n, inv) * Fact(r, inv ^ 1) * Fact(n - r, inv ^ 1); } // sum = n, r tuples template T nHr(int n, int r, bool inv = 0) { return nCr(n + r - 1, r - 1, inv); } // sum = n, a nonzero tuples and b tuples template T choose(int n, int a, int b) { if (n == 0) return !a; return nCr(n + b - 1, a + b - 1); } /** * @brief Combination */ #line 3 "library/Math/modint.hpp" template struct fp { static_assert(mod < uint(1) << 31); unsigned v; static constexpr int get_mod() { return mod; } constexpr unsigned inv() const { assert(v != 0); int x = v, y = mod, p = 1, q = 0, t = 0, tmp = 0; while (y > 0) { t = x / y; x -= t * y, p -= t * q; tmp = x, x = y, y = tmp; tmp = p, p = q, q = tmp; } if (p < 0) p += mod; return p; } constexpr fp(ll x = 0) : v(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {} fp operator-() const { return fp() - *this; } fp pow(ull t) { fp res = 1, b = *this; while (t) { if (t & 1) res *= b; b *= b; t >>= 1; } return res; } fp &operator+=(const fp &x) { if ((v += x.v) >= mod) v -= mod; return *this; } fp &operator-=(const fp &x) { if ((v += mod - x.v) >= mod) v -= mod; return *this; } fp &operator*=(const fp &x) { v = ull(v) * x.v % mod; return *this; } fp &operator/=(const fp &x) { if (x.v < 15000000) { return *this *= Inv(x.v); } v = ull(v) * x.inv() % mod; return *this; } fp operator+(const fp &x) const { return fp(*this) += x; } fp operator-(const fp &x) const { return fp(*this) -= x; } fp operator*(const fp &x) const { return fp(*this) *= x; } fp operator/(const fp &x) const { return fp(*this) /= x; } bool operator==(const fp &x) const { return v == x.v; } bool operator!=(const fp &x) const { return v != x.v; } friend istream &operator>>(istream &is, fp &x) { return is >> x.v; } friend ostream &operator<<(ostream &os, const fp &x) { return os << x.v; } }; template void rd(fp &x) { fastio::rd(x.v); } template void wt(fp x) { fastio::wt(x.v); } /** * @brief Modint */ #line 10 "sol.cpp" using F1 = fp<938238293>; using F2 = fp<963688967>; int main() { // for (;;) { // int p = Random::get(9 * POW(10, 8), POW(10, 9)); // if (Miller(p)) { // show(p); // break; // } // } int H, W, n; read(H, W, n); DynamicPointAddRectangleSum buf1[2]; DynamicPointAddRectangleSum buf2[2]; vector> b1, b2; rep(_, 0, n) { int x, y, w; read(x, y, w); x--, y--; b1.push_back({x, y, w}); if (w == 6) w = 9; else if (w == 9) w = 6; b2.push_back({H - 1 - x, W - 1 - y, w}); } if (debug) { vector B1(H, vector(W)); vector B2(H, vector(W)); for (auto &[x, y, w] : b1) { B1[x][y] = w; } for (auto &[x, y, w] : b2) { B2[x][y] = w; } rep(i, 0, H) print(B1[i]); rep(i, 0, H) print(B2[i]); } F1 baseX1 = Random::get(0, F1::get_mod()); F1 baseY1 = Random::get(0, F1::get_mod()); F2 baseX2 = Random::get(0, F2::get_mod()); F2 baseY2 = Random::get(0, F2::get_mod()); for (auto &[x, y, w] : b1) { F1 W1 = baseX1.pow(x) * baseY1.pow(y) * w; buf1[0].add(x, y, W1); F2 W2 = baseX2.pow(x) * baseY2.pow(y) * w; buf2[0].add(x, y, W2); } for (auto &[x, y, w] : b2) { F1 W1 = baseX1.pow(x) * baseY1.pow(y) * w; buf1[1].add(x, y, W1); F2 W2 = baseX2.pow(x) * baseY2.pow(y) * w; buf2[1].add(x, y, W2); } int Q; read(Q); vector> que(Q); rep(i, 0, Q) { int L, D, R, U; read(L, D, R, U); L--, D--; que[i] = {L, D, R, U}; } for (auto [L, D, R, U] : que) { buf1[0].query(L, D, R, U); buf2[0].query(L, D, R, U); swap(L, R); L = H - L, R = H - R; swap(D, U); D = W - D, U = W - U; buf1[1].query(L, D, R, U); buf2[1].query(L, D, R, U); } vector ret1[2]; vector ret2[2]; ret1[0] = buf1[0].run(); ret1[1] = buf1[1].run(); ret2[0] = buf2[0].run(); ret2[1] = buf2[1].run(); rep(i, 0, Q) { auto [L, D, R, U] = que[i]; F1 sum10 = ret1[0][i]; F2 sum20 = ret2[0][i]; sum10 *= baseX1.pow(H - L) * baseY1.pow(W - D); sum20 *= baseX2.pow(H - L) * baseY2.pow(W - D); swap(L, R); L = H - L, R = H - R; swap(D, U); D = W - D, U = W - U; F1 sum11 = ret1[1][i]; F2 sum21 = ret2[1][i]; sum11 *= baseX1.pow(H - L) * baseY1.pow(W - D); sum21 *= baseX2.pow(H - L) * baseY2.pow(W - D); Yes(sum10 == sum11 and sum20 == sum21); } return 0; }