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 <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 200005
#define M ll(1e9 + 7)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef short int si;
int h[2005][2005], lf[2005], rg[2005];
vector <int> g;
int main()
{
//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
string s[n];
for (int i = 0; i < n; i++) cin >> s[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
if (s[i][j] == '#') {h[i][j] = 0; continue;}
h[i][j] = 1;
if (i != 0) h[i][j] += h[i - 1][j];
}
ll ans = 0;
for (int i = 0; i < n; i++)
{
g.clear();
for (int j = 0; j < m; j++)
{
while (sz(g) && h[i][g.back()] >= h[i][j]) g.pop_back();
if (sz(g)) lf[j] = g.back(); else lf[j] = -1;
g.pb(j);
}
g.clear();
for (int j = m - 1; j >= 0; j--)
{
while (sz(g) && h[i][g.back()] > h[i][j]) g.pop_back();
if (sz(g)) rg[j] = g.back(); else rg[j] = m;
g.pb(j);
}
for (int j = 0; j < m; j++)
{
ll l = j - lf[j], r = rg[j] - j;
ll kl = r * (l * (l + 1)) / 2 + l * (r * (r + 1)) / 2 - r * l;
ll hr = h[i][j];
ans += kl * (hr * (hr + 1)) / 2;
}
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |