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 F first
#define S second
using namespace std;
typedef long long ll;
typedef double D;
const ll mod=998244353;
const ll inf=(1ll<<61);
const int MX=1009;
int n,m,a[MX][MX],dp[MX][MX];
ll k[MX];
pair<ll,int>seg[MX*6];
int DP(int x,int y){
int &ret=dp[x][y];if(ret!=-1)return ret;
if(x==0)return ret=1;
if(a[x-1][y]==a[x][y])return ret=DP(x-1,y)+1;
return ret=1;
}
void up(int node,int l,int r,int ind,int val){
if(l>ind||r<ind)return;
if(l==r){
seg[node]={val,ind};
return;
}
int mid=(l+r)/2;
up(node*2,l,mid,ind,val);
up(node*2+1,mid+1,r,ind,val);
if(seg[node*2].F<seg[node*2+1].F)seg[node]=seg[node*2];
else seg[node]=seg[node*2+1];
}
pair<ll,int> q(int node,int l,int r,int s,int e){
if(l>e||r<s)return {1e9+1,0};
if(l>=s&&r<=e)return seg[node];
int mid=(l+r)/2;
pair<ll,int> p1=q(node*2,l,mid,s,e);
pair<ll,int> p2=q(node*2+1,mid+1,r,s,e);
if(p1.F<p2.F)return p1;
return p2;
}
ll ans=0,ret;
void solve(ll l,ll r){
if(l>r)return;
pair<ll,int>p=q(1,0,m-1,l,r);
ans+=(r-l+1)*p.first;
ans+=(p.first)*abs(l-p.second)*abs(r-p.second);
solve(l,p.second-1);
solve(p.second+1,r);
}
int main(){
memset(dp,-1,sizeof(dp));
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&a[i][j]);
}
}
// puts("");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
DP(i,j);
}
}
for(int i=0;i<m;i++)up(1,0,m-1,i,1e9+1);
for(int i=0;i<n;i++){
int j=0,j1=0;
// cout<<i<<endl;
ret=0;
while(j1<m){
while(j1<m&&a[i][j1]==a[i][j])j1++;
for(int k=j;k<j1;k++)up(1,0,m-1,k,dp[i][k]);
solve(j,j1-1);
for(int k=j;k<j1;k++)up(1,0,m-1,k,1e9+1);
j=j1;
}
// cout<<ret<<endl;
// cout<<endl;
}
cout<<ans<<endl;
}
/*
5 3
2 2 2
2 2 1
1 1 1
2 1 2
1 2 1
*/
Compilation message (stderr)
bob.cpp: In function 'int main()':
bob.cpp:54:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&a[i][j]);
~~~~~^~~~~~~~~~~~~~~
# | 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... |