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<bits/stdc++.h>
#define fi first
#define se second
#define pitem item*
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=1e6+10;
const int SS=1<<19;
const int INFi=2e9;
const ll INFl=1e13;
const ll mod2=998244353;
const ll mod=1e9+7;
const ll mod3=1000696969;
const ll p=70032301;
const ull p2=913;
const int L=20;
int t[2003][2003],n,m,fau[N],rep[N],pierw[N],dp[N];
vi g[N];
int Find(int a){
if(fau[a]==a) return a;
return fau[a]=Find(fau[a]);
}
void Union(int a,int b){
fau[Find(a)]=Find(fau[b]);
}
int wzo(int dl){
return dp[dl];
}
ll single(int i){
for(int j=1;j<=m;j++){
if(!t[i][j]) pierw[j]=-1;
else{
if(!t[i-1][j]){
int curr=i;
while(t[curr][j]) curr++;
pierw[j]=curr-1;
}
}
if(pierw[j]!=-1) g[pierw[j]].push_back(j);
}
ll curr=0,res=0;
for(int j=n;j>=i;j--){
for(auto u:g[j]){
fau[u]=u;
int dl=1;
if(fau[u-1]) dl+=rep[Find(u-1)],curr-=wzo(rep[Find(u-1)]),Union(u,u-1);
if(fau[u+1]) dl+=rep[Find(u+1)],curr-=wzo(rep[Find(u+1)]),Union(u,u+1);
rep[Find(u)]=dl;
curr+=(ll)wzo(dl);
}
res+=curr*(ll)(j-i+1);
}
for(int j=i;j<=n;j++) g[j].clear();
for(int j=1;j<=m;j++) fau[j]=0,rep[j]=0;
return res;
}
void solve(){
cin>>n>>m;
for(int i=1;i<=m;i++) dp[i]=dp[i-1]+((i*(i+1))>>1);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
char a;
cin>>a;
t[i][j]=(a=='.');
}
}
ll res=0;
for(int i=1;i<=n;i++) res+=(ll)single(i);
cout<<res<<"\n";
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
int tt=1;
while(tt--) solve();
}
# | 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... |