#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
#define ll long long
//#define int ll
#define pii pair<int, int>
#define f first
#define s second
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<": ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#define IOS()
#else
#define bug(...)
#define IOS() ios::sync_with_stdio(0)
#define endl '\n'
#endif // BALBIT
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)((x).size())
#define pb push_back
const int maxn = 2005;
queue<int> at[maxn];
inline ll C(ll x) {
return ((x * (2*x+1) * (x+1)) / 6 + (x * (x+1)) / 2)/2;
}
inline ll S(ll a, ll b) {
return (a+b) * (b-a+1) / 2;
}
vector<int> qh[maxn];
signed main(){
IOS();
bug(C(1), C(2), C(3));
bug(1,2);
int n,m; cin>>n>>m;
for (int i = 0; i<n; ++i) {
for (int j = 0; j<m; ++j) {
char c; cin>>c;
if (c == '#') {
at[j].push(i);
}
}
}
ll re = 0;
for (int i = 0; i<n; ++i) {
set<int> s; s.insert(-1); s.insert(m);
vector<pii> hi;
vector<int> H(m+1, -1), L(m+1,-1), R(m+1,-1);
vector<pii> stk = {{-1,-100}}; // position, height
for (int j = 0; j<n; ++j) qh[j].clear();
for (int j = 0; j<=m; ++j) {
if (!at[j].empty() && at[j].front() < i) at[j].pop();
if (!at[j].empty() || j == m) {
// hi.pb({at[j].front(), j});
if (j != m)
H[j] = at[j].front(), qh[H[j]] .pb (j);
else H[j] = -100;
while (stk.back().s > H[j]) {
R[stk.back().f] = j;
stk.pop_back();
}
L[j] = stk.back().f;
stk.pb({j, H[j]});
}
}
int prv = i;
ll now = C(m);
for (int j = i; j<n; ++j) {
for (int x : qh[j]) {
bug(x,L[x],R[x]);
now -= C(R[x] - L[x] - 1);
now += C(x-L[x]-1) + C(R[x]-x-1);
}
re += now * (j-i+1);
}
// sort(ALL(hi));
// for (pii & p : hi) {
// re += S(prv - i+1, p.f - i) * now;
// bug(re, S(prv - i+1, p.f - i) , now);
// auto r = s.upper_bound(p.s);
// auto l = prev(r);
// bug(*l,p.s,*r);
// now -= C(*r-*l-1);
// now += C(p.s - *l - 1) + C(*r - p.s - 1);
// prv = p.f;
// s.insert(p.s);
// }
//// bug(m, prv);
// re += S( prv - i +1 , n - i ) * now;
}
cout<<re<<endl;
}
/*
3 4
..#.
#...
...#
*/
Compilation message
strah.cpp: In function 'int main()':
strah.cpp:76:13: warning: unused variable 'prv' [-Wunused-variable]
76 | int prv = i;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1664 KB |
Output is correct |
2 |
Correct |
1 ms |
1664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1664 KB |
Output is correct |
2 |
Correct |
1 ms |
1664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1792 KB |
Output is correct |
2 |
Correct |
7 ms |
1792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
1792 KB |
Output is correct |
2 |
Correct |
8 ms |
1792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1792 KB |
Output is correct |
2 |
Correct |
9 ms |
1792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
3832 KB |
Output is correct |
2 |
Correct |
170 ms |
2808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
182 ms |
6020 KB |
Output is correct |
2 |
Correct |
249 ms |
3136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
1784 KB |
Output is correct |
2 |
Correct |
181 ms |
2808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
2048 KB |
Output is correct |
2 |
Correct |
176 ms |
1872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
195 ms |
1912 KB |
Output is correct |
2 |
Correct |
306 ms |
8312 KB |
Output is correct |