結果
問題 | No.440 2次元チワワ問題 |
ユーザー | pekempey |
提出日時 | 2016-10-29 04:49:41 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 69 ms / 5,000 ms |
コード長 | 1,892 bytes |
コンパイル時間 | 1,341 ms |
コンパイル使用メモリ | 161,316 KB |
実行使用メモリ | 13,568 KB |
最終ジャッジ日時 | 2024-05-03 08:40:24 |
合計ジャッジ時間 | 3,082 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 6 ms
13,312 KB |
testcase_01 | AC | 6 ms
13,568 KB |
testcase_02 | AC | 5 ms
13,568 KB |
testcase_03 | AC | 5 ms
13,440 KB |
testcase_04 | AC | 5 ms
13,440 KB |
testcase_05 | AC | 5 ms
13,568 KB |
testcase_06 | AC | 5 ms
13,416 KB |
testcase_07 | AC | 10 ms
13,568 KB |
testcase_08 | AC | 18 ms
13,568 KB |
testcase_09 | AC | 37 ms
13,440 KB |
testcase_10 | AC | 9 ms
13,416 KB |
testcase_11 | AC | 11 ms
13,540 KB |
testcase_12 | AC | 12 ms
13,540 KB |
testcase_13 | AC | 39 ms
13,568 KB |
testcase_14 | AC | 6 ms
13,568 KB |
testcase_15 | AC | 38 ms
13,544 KB |
testcase_16 | AC | 10 ms
13,568 KB |
testcase_17 | AC | 60 ms
13,312 KB |
testcase_18 | AC | 55 ms
13,568 KB |
testcase_19 | AC | 65 ms
13,416 KB |
testcase_20 | AC | 57 ms
13,568 KB |
testcase_21 | AC | 63 ms
13,312 KB |
testcase_22 | AC | 64 ms
13,440 KB |
testcase_23 | AC | 62 ms
13,440 KB |
testcase_24 | AC | 69 ms
13,540 KB |
testcase_25 | AC | 62 ms
13,440 KB |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:61:20: warning: format ‘%lld’ expects argument of type ‘long long int’, but argument 2 has type ‘int64_t’ {aka ‘long int’} [-Wformat=] 61 | printf("%lld\n", s1.solve(x1, x2, y1, y2) + s2.solve(y1, y2, x1, x2)); | ~~~^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | long long int int64_t {aka long int} | %ld main.cpp:52:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 52 | scanf("%s", buf); | ~~~~~^~~~~~~~~~~ main.cpp:60:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 60 | scanf("%d %d %d %d", &y1, &x1, &y2, &x2); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h> using namespace std; const int N = 500; struct Solver { struct DP { int c__, cw_, cww, _wc, wwc; }; int H, W; DP dp[N + 1][N + 2]; bitset<N + 1> s[N + 1]; // c:0 w:1 void build(int H, int W) { this->H = H; this->W = W; memset(dp, 0, sizeof(dp)); for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { dp[i][j].c__ = dp[i][j - 1].c__ + !s[i][j]; dp[i][j].cw_ = dp[i][j - 1].cw_ + (s[i][j] ? dp[i][j - 1].c__ : 0); dp[i][j].cww = dp[i][j - 1].cww + (s[i][j] ? dp[i][j - 1].cw_ : 0); } int c = 0; for (int j = W; j >= 1; j--) { dp[i][j]._wc = dp[i][j + 1]._wc + (s[i][j] ? c : 0); dp[i][j].wwc = dp[i][j + 1].wwc + (s[i][j] ? dp[i][j + 1]._wc : 0); c += !s[i][j]; } } } int64_t solve(int l, int r, int y1, int y2) { int64_t res = 0; for (int i = y1; i <= y2; i++) { int c = dp[i][r].c__ - dp[i][l - 1].c__; int w = (r - l + 1) - c; res += dp[i][r].cww - dp[i][l - 1].cww; res += dp[i][l].wwc - dp[i][r + 1].wwc; res -= (dp[i][W].c__ - c) * w * (w - 1) / 2; res -= w * (dp[i][l - 1].cw_ + dp[i][r + 1]._wc); } return res; } }; Solver s1, s2; int main() { int h, w, q; cin >> h >> w; for (int i = 0; i < h; i++) { char buf[512]; scanf("%s", buf); for (int j = 0; j < w; j++) s1.s[i + 1][j + 1] = s2.s[j + 1][i + 1] = buf[j] == 'w'; } s1.build(h, w); s2.build(w, h); cin >> q; for (int i = 0; i < q; i++) { int y1, x1, y2, x2; scanf("%d %d %d %d", &y1, &x1, &y2, &x2); printf("%lld\n", s1.solve(x1, x2, y1, y2) + s2.solve(y1, y2, x1, x2)); } }