Submission #66773845
Source Code Expand
// Original Python Code:
/*
import os
import sys
import numpy as np
def solve(h, w, field):
if h > w:
h, w = w, h
field = field.T
acc = np.zeros((h + 1, w + 1), dtype=np.int64)
for i in range(h):
for j in range(w):
acc[i + 1, j + 1] = acc[i + 1, j] + field[i, j]
for j in range(w):
for i in range(h):
acc[i + 1, j + 1] += acc[i, j + 1]
ans = 0
for i1 in range(h):
for i2 in range(i1 + 1, h + 1):
cnt = {}
for j in range(w + 1):
d = acc[i1, j] - acc[i2, j]
if d in cnt:
cnt[d] += 1
else:
cnt[d] = 1
for c in cnt.values():
ans += c * (c - 1) // 2
return ans
t = int(input())
for _ in range(t):
h, w = map(int, input().split())
field = []
getter = lambda c: '._#'.index(c) - 1
for _ in range(h):
field.append(list(map(getter, input())))
field = np.array(field, dtype=np.int64)
ans = solve(h, w, field)
print(ans)
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int getter(char c) {
if (c == '.') return -1;
if (c == '_') return 0;
if (c == '#') return 1;
return 0; // should not happen
}
ll solve(int h, int w, vector<vector<int>> field) {
if (h > w) {
swap(h, w);
vector<vector<int>> new_field(w, vector<int>(h));
for (int i = 0; i < h; ++i)
for (int j = 0; j < w; ++j)
new_field[j][i] = field[i][j];
field = new_field;
}
vector<vector<ll>> acc(h + 1, vector<ll>(w + 1, 0));
for (int i = 0; i < h; ++i)
for (int j = 0; j < w; ++j)
acc[i + 1][j + 1] = acc[i + 1][j] + field[i][j];
for (int j = 0; j < w; ++j)
for (int i = 0; i < h; ++i)
acc[i + 1][j + 1] += acc[i][j + 1];
ll ans = 0;
for (int i1 = 0; i1 < h; ++i1) {
for (int i2 = i1 + 1; i2 <= h; ++i2) {
unordered_map<ll, int> cnt;
for (int j = 0; j <= w; ++j) {
ll d = acc[i1][j] - acc[i2][j];
cnt[d]++;
}
for (auto& [key, c] : cnt) {
ans += 1LL * c * (c - 1) / 2;
}
}
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int h, w;
cin >> h >> w;
vector<vector<int>> field(h, vector<int>(w));
for (int i = 0; i < h; ++i) {
string line;
cin >> line;
for (int j = 0; j < w; ++j) {
field[i][j] = getter(line[j]);
}
}
cout << solve(h, w, field) << '\n';
}
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | F - Balanced Rectangles |
| User | ikatakos |
| Language | C++ 20 (gcc 12.2) |
| Score | 0 |
| Code Size | 2906 Byte |
| Status | WA |
| Exec Time | 3311 ms |
| Memory | 52460 KiB |
Judge Result
| Set Name | Sample | All | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 0 / 525 | ||||||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | sample_01.txt |
| All | hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, sample_01.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt, test_37.txt, test_38.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| hand_01.txt | WA | 60 ms | 52460 KiB |
| hand_02.txt | AC | 24 ms | 22156 KiB |
| hand_03.txt | WA | 64 ms | 52328 KiB |
| hand_04.txt | AC | 9 ms | 12540 KiB |
| sample_01.txt | WA | 1 ms | 3536 KiB |
| test_01.txt | AC | 1 ms | 3632 KiB |
| test_02.txt | WA | 1 ms | 3564 KiB |
| test_03.txt | WA | 1 ms | 3512 KiB |
| test_04.txt | WA | 1 ms | 3508 KiB |
| test_05.txt | WA | 1 ms | 3452 KiB |
| test_06.txt | WA | 1 ms | 3440 KiB |
| test_07.txt | WA | 1 ms | 3452 KiB |
| test_08.txt | WA | 2 ms | 3572 KiB |
| test_09.txt | WA | 3 ms | 3516 KiB |
| test_10.txt | WA | 6 ms | 3508 KiB |
| test_11.txt | WA | 6 ms | 3508 KiB |
| test_12.txt | WA | 33 ms | 3376 KiB |
| test_13.txt | WA | 22 ms | 3516 KiB |
| test_14.txt | AC | 20 ms | 15092 KiB |
| test_15.txt | WA | 36 ms | 27616 KiB |
| test_16.txt | AC | 2869 ms | 8380 KiB |
| test_17.txt | WA | 2271 ms | 8336 KiB |
| test_18.txt | WA | 2852 ms | 8304 KiB |
| test_19.txt | AC | 2162 ms | 8296 KiB |
| test_20.txt | WA | 34 ms | 3456 KiB |
| test_21.txt | AC | 1979 ms | 8160 KiB |
| test_22.txt | WA | 1625 ms | 7252 KiB |
| test_23.txt | AC | 2360 ms | 8308 KiB |
| test_24.txt | TLE | 3311 ms | 8052 KiB |
| test_25.txt | WA | 2525 ms | 6384 KiB |
| test_26.txt | WA | 1723 ms | 4240 KiB |
| test_27.txt | AC | 474 ms | 8276 KiB |
| test_28.txt | WA | 323 ms | 5568 KiB |
| test_29.txt | WA | 663 ms | 6516 KiB |
| test_30.txt | WA | 1972 ms | 8328 KiB |
| test_31.txt | TLE | 3132 ms | 7800 KiB |
| test_32.txt | WA | 1655 ms | 5844 KiB |
| test_33.txt | AC | 264 ms | 7932 KiB |
| test_34.txt | WA | 209 ms | 7912 KiB |
| test_35.txt | AC | 268 ms | 7896 KiB |
| test_36.txt | WA | 182 ms | 7936 KiB |
| test_37.txt | AC | 322 ms | 7912 KiB |
| test_38.txt | WA | 173 ms | 8436 KiB |