結果
| 問題 | No.3806 Crossword Key with Cropping (Easy) |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-11-17 14:20:59 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 55 ms / 2,000 ms |
| + 731µs | |
| コード長 | 2,715 bytes |
| 記録 | |
| コンパイル時間 | 2,371 ms |
| コンパイル使用メモリ | 351,888 KB |
| 実行使用メモリ | 9,868 KB |
| 最終ジャッジ日時 | 2026-11-17 14:21:11 |
| 合計ジャッジ時間 | 9,397 ms |
|
ジャッジサーバーID (参考情報) |
judge4_0 / judge5_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 53 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int h, w;
cin >> h >> w;
vector<string> S(h);
for (auto &s : S) cin >> s;
// 切り出しによって壁になる方向
const int UP = 1;
const int DOWN = 2;
const int LEFT = 4;
const int RIGHT = 8;
// maskで指定した方向を盤面外とみなす
auto key = [&](int x, int y, int mask) -> int {
if (S[x][y] == '#') return 0;
bool u = (mask & UP) || x == 0 || S[x-1][y] == '#';
bool d = (mask & DOWN) || x == h-1 || S[x+1][y] == '#';
bool l = (mask & LEFT) || y == 0 || S[x][y-1] == '#';
bool r = (mask & RIGHT) || y == w-1 || S[x][y+1] == '#';
// 横のカギ、縦のカギ、1マスカギ
return (l && !r) || (u && !d) || (u && d && l && r);
};
// 16通りの境界条件について二次元累積和
vector<vvi> P(16, vvi(h+1, vi(w+1, 0)));
for (int mask = 0; mask < 16; mask++) {
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
P[mask][i+1][j+1] =
P[mask][i][j+1]
+ P[mask][i+1][j]
- P[mask][i][j]
+ key(i, j, mask);
}
}
}
// 閉区間 [a,c] × [b,d] の和
auto sum = [&](int mask, int a, int b, int c, int d) -> int {
if (a > c || b > d) return 0;
return P[mask][c+1][d+1]
- P[mask][a][d+1]
- P[mask][c+1][b]
+ P[mask][a][b];
};
int q;
cin >> q;
while (q--) {
int a, b, c, d;
cin >> a >> b >> c >> d;
--a; --b; --c; --d;
// 高さ1・幅1の場合は両側を境界にする
int top = (a == c ? UP | DOWN : UP);
int left = (b == d ? LEFT | RIGHT : LEFT);
int ans = 0;
// 1. 内部
ans += sum(0, a+1, b+1, c-1, d-1);
// 2. 上端・下端(角を除く)
ans += sum(top, a, b+1, a, d-1);
if (a < c) {
ans += sum(DOWN, c, b+1, c, d-1);
}
// 3. 左端・右端(角を除く)
ans += sum(left, a+1, b, c-1, b);
if (b < d) {
ans += sum(RIGHT, a+1, d, c-1, d);
}
// 4. 四隅
ans += key(a, b, top | left);
if (b < d) {
ans += key(a, d, top | RIGHT);
}
if (a < c) {
ans += key(c, b, DOWN | left);
}
if (a < c && b < d) {
ans += key(c, d, DOWN | RIGHT);
}
cout << ans << '\n';
}
}