#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<int,int>pii;
typedef pair<pii,int>pi2;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
void solve(){
int n,m;
cin >> n >> m;
int arr[n][m];
for(int x=0;x<n;x++){
for(int y=0;y<m;y++){
cin >> arr[x][y];
}
}
int down[n][m];
for(int y=0;y<m;y++){
down[n-1][y]=n-1;
for(int x=n-2;x>=0;x--){
down[x][y]=x;
if(arr[x][y]==arr[x+1][y]){
down[x][y]=max(down[x][y],down[x+1][y]);
}
}
}
int ans=0;
for(int x=0;x<n;x++){
stack<pii>stk;
int color=-1;
int counter=0;
for(int y=m-1;y>=0;y--){
if(arr[x][y]!=color){
while(!stk.empty()) stk.pop();
counter=0;
}
color=arr[x][y];
int cnt=1;
int dep=down[x][y]-x+1;
while(!stk.empty()&&stk.top().first>=dep){
cnt+=stk.top().second;
counter-=stk.top().first*stk.top().second;
stk.pop();
}
stk.push({dep,cnt});
counter+=dep*cnt;
ans+=counter;
}
}
cout << ans;
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
//freopen("in.txt","r",stdin);
//freopen("in.txt","w",stdout);
int t=1;
//cin >> t;
while(t--){
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... |