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 ll long long
#define pb push_back
using namespace std;
const int maxn=1010;
const int inf=1e9+100;
int n,m;
int a[maxn][maxn],up[maxn][maxn],ans[maxn][maxn];
ll tot;
stack<pair<int,int>>s;
inline void clr(){
while(s.size())s.pop();
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
for(int col=1;col<=m;col++){
for(int row=1;row<=n;row++){
up[row][col]=1+up[row-1][col]*(a[row-1][col]==a[row][col]);
}
}
/*for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout<<up[i][j]<<' ';
}
cout<<endl;
}*/
for(int row=1;row<=n;row++){
clr();
int curclr=0;
int last=0;
for(int col=1;col<=m;col++){
if(a[row][col]!=curclr){
clr();
s.push({col,up[row][col]});
last=col-1;
curclr=a[row][col];
ans[row][col]=up[row][col];
continue;
}
while(s.size()&&s.top().second>=up[row][col])s.pop();
if(!s.size()){
ans[row][col]=(col-last)*up[row][col];
}
else{
ans[row][col]=ans[row][s.top().first]+(col-s.top().first)*up[row][col];
}
s.push({col,up[row][col]});
}
}
tot=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
tot+=ans[i][j];
}
}
cout<<tot<<endl;
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... |