#include <bits/stdc++.h>
#define eb emplace_back
#define printv(a, b) {\
bool ps = false;\
for(auto pv : a){\
if(ps) b << ' '; ps = true;\
b << pv;\
}\
b << "\n";\
}
#define iter(a) a.begin(), a.end()
#define lsort(a) sort(iter(a))
#define mp make_pair
#define F first
#define S second
using namespace std;
typedef long long ll;
using pii = pair<int, int>;
struct qq{
int l, lh, h;
qq(int l = 0, int lh = 0, int h = 0): l(l), lh(lh), h(h) {}
};
ostream& operator<<(ostream& o, qq now){
return o << '(' << now.l << ',' << now.lh << ',' << now.h << ')';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m + 1));
for(int i = 0; i < n; i++){
string s;
cin >> s;
for(int j = 0; j < m; j++){
if(s[j] == '#') continue;
a[i][j] = 1;
if(i && a[i - 1][j]) a[i][j] += a[i - 1][j];
}
}
//for(int i = 0; i < n; i++) printv(a[i], cerr);
vector<ll> f(m + 1);
for(ll i = 1; i <= m; i++){
f[i] = f[i - 1] + i * (i + 1) / 2;
}
ll ans = 0;
for(int i = 0; i < n; i++){
stack<qq> st;
for(int j = 0; j <= m; j++){
int l = j;
while(!st.empty() && st.top().h >= a[i][j]){
qq now = st.top();
st.pop();
l = now.l;
ll len = j - now.l;
ll th = max(now.lh, a[i][j]);
ll h = now.h;
ll tans = f[len] * (th + 1 + h) * (h - th) / 2;
//cerr << i << " " << j << " " << len << " "
// << h << " " << now << " " << tans << "\n";
ans += tans;
}
int lh = st.empty() ? 0 : st.top().h;
st.push(qq(l, lh, a[i][j]));
}
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
748 KB |
Output is correct |
2 |
Correct |
3 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
748 KB |
Output is correct |
2 |
Correct |
3 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
748 KB |
Output is correct |
2 |
Correct |
4 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
5484 KB |
Output is correct |
2 |
Correct |
67 ms |
12108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
12524 KB |
Output is correct |
2 |
Correct |
93 ms |
18412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
8300 KB |
Output is correct |
2 |
Correct |
69 ms |
13164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1772 KB |
Output is correct |
2 |
Correct |
74 ms |
14828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
20076 KB |
Output is correct |
2 |
Correct |
130 ms |
20076 KB |
Output is correct |