結果

問題 No.440 2次元チワワ問題
ユーザー しらっ亭しらっ亭
提出日時 2016-10-31 05:12:44
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,272 ms / 5,000 ms
コード長 3,119 bytes
コンパイル時間 1,907 ms
コンパイル使用メモリ 179,184 KB
実行使用メモリ 13,336 KB
最終ジャッジ日時 2023-08-16 11:33:23
合計ジャッジ時間 10,834 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 16 ms
4,600 KB
testcase_08 AC 40 ms
4,916 KB
testcase_09 AC 92 ms
8,152 KB
testcase_10 AC 12 ms
4,680 KB
testcase_11 AC 19 ms
4,380 KB
testcase_12 AC 24 ms
6,060 KB
testcase_13 AC 103 ms
9,996 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 AC 106 ms
10,584 KB
testcase_16 AC 16 ms
4,376 KB
testcase_17 AC 256 ms
13,292 KB
testcase_18 AC 253 ms
13,292 KB
testcase_19 AC 247 ms
13,240 KB
testcase_20 AC 246 ms
13,336 KB
testcase_21 AC 1,263 ms
13,308 KB
testcase_22 AC 1,260 ms
13,332 KB
testcase_23 AC 1,255 ms
13,308 KB
testcase_24 AC 1,272 ms
13,300 KB
testcase_25 AC 1,256 ms
13,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define _p(...) (void)printf(__VA_ARGS__)
#define forr(x,arr) for(auto&& x:arr)
#define _overload3(_1,_2,_3,name,...) name
#define _rep2(i,n) _rep3(i,0,n)
#define _rep3(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,_rep3,_rep2,)(__VA_ARGS__)
#define _rrep2(i,n) _rrep3(i,0,n)
#define _rrep3(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define rrep(...) _overload3(__VA_ARGS__,_rrep3,_rrep2,)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define bit(n) (1LL<<(n))
#define sz(x) ((int)(x).size())
#define fst first
#define snd second
using ll=long long;using pii=pair<int,int>;using vb=vector<bool>;
using vi=vector<int>;using vvi=vector<vi>;using vvvi=vector<vvi>;
using vl=vector<ll>;using vvl=vector<vl>;using vvvl=vector<vvl>;
using vd=vector<double>;using vvd=vector<vd>;using vvvd=vector<vvd>;
using vpii=vector<pii>;using vvpii=vector<vpii>;using vvvpii=vector<vvpii>;
template <typename T> T read() {T t; cin >> t; return t;}

int H, W;

pii conv1(int y, int x, int rot) {
  if (rot == 0) return {y, x};
  if (rot == 1) return {H-x-1, y};
  if (rot == 2) return {H-y-1, W-x-1};
  if (rot == 3) return {x, W-y-1};
  assert(false);
};

pii conv2(int y, int x, int rot) {
  if (rot == 0) return {y, x};
  if (rot == 1) return {x, H-y-1};
  if (rot == 2) return {H-y-1, W-x-1};
  if (rot == 3) return {W-x-1, y};
  assert(false);
};

void Main() {
  H = read<int>();
  W = read<int>();
  vector<string> M(H);
  rep(i, H) M[i] = read<string>();
  int Q = read<int>();
  vi T(Q), L(Q), B(Q), R(Q);
  rep(i, Q) {
    T[i] = read<int>()-1;
    L[i] = read<int>()-1;
    B[i] = read<int>()-1;
    R[i] = read<int>()-1;
  }

  vl ans(Q);
  int HH = H, WW = W;

  rep(rot, 4) {
    vvl c(HH, vl(WW+1)), w(HH, vl(WW+1)), cw(HH, vl(WW+1)), ww(HH, vl(WW+1)), cww(HH, vl(WW+1));

    auto get = [&](int y, int x) -> char {
      pii yx = conv1(y, x, rot);
      //_p("rot: %d, (%d,%d)->(%d,%d)\n", rot, y, x, yx.fst, yx.snd);
      return M[yx.fst][yx.snd];
    };

    rep(y, HH) rep(x, WW) {
      c[y][x+1] = c[y][x];
      w[y][x+1] = w[y][x];
      cw[y][x+1] = cw[y][x];
      ww[y][x+1] = ww[y][x];
      cww[y][x+1] = cww[y][x];

      if (get(y, x) == 'c') {
        c[y][x+1]++;
      }
      else {
        w[y][x+1]++;
        cw[y][x+1] += c[y][x];
        ww[y][x+1] += w[y][x];
        cww[y][x+1] += cw[y][x];
      }
    }

    rep(q, Q) {
      int t = T[q];
      int l = L[q];
      int b = B[q];
      int r = R[q];
      {
        pii tl = conv2(t, l, rot);
        pii br = conv2(b, r, rot);
        t = min(tl.fst, br.fst);
        b = max(tl.fst, br.fst) + 1;
        l = min(tl.snd, br.snd);
        r = max(tl.snd, br.snd) + 1;
      }

      ll n = 0;
      rep(y, t, b) {
        n += cww[y][r] - cww[y][l]
            - cw[y][l] * (w[y][r] - w[y][l])
            - c[y][l] * (ww[y][r] - ww[y][l] - (w[y][l] * (w[y][r] - w[y][l])));
      }
      ans[q] += n;
    }
    swap(HH, WW);
  }

  rep(q, Q) {
    cout << ans[q] << endl;
  }
}
int main() { cin.tie(nullptr); ios::sync_with_stdio(false); Main(); return 0; }
0