This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <cstdio>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
int n, m;
int nu[3005][3005];
char S[3005][3005];
long long d[3005];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%s", S[i] + 1);
for (int i = 1; i <= n; i++) {
for (int j = m; j >= 1; j--) {
nu[i][j] = S[i][j] == 'B' ? 0 : nu[i][j+1] + 1;
}
}
long long res = 0;
for (int i = 1; i <= m; i++) {
stack < pair <int, int> > s;
for (int j = 1; j <= n; j++) {
while (s.size() > 0 && s.top().first >= nu[j][i]) s.pop();
long long v = 0;
if (s.size() > 0) {
v = d[s.top().second];
d[j] = v + (long long)(j - s.top().second)*(nu[j][i]);
}
else
d[j] = (long long)(nu[j][i]) * (j);
s.push(make_pair(nu[j][i], j));
res += d[j];
}
}
printf("%lld", res);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |