結果

問題 No.440 2次元チワワ問題
ユーザー parukiparuki
提出日時 2016-11-03 22:10:58
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 810 ms / 5,000 ms
コード長 2,219 bytes
コンパイル時間 1,850 ms
コンパイル使用メモリ 181,220 KB
実行使用メモリ 12,160 KB
最終ジャッジ日時 2023-08-16 13:21:52
合計ジャッジ時間 7,574 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
11,360 KB
testcase_01 AC 7 ms
11,380 KB
testcase_02 AC 7 ms
11,404 KB
testcase_03 AC 6 ms
11,356 KB
testcase_04 AC 6 ms
11,372 KB
testcase_05 AC 6 ms
11,424 KB
testcase_06 AC 6 ms
11,344 KB
testcase_07 AC 12 ms
11,532 KB
testcase_08 AC 23 ms
11,636 KB
testcase_09 AC 59 ms
11,796 KB
testcase_10 AC 11 ms
11,508 KB
testcase_11 AC 13 ms
11,740 KB
testcase_12 AC 16 ms
11,620 KB
testcase_13 AC 57 ms
11,768 KB
testcase_14 AC 8 ms
11,404 KB
testcase_15 AC 54 ms
12,160 KB
testcase_16 AC 11 ms
11,420 KB
testcase_17 AC 102 ms
12,020 KB
testcase_18 AC 98 ms
11,952 KB
testcase_19 AC 99 ms
11,980 KB
testcase_20 AC 97 ms
11,968 KB
testcase_21 AC 810 ms
11,972 KB
testcase_22 AC 749 ms
11,972 KB
testcase_23 AC 748 ms
11,948 KB
testcase_24 AC 752 ms
11,976 KB
testcase_25 AC 757 ms
12,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

int H, W, Q;
vector<string> S, T;
typedef tuple<int, int, int, int> Query;
vector<Query> q, qs[501];
int sy, sx, ey, ex;
ll ans[10001], c[501][501], w[501][501], cw[501][501], cww[501][501];

void calc() {
    rep(ii, 2) {
        MEM(c, 0); MEM(w, 0); MEM(cw, 0); MEM(cww, 0);
        rep(y, H) {
            rep(x, W) {
                c[y][x + 1] = c[y][x] + (S[y][x] == 'c');
                w[y][x + 1] = w[y][x] + (S[y][x] == 'w');
                cw[y][x + 1] = cw[y][x] + (S[y][x] == 'w' ? c[y][x] : 0);
                cww[y][x + 1] = cww[y][x] + (S[y][x] == 'w' ? cw[y][x] : 0);
            }
        }
        rep(i, Q) {
            tie(sy, sx, ey, ex) = q[i];
            FOR(y, sy, ey) {
                ll wa = w[y][ex] - w[y][sx];
                ll ww = wa*(wa - 1) / 2;
                ans[i] += cww[y][ex] - cww[y][sx] - c[y][sx] * ww - cw[y][sx] * wa;
            }
        }
        rep(y, H)reverse(all(S[y]));
        rep(i, Q) {
            tie(sy, sx, ey, ex) = q[i];
            int t = W - sx;
            sx = W - ex;
            ex = t;
            q[i] = tie(sy, sx, ey, ex);
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> H >> W;
    S.resize(H);
    rep(y, H)cin >> S[y];
    cin >> Q;
    q.resize(Q);
    rep(i, Q) {
        cin >> sy >> sx >> ey >> ex;
        --sy; --sx;
        q[i] = make_tuple(sy, sx, ey, ex);
    }

    T = vector<string>(W, string(H, ' '));
    rep(y, H)rep(x, W)T[x][y] = S[y][x];
    calc();
    S = T;
    swap(W, H);
    rep(i, Q) {
        tie(sy, sx, ey, ex) = q[i];
        swap(sy, sx); swap(ey, ex);
        q[i] = tie(sy, sx, ey, ex);
    }
    calc();

    rep(i, Q)printf("%lld\n", ans[i]);
}
0