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>
using namespace std;
const int N=1e3 + 100;
const int mod=1e9 + 7;
#define int long long
const int inf=1e18;
#define pii pair<int, int>
#define f first
#define s second
#define mp make_pair
int grid[N][N], ans[N][N], len[N][N];
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int n, m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++) cin>>grid[i][j];
}
for(int i=1;i<=n;i++)
{
int prev=m, type=grid[i][m];
for(int j=m;j>=1;j--)
{
if(type!=grid[i][j])
{
prev=j;type=grid[i][j];
}
len[i][j]=prev - j+1;
}
}
int output=0;
for(int j=1;j<=m;j++)
{
stack<int> st;int curt=grid[1][j], prev=1;
for(int i=1;i<=n;i++)
{
while(!st.empty()&&((curt==grid[i][j]&&len[st.top()][j]>len[i][j])||curt!=grid[i][j]))
{
st.pop();
}
if(grid[i][j]!=curt)
{
curt=grid[i][j];prev=i;
}
if(!st.empty())
{
ans[i][j]=len[i][j]*(i-st.top()) + ans[st.top()][j];
}
else
{
ans[i][j]=len[i][j]*(i-prev + 1);
}
st.push(i);output+=ans[i][j];
}
}
cout<<output;
}
# | 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... |