結果

問題 No.3207 Digital Font
ユーザー hint908
提出日時 2025-07-18 22:52:56
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,496 ms / 3,000 ms
コード長 6,763 bytes
コンパイル時間 4,114 ms
コンパイル使用メモリ 308,208 KB
実行使用メモリ 73,296 KB
最終ジャッジ日時 2025-07-18 22:53:53
合計ジャッジ時間 50,835 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")


#include<bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
template<class T> using VVV = V<VV<T>>;
template<class T> using VVVV = VV<VV<T>>;
#define rep(i,n) for(ll i=0ll;(i)<(n);(i)++)
#define REP(i,a,n) for(ll i=(a);(i)<(n);(i)++)
#define rrep(i,n) for(ll i=(n)-1;(i)>=(0ll);(i)--)
#define RREP(i,a,n) for(ll i=(n)-1;(i)>=(a);(i)--)
const long long INF = (1LL << 60);
const long long mod99 = 998244353;
const long long mod107 = 1000000007;
const long long mod = mod99;
#define eb emplace_back
#define be(v) (v).begin(),(v).end()
#define all(v) (v).begin(),(v).end()
#define foa(i,v) for(auto& (i) : (v))
#define UQ(v) sort(be(v)), (v).erase(unique(be(v)), (v).end())
#define UQ2(v,cmp) sort(be(v)), (v).erase(unique(be(v),cmp), (v).end())
#define UQ3(v,cmp) sort(be(v),cmp), (v).erase(unique(be(v)), (v).end())
#define UQ4(v,cmp,cmp2) sort(be(v), cmp), (v).erase(unique(be(v),cmp2), (v).end())
#define LB(x,v) (lower_bound(be(v),(x))-(v).begin())
#define LB2(x,v,cmp) (lower_bound(be(v),(x),(cmp))-(v).begin())
#define UB(x,v) (upper_bound(be(v),(x))-(v).begin())
#define UB2(x,v,cmp) (upper_bound(be(v),(x),(cmp))-(v).begin())
#define dout()  cout << fixed << setprecision(20)
#define randinit() srand((unsigned)time(NULL))

template<class T, class U> bool chmin(T& t, const U& u) { if (t > u){ t = u; return 1;} return 0; }
template<class T, class U> bool chmax(T& t, const U& u) { if (t < u){ t = u; return 1;} return 0; }


ll Rnd(ll L=0, ll R=mod99){return rand()%(R-L)+L;}
template<class K, class M> struct range_tree {
   using S = typename M::S;
   using D = typename M::D;

   private:
   vector<pair<K, K>> ps;
   vector<K> xs;
   vector<vector<K>> ys;
   vector<D> ds;
   int n;
   int id(K x) const { return lower_bound(all(xs), x) - xs.begin(); }

   int id(int k, K y) const { return lower_bound(all(ys[k]), y) - ys[k].begin(); }

   public:
   void add(K x, K y) { ps.emplace_back(x, y); }
   void build() {
      sort(ps.begin(), ps.end());
      ps.erase(unique(all(ps)), ps.end());
      n = size(ps);
      xs.reserve(n);
      for(auto& [x, _] : ps) xs.push_back(x);
      ys.resize(2 * n);
      ds.resize(2 * n, M::init(0));
      for(int i = 0; i < n; i++) {
         ys[i + n] = {ps[i].second};
         ds[i + n] = M::init(1);
      }
      for(int i = n - 1; i > 0; i--) {
         ys[i].resize(size(ys[i << 1]) + size(ys[(i << 1) | 1]));
         merge(all(ys[i << 1]), all(ys[(i << 1) | 1]), ys[i].begin());
         ys[i].erase(unique(all(ys[i])), ys[i].end());
         ds[i] = M::init(size(ys[i]));
      }
   }

   void apply(K x, K y, S a) {
      int k = lower_bound(all(ps), make_pair(x, y)) - ps.begin() + n;
      while(k > 0) {
         M::apply(ds[k], id(k, y), a);
         k >>= 1;
      }
   }

   S prod(K x1, K y1, K x2, K y2) {
      int a = id(x1), b = id(x2);
      a += n;
      b += n;
      S l = M::e(), r = M::e();
      while(a < b) {
         if(a & 1) {
            l = M::op(l, M::prod(ds[a], id(a, y1), id(a, y2)));
            ++a;
         }
         if(b & 1) {
            --b;
            r = M::op(M::prod(ds[b], id(b, y1), id(b, y2)), r);
         }
         a >>= 1;
         b >>= 1;
      }
      return M::op(l, r);
   }
};

using u64 = uint64_t;
const u64 MOD = (LLONG_MAX) / 4;
u64 add(u64 a, u64 b){
    a += b;
    if(a >= MOD) a -= MOD;
    return a;
}
u64 mul(u64 a, u64 b){
    auto c = (__uint128_t)a * b;
    return add(c >> 61, c & MOD);
}
struct BIT {
   vector<ll> a;
   BIT(ll n) : a(n + 1) {}
   void add(ll i, ll x) {  // A[i] += x
      i++;
      while(i < size(a)) {
         a[i] += x;
         a[i] %= MOD;
         i += i & -i;
      }
   }
   ll sum(ll r) {
      ll s = 0;
      while(r) {
         s += a[r];
         s %= MOD;
         r -= r & -r;
      }
      return s;
   }
   ll sum(ll l, ll r) {  // sum of A[l, r)
      return (sum(r) + MOD - sum(l)) % MOD;
   }
};

// モノイド
struct M {
  using S = u64;  // データ(モノイド)の型
  using D = BIT;  // ノードに持たせるデータ構造の型
  static S op(S a, S b) { return add(a, b); } // Sの二項演算
  static S e() { return 0; }  // Sの単位元
  static D init(int n) { return BIT(n); }  // Dを長さnで初期化する関数
  static void apply(D& bit, int k, const S& v) { bit.add(k, v); } // D のk番目に v を適用する関数
  static S prod(D& bit, int l, int r) { return bit.sum(l, r); } // D の[l, r) に対するクエリを行う関数
};

u64 modinv(u64 n, u64 p = mod) { 
    if(n == 1) return 1;
    // cout << n << " " << p << endl;
    return p - mul(modinv(p % n, p), (p / n)) % p;
    // return modpow(n, p - 2, p);
}


u64 modpow(u64 n, long long k, u64 p = mod){
    u64 a = n % p;
    u64 ans = 1;
    while(k != 0) {
        if(k & 1) ans = mul(ans, a);
        k /= 2;
        a = mul(a, a);
    }
    
    return ans;  
}


/*
rt.add(x, y): 座標 (x, y) を追加
rt.build(): クエリを受け付ける準備をする
rt.apply(x, y, a): 座標 (x, y) に a を適用
rt.prod(x1, y1, x2, y2): 座標 x \in [x1, x2), y \in [y1, y2) の領域にクエリを行う
*/

void solve(){
    
    ll h,w,n;
    cin >> h >> w >> n;
    const u64 r = Rnd();
    range_tree<int, M> rt1, rt2;
    V<ll> a, b, x;
    rep(i, n){
        ll p,q,r;
        cin >> p >> q >> r;
        if(r == 0) continue;
        p--;
        q--;
        rt1.add(p, q);
        rt2.add(h-1-p, w-1-q);
        a.eb(p);
        b.eb(q);
        x.eb(r);
    }
    rt1.build();
    rt2.build();
    rep(i, a.size()){
        rt1.apply(a[i], b[i], mul(r + x[i], modpow(908, a[i]*w+b[i], MOD)));
        if(x[i] == 6 or x[i] == 9) x[i] = 15-x[i];
        rt2.apply(h-1-a[i], w-1-b[i], mul(r + x[i], modpow(908, (h-1-a[i])*w+(w-1-b[i]), MOD)));
    }
    ll q;
    cin >> q;
    rep(i, q){
        ll a,b,c,d;
        cin >> a >> b >> c >> d;
        a--;
        b--;
        u64 A = rt1.prod(a, b, c, d);
        u64 B = rt2.prod(h-c, w-d, h-a, w-b);
        (((A %= MOD) += MOD) %= MOD);
        (((B %= MOD) += MOD) %= MOD);
        // cout << modpow(r, a*w+b, MOD) << endl;
        A = mul(A, modinv(modpow(908, a*w+b, MOD), MOD));
        // cout << modpow(r, (h-c)*w+(w-d), MOD) % MOD << endl;
        B = mul(B, modinv(modpow(908, (h-c)*w+(w-d), MOD) % MOD, MOD));
        
        // cout << A << " " << B << endl;
        if(A == B) cout << "Yes\n";
        else cout << "No\n";
    }
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t=1;
    // cin >> t;
    rep(i,t) solve();
}
0