#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define crep(i,x,n) for(int i=x;i<n;i++)
#define drep(i,n) for(int i=n-1;i>=0;i--)
#define vec(...) vector<__VA_ARGS__>
#define _3qplfh5 ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
void print(){
cout<<"\n";
}
template<class te,class ...ti>
void print(const te&v, const ti&...nv) {
cout<<v;
if(sizeof...(nv)){
cout<<" ";
print(nv...);
}
}
using pii=pair<int,int>;
using vi=vector<int>;
using vll=vector<long long>;
int tria(int x){
return (x*(x+1))/2;
}
void slv(){
int h,w;
cin>>h>>w;
vec(vi) a(h+1,vi(w));
rep(i,h){
rep(j,w){
char ch;
cin>>ch;
a[i][j]=(ch=='.');
}
}
h++;
vec(vi) dwn(h,vi(w));
drep(i,h){
rep(j,w){
if(!a[i][j]) dwn[i][j]=i;
else dwn[i][j]=dwn[i+1][j];
}
}
ll ans=0;
drep(i,h-1){
deque<tuple<int,int,int>> dq;
ll _fsum=0,_sum=0;
drep(j,w){
if(!a[i][j]){
dq.clear();
_fsum=0;
_sum=0;
}else{
int lo=dwn[i][j],cnt=1,sj=j;
while(sz(dq)){
auto [plo,pcnt,psj]=dq.front();
if(plo<lo){
break;
}
dq.pop_front();
cnt+=pcnt;
_sum-=tria(plo-i)*pcnt;
_sum+=tria(lo-i)*pcnt;
// if(i==0 and j==0) print(psj,'\n');
_fsum-=tria(plo-i)*(psj-pcnt*j);
_fsum+=tria(lo-i)*(psj-pcnt*j);
sj+=psj;
}
_sum+=tria(lo-i);
_fsum+=_sum;
ans+=_fsum;
dq.push_front({lo,cnt,sj});
}
// if(i==0) print(_fsum,"");
}
// print();
}
print(ans,'\n');
}
int main(){
_3qplfh5;
int t=1;
// cin>>t;
rep(cs,t)
slv();
//
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
312 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1032 KB |
Output is correct |
2 |
Correct |
4 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1136 KB |
Output is correct |
2 |
Correct |
4 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1100 KB |
Output is correct |
2 |
Correct |
4 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
9644 KB |
Output is correct |
2 |
Correct |
94 ms |
21432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
20872 KB |
Output is correct |
2 |
Correct |
148 ms |
32644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
13716 KB |
Output is correct |
2 |
Correct |
101 ms |
23492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
2764 KB |
Output is correct |
2 |
Correct |
126 ms |
26276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
138 ms |
32940 KB |
Output is correct |
2 |
Correct |
146 ms |
35704 KB |
Output is correct |