#include<bits/stdc++.h>
#define lsb(x) (x &(-x))
using namespace std;
using pii = pair<int, int>;
using tp = tuple<int, int, int>;
const int N = 2005, K = N * N, cd[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int n, m, a[N][N], val[N][N], par[K], sz[K], mx[K], mn[K], fwk[N], dp[N][N];
string s;
vector<pii> pos[N];
void upd (int idx, int val) {
for (; idx < N; idx += lsb(idx)) fwk[idx] += val;
}
int qr (int idx) {
int res = 0;
for (; idx; idx -= lsb(idx)) res += fwk[idx];
return res;
}
void updran (int l, int r, int val) {
upd(l, val);
upd(r + 1, -val);
}
int fset (int x) {
return (par[x] == x ? x : par[x] = fset(par[x]));
}
void mg (int x, int y) {
if ((x = fset(x)) == (y = fset(y))) return;
sz[x] += sz[y];
mx[x] = max(mx[x], mx[y]);
mn[x] = min(mn[x], mn[y]);
par[y] = x;
}
int get (int x, int y) {
return (--x) * m + y;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n * m; i++) par[i] = i;
for (int i = 1; i <= n; i++) {
cin >> s; s = " " + s;
for (int j = 1; j <= m; j++) {
a[i][j] = -1;
if (s[j] != '.') {
a[i][j] = (s[j] - '0');
int z = get(i, j);
sz[z] = a[i][j];
mx[z] = mn[z] = j;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] == -1) continue;
for (int k = 0; k < 4; k++) {
int nx = i + cd[k][0], ny = j + cd[k][1];
if (nx <= 0 || nx > n || ny <= 0 || ny > m || a[nx][ny] == -1) continue;
mg(get(i, j), get(nx, ny));
}
}
}
vector<tp> c;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int z = get(i, j);
if (a[i][j] != -1 && fset(z) == z) c.emplace_back(mn[z], mx[z], sz[z]);
}
}
for (auto &[x, y, z]: c) {
pos[x].emplace_back(y, z);
}
for (int i = 1; i <= m; i++) {
for (auto &[x, y]: pos[i]) updran(i, x, y);
}
for (int i = 0; i <= m; i++) {
for (auto &[x, y]: pos[i]) updran(i, x, -y);
for (int j = i + 1; j <= m; j++) val[j][i] = qr(j);
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= i; j++) {
for (int k = 0; k < i; k++) {
dp[i][j] = max(dp[i][j], dp[k][j - 1] + val[i][k]);
}
}
}
for (int j = 1; j <= m; j++) {
int ans = 0;
for (int i = 1; i <= m; i++) ans = max(ans, dp[i][j]);
printf("%d\n", ans);
}
}
Compilation message (stderr)
nafta.cpp: In function 'int main()':
nafta.cpp:45:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |