#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e3+9;
char c;
int a[N][N];
int n,m,l[N],r[N],h[N][N],pf[N][N],e[N],res=0,dem=0,tmp=0;
map <int,int> save[N];
bool ban(int x,int y)
{
return (x>n||x<1||y>m||y<1||!a[x][y]);
}
int cal(int n,int m)
{
int fi=((1+m)*m)/2;
int last=n*fi;
return (last+fi)*n/2;
}
void solve(int h[])
{
stack <int> q;
h[0]=-1;
q.push(0);
for (int i=1;i<=m;i++)
{
while (h[i]<=h[q.top()]) q.pop();
l[i]=q.top()+1;
q.push(i);
}
while (!q.empty()) q.pop();
h[m+1]=-1;
q.push(m+1);
for (int i=m;i>=1;i--)
{
while (h[i]<=h[q.top()]) q.pop();
r[i]=q.top()-1;
q.push(i);
}
for (int i=1;i<=m;i++)
{
if (h[i]!=0) save[l[i]][h[i]]=max(save[l[i]][h[i]],r[i]);
}
set <int,greater<int> > s;
int back;
dem++;
for (int i=1;i<=m;i++)
{
if (h[i]==0) s.clear();
if (!save[i].size())
{
s.insert(h[i]);
continue;
}
auto it=s.upper_bound(save[i].begin()->first);
if (it==s.end()) e[i]=0;
else e[i]=*it;
s.insert(h[i]);
}
// if (dem==2) cout<<save[1].size()<<endl;
// cout<<e[m]<<endl;
for (int i=1;i<=m;i++)
{
back=e[i];
for (auto &xnew:save[i])
{
res+=pf[xnew.first][xnew.second-i+1]-pf[back][xnew.second-i+1];
back=xnew.first;
}
// if (dem==2) break;
}
//cout<<res-tmp<<endl;
tmp=res;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
// freopen("test.inp","r",stdin);
cin>>n>>m;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
cin>>c;
a[i][j]=(c=='.');
}
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
h[i][j]=((!a[i][j])?0:(h[i-1][j]+1));
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
pf[i][j]=pf[i][j-1]+cal(i,j);
// cout<<cal(2,1);
// cout<<pf[1][2]<<" "<<cal(1,2)<<endl;
// return 0;
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
save[j].clear();
solve(h[i]);
}
cout<<res;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
412 KB |
Output is correct |
2 |
Incorrect |
1 ms |
464 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
6184 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
6224 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
6224 KB |
Output is correct |
2 |
Incorrect |
18 ms |
6224 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
173 ms |
36112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
365 ms |
65660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
318 ms |
42436 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
61 ms |
28824 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
774 ms |
99008 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |