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 <iostream>
#include <sstream>
#include <queue>
#include <stack>
#include <vector>
#include <cstring>
#include <cmath>
#include <map>
#include <unordered_map>
#include <set>
#include <algorithm>
#include <iomanip>
#define F first
#define S second
#define PB push_back
#define PF push_front
#define MP make_pair
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int maxn = 1e5 + 37;
int n, m;
int a[maxn];
int l[maxn], r[maxn];
ll sum (ll fi, ll se) {
return se * (se + 1) / 2 - fi * (fi + 1) / 2;
}
ll get () {
stack <int> s;
for (int i = m - 1; i >= 0; i--) {
while (!s.empty() and a[s.top()] >= a[i])
s.pop();
if (!s.empty())
r[i] = s.top();
else
r[i] = m;
s.push (i);
}
while (!s.empty())
s.pop();
for (int i = 0; i < m; i++) {
while (!s.empty() and a[s.top()] > a[i])
s.pop();
if (!s.empty())
l[i] = s.top();
else
l[i] = -1;
s.push (i);
}
ll ret = 0;
for (int i = 0; i < m; i++) {
ll cnt = sum (i - 1, r[i] - 1) * (i - l[i]) + (r[i] - i) * (i - l[i]);
cnt -= sum (l[i], i) * (r[i] - i);
ret += a[i] * (a[i] + 1) * cnt / 2;
}
return ret;
}
string s[maxn];
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> s[i];
ll ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] == '#')
a[j] = 0;
else
a[j] ++;
}
ans += get ();
}
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... |