結果

問題 No.440 2次元チワワ問題
ユーザー はまやんはまやん
提出日時 2017-04-20 17:14:50
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 968 ms / 5,000 ms
コード長 2,269 bytes
コンパイル時間 1,583 ms
コンパイル使用メモリ 170,808 KB
実行使用メモリ 12,544 KB
最終ジャッジ日時 2024-07-19 21:09:27
合計ジャッジ時間 7,741 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)





typedef long long ll;
int H, W; string _S[501]; int Q, A[10101], B[10101], C[10101], D[10101];
//-----------------------------------------------------------------------------------
string S[501];
ll c[501][501], w[501][501], cw[501][501], cww[501][501];
void pre(int HH, int WW) {
    rep(y, 1, HH + 1) rep(x, 0, WW + 1) c[y][x] = w[y][x] = cw[y][x] = cww[y][x] = 0;
    
    rep(y, 1, HH + 1) {
        rep(x, 1, WW + 1) {
            if (S[y][x] == 'c') {
                c[y][x] = c[y][x - 1] + 1;
                w[y][x] = w[y][x - 1];
                cw[y][x] = cw[y][x - 1];
                cww[y][x] = cww[y][x - 1];
            } else {
                c[y][x] = c[y][x - 1];
                w[y][x] = w[y][x - 1] + 1;
                cw[y][x] = cw[y][x - 1] + c[y][x - 1];
                cww[y][x] = cww[y][x - 1] + cw[y][x - 1];
            }
        }
    }
}
ll get(int y, int L, int R) {
    ll res = cww[y][R] - cww[y][L - 1];
    ll cnt_w = w[y][R] - w[y][L - 1];
    res -= c[y][L - 1] * cnt_w * (cnt_w - 1) / 2;
    res -= cw[y][L - 1] * cnt_w;
    return res;
}
//-----------------------------------------------------------------------------------
ll ans[101010];
int main() {
    cin >> H >> W;
    rep(y, 1, H + 1) cin >> _S[y], _S[y] = "#" + _S[y];
    cin >> Q;
    rep(q, 0, Q) scanf("%d%d%d%d", &A[q], &B[q], &C[q], &D[q]);

    rep(y, 1, max(H, W) + 1) rep(x, 0, max(H, W) + 1) S[y] += "#";

    // →
    rep(y, 1, H + 1) rep(x, 1, W + 1) S[y][x] = _S[y][x];
    pre(H, W);
    rep(q, 0, Q) rep(y, A[q], C[q] + 1) ans[q] += get(y, B[q], D[q]);

    // ←
    rep(y, 1, H + 1) rep(x, 1, W + 1) S[y][W - x + 1] = _S[y][x];
    pre(H, W);
    rep(q, 0, Q) rep(y, A[q], C[q] + 1) ans[q] += get(y, W - D[q] + 1, W - B[q] + 1);
    
    // ↓
    rep(y, 1, H + 1) rep(x, 1, W + 1) S[x][y] = _S[y][x];
    pre(W, H);
    rep(q, 0, Q) rep(y, B[q], D[q] + 1) ans[q] += get(y, A[q], C[q]);

    // ↑
    rep(y, 1, H + 1) rep(x, 1, W + 1) S[x][H - y + 1] = _S[y][x];
    pre(W, H);
    rep(q, 0, Q) rep(y, B[q], D[q] + 1) ans[q] += get(y, H - C[q] + 1, H - A[q] + 1);

    //rep(y, 1, H + 1) cout << S[y] << endl;
    rep(q, 0, Q) printf("%lld\n", ans[q]);
}
0