結果

問題 No.3207 Digital Font
ユーザー tko919
提出日時 2025-07-18 22:21:26
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,648 ms / 3,000 ms
コード長 24,016 bytes
コンパイル時間 5,034 ms
コンパイル使用メモリ 300,268 KB
実行使用メモリ 43,116 KB
最終ジャッジ日時 2025-07-18 22:22:16
合計ジャッジ時間 49,356 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "library/Template/template.hpp"
#include <bits/stdc++.h>
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 <typename T, typename S = T> S SUM(const vector<T> &a) {
    return accumulate(ALL(a), S(0));
}
template <typename S, typename T = S> 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 <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> 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 <typename T, typename U> 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 <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &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 <typename T> ostream &operator<<(ostream &os, const set<T> &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 <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
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 <unistd.h>
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 <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> 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<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        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 <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> 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 <typename T> 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 <typename T> 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 <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(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 <typename T> T get(T L) { // [0,L]
    return get() % (L + 1);
}
template <typename T> 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 <typename Iter> 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 <typename T> vector<T> select(int n, T L, T R) { // [L,R]
    if (n * 2 >= R - L + 1) {
        vector<T> ret(R - L + 1);
        iota(ALL(ret), L);
        shuffle(ALL(ret));
        ret.resize(n);
        return ret;
    } else {
        unordered_set<T> used;
        vector<T> 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<pair<int, int>> &es) {
    shuffle(ALL(es));
    vector<int> ord(n);
    iota(ALL(ord), 0);
    shuffle(ALL(ord));
    for (auto &[u, v] : es)
        u = ord[u], v = ord[v];
}
template <bool directed, bool multi, bool self>
vector<pair<int, int>> genGraph(int n, int m) {
    vector<pair<int, int>> 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<int> 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<pair<int, int>> genTree(int n) {
    vector<pair<int, int>> 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 <typename T> struct BIT {
    int n;
    T all = 0;
    vector<T> 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"

template<class T>struct BIT2D{
    int n; vector<BIT<T>> d;
    vector<int> xs; vector<vector<int>> ys;
    vector<pair<ll,ll>> 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<n;i+=(i&-i))ys[i].push_back(p[j].second);
        }
        d.resize(n);
        rep(i,0,n){
            sort(ALL(ys[i])); ys[i].erase(unique(ALL(ys[i])),ys[i].end());
            d[i]=BIT<T>(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<n;i+=(i&-i)){
            int idx=upper_bound(ALL(ys[i]),y)-ys[i].begin();
            d[i].add(idx,z);
        }
    }
    T sum(ll x,ll y){
        T res=0;
        int s=upper_bound(ALL(xs),x)-xs.begin();
        for(int i=s;i;i-=(i&-i)){
            int idx=upper_bound(ALL(ys[i]),y)-ys[i].begin();
            res+=d[i].sum(idx+1);
        }
        return res;
    }
    T sum(ll x1,ll y1,ll x2,ll y2){return sum(x2,y2)-sum(x1-1,y2)-sum(x2,y1-1)+sum(x1-1,y1-1);}
};

/**
 * @brief 2D Binary Indexed Tree
*/
#line 3 "library/DataStructure/staticrectsum.hpp"

template <typename XY, typename T> 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<P> plus;
    vector<Rect> 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<T> run() {
        ll n = plus.size(), q = que.size();
        sort(ALL(plus), [](P &p, P &q) { return p.y < q.y; });
        vector<XY> 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<Q> 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<T> res(q);
        int k = 0;
        BIT<T> 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 <typename XY, typename T> struct DynamicPointAddRectangleSum {
    using Point = tuple<XY, XY, T>;
    using Query = tuple<XY, XY, XY, XY>;
    vector<variant<Point, Query>> 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<T> run() {
        int Q = SZ(que);
        vector<int> isq(Q);
        vector<int> pos(Q);
        int ptr = 0;
        rep(i, 0, Q) if (holds_alternative<Query>(que[i])) {
            isq[i] = 1;
            pos[i] = ptr++;
        }
        vector<T> 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<XY, T> buf;
            rep(k, L, mid) if (!isq[k]) {
                XY x, y;
                T w;
                tie(x, y, w) = get<Point>(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<Query>(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 <typename T> T Inv(ll n) {
    static int md;
    static vector<T> buf({0, 1});
    if (md != T::get_mod()) {
        md = T::get_mod();
        buf = vector<T>({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 <typename T> T Fact(ll n, bool inv = 0) {
    static int md;
    static vector<T> buf({1, 1}), ibuf({1, 1});
    if (md != T::get_mod()) {
        md = T::get_mod();
        buf = ibuf = vector<T>({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<T>(SZ(ibuf)));
    }
    return inv ? ibuf[n] : buf[n];
}

template <typename T> T nPr(int n, int r, bool inv = 0) {
    if (n < 0 || n < r || r < 0)
        return 0;
    return Fact<T>(n, inv) * Fact<T>(n - r, inv ^ 1);
}
template <typename T> T nCr(int n, int r, bool inv = 0) {
    if (n < 0 || n < r || r < 0)
        return 0;
    return Fact<T>(n, inv) * Fact<T>(r, inv ^ 1) * Fact<T>(n - r, inv ^ 1);
}
// sum = n, r tuples
template <typename T> T nHr(int n, int r, bool inv = 0) {
    return nCr<T>(n + r - 1, r - 1, inv);
}
// sum = n, a nonzero tuples and b tuples
template <typename T> T choose(int n, int a, int b) {
    if (n == 0)
        return !a;
    return nCr<T>(n + b - 1, a + b - 1);
}

/**
 * @brief Combination
 */
#line 3 "library/Math/modint.hpp"

template <unsigned mod = 1000000007> 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<fp>(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 <unsigned mod> void rd(fp<mod> &x) {
    fastio::rd(x.v);
}
template <unsigned mod> void wt(fp<mod> 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<int, F1> buf1[2];
    DynamicPointAddRectangleSum<int, F2> buf2[2];
    vector<tuple<int, int, int>> 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<int>(W));
        vector B2(H, vector<int>(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<tuple<int, int, int, int>> 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<F1> ret1[2];
    vector<F2> 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;
}
0