#include <bits/stdc++.h>
#define ll long long
#define co cout<<
using namespace std;
//stuff
ll n,m;
const ll maxn=1005;
ll arr[maxn][maxn]={};
ll l[maxn][maxn]={},r[maxn][maxn]={},u[maxn][maxn]={};
void solve(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>arr[i][j];
}
}
ll ans=0;
for(int i=1;i<=n;i++){
vector<ll>v;
ll last;
for(int j=1;j<=m;j++){
if(arr[i][j]==arr[i-1][j]) u[i][j]=u[i-1][j]+1;
else u[i][j]=1;
if(arr[i][j]!=arr[i][j-1]){
v.clear();
last=j-1;
}
while(v.size()&&u[i][j]<=u[i][v.back()]) v.pop_back();
if(!v.size()) l[i][j]=j-last;
else l[i][j]=j-v.back();
v.push_back(j);
}
for(int j=m;j>=1;j--){
if(arr[i][j]!=arr[i][j+1]){
v.clear();
last=j+1;
}
while(v.size()&&u[i][j]<u[i][v.back()]) v.pop_back();
if(!v.size()) r[i][j]=last-j;
else r[i][j]=v.back()-j;
v.push_back(j);
// co i<<' '<<j<<' '<<u[i][j]<<' '<<l[i][j]<<' '<<r[i][j]<<'\n';
ans+=u[i][j]*l[i][j]*r[i][j];
}
}
co ans;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int _=1;
// cin>>_;
while(_--) solve();
return 0;
}
# | 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... |