#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
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]);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
6392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
6472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
6472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
6516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
520 ms |
8456 KB |
Output is correct |
2 |
Correct |
541 ms |
10612 KB |
Output is correct |
3 |
Correct |
417 ms |
12580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
547 ms |
12664 KB |
Output is correct |
2 |
Correct |
476 ms |
14584 KB |
Output is correct |
3 |
Correct |
419 ms |
16660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
540 ms |
16660 KB |
Output is correct |
2 |
Correct |
436 ms |
18476 KB |
Output is correct |
3 |
Correct |
418 ms |
20460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
552 ms |
20548 KB |
Output is correct |
2 |
Correct |
518 ms |
22500 KB |
Output is correct |
3 |
Correct |
427 ms |
24296 KB |
Output is correct |