結果

問題 No.1141 田グリッド
ユーザー 0w10w1
提出日時 2020-11-17 12:26:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 188 ms / 2,000 ms
コード長 3,117 bytes
コンパイル時間 2,284 ms
コンパイル使用メモリ 212,940 KB
実行使用メモリ 11,188 KB
最終ジャッジ日時 2023-09-30 14:07:47
合計ジャッジ時間 8,508 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 4 ms
4,380 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 136 ms
4,900 KB
testcase_14 AC 170 ms
11,188 KB
testcase_15 AC 150 ms
4,376 KB
testcase_16 AC 160 ms
4,436 KB
testcase_17 AC 188 ms
4,444 KB
testcase_18 AC 103 ms
4,432 KB
testcase_19 AC 103 ms
4,376 KB
testcase_20 AC 101 ms
4,380 KB
testcase_21 AC 173 ms
4,380 KB
testcase_22 AC 173 ms
4,380 KB
testcase_23 AC 173 ms
4,436 KB
testcase_24 AC 104 ms
4,380 KB
testcase_25 AC 102 ms
4,496 KB
testcase_26 AC 104 ms
4,420 KB
testcase_27 AC 104 ms
4,400 KB
testcase_28 AC 101 ms
4,488 KB
testcase_29 AC 100 ms
4,380 KB
testcase_30 AC 99 ms
4,488 KB
testcase_31 AC 104 ms
4,380 KB
testcase_32 AC 103 ms
4,428 KB
testcase_33 AC 102 ms
4,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<int m>
struct Mint {
  int v;
  Mint(int _v = 0) : v(_v) { v %= m; adjust(); }
  void adjust() { if (v < 0) v += m; if (v >= m) v -= m; assert(0 <= v && v < m); }
  Mint operator+() const { return *this; }
  Mint operator-() const { Mint r = Mint(0) - *this; return r.adjust(), r; }
  friend Mint operator+(Mint a, Mint b) { a.v += b.v; return a.adjust(), a; }
  Mint& operator+=(const Mint &b) { return v += b.v, adjust(), *this; }
  friend Mint operator-(Mint a, Mint b) { a.v -= b.v; return a.adjust(), a; }
  Mint& operator-=(const Mint &b) { return v -= b.v, adjust(), *this; }
  friend Mint operator*(Mint a, Mint b) { a.v = (int64_t) a.v * b.v % m; return a.adjust(), a; }
  friend Mint operator*(Mint a, int v) { return a * Mint(v); }
  friend Mint operator^(Mint a, int p) {
    int v = a.v;
    int r = 1;
    for (int i = p; i; i >>= 1) {
      if (i & 1) r = (int64_t) r * v % m;
      v = (int64_t) v * v % m;
    }
    return r;
  }
  friend Mint operator^(Mint a, Mint p) { return a^p.v; }
  friend Mint operator/(Mint a, Mint b) { return a * (Mint(b.v)^(m-2)); }
  friend ostream& operator<<(ostream &os, Mint a) { return os << a.v, os; }
};

int main() {
  ios::sync_with_stdio(false);
  
  const int M = 1e9 + 7;

  int H, W; { cin >> H >> W; }
  vector<vector<int>> A(H, vector<int>(W)); {
    for (int i = 0; i < H; ++i)
      for (int j = 0; j < W; ++j)
        cin >> A[i][j];
  }

  auto fadd = [](auto a, auto b) {
    if (is_same<decltype(a), int>::value) return a + b;
    return a * b;
  };

  auto add = [fadd](auto v, int x, int y, auto &ftree) {
    for (int i = x + 1; i < ftree.size(); i += i & -i) 
      for (int j = y + 1; j < ftree[i].size(); j += j & -j)
        ftree[i][j] = fadd(ftree[i][j], v);
  };

  auto sum = [fadd](int x, int y, auto &ftree) {
    auto r = ftree[0][0];
    for (int i = x + 1; i; i -= i & -i) 
      for (int j = y + 1; j; j -= j & -j)
        r = fadd(ftree[i][j], r);
    return r;
  };

  vector<vector<int>> cnt0(H + 1, vector<int>(W + 1)); {
    for (int i = 0; i < H; ++i)
      for (int j = 0; j < W; ++j) if (A[i][j] == 0) 
        add(1, i, j, cnt0);
  }

  vector<vector<Mint<M>>> mul1(H + 1, vector<Mint<M>>(W + 1, Mint<M>(1))); {
    for (int i = 0; i < H; ++i)
      for (int j = 0; j < W; ++j) 
        add(A[i][j] ? Mint<M>(A[i][j]) : Mint<M>(1), i, j, mul1);
  }

  int Q; { cin >> Q; }

  while (Q--) {
    int R, C; { cin >> R >> C; --R, --C; }

    auto fsub = [](auto a, auto b) {
      if (is_same<decltype(a), int>::value) return a - b;
      return a / b;
    };

    auto query = [&](auto &ftree) {
      auto h = fadd(fsub(sum(H - 1, W - 1, ftree), sum(R, W - 1, ftree)), sum(R - 1, W - 1, ftree));
      auto v = fadd(fsub(sum(H - 1, W - 1, ftree), sum(H - 1, C, ftree)), sum(H - 1, C - 1, ftree));
      auto r = fsub(fadd(h, v), sum(H - 1, W - 1, ftree));
      if (is_same<decltype(r), int>::value) r += A[R][C] == 0;
      else if (A[R][C]) r = r * A[R][C];
      return r;
    };

    if (query(cnt0)) cout << 0 << "\n";
    else cout << query(mul1) << "\n";
  }
}

0