#include<bits/stdc++.h>
#define f1 first
#define s2 second
#define pb push_back
#define pob pop_back
#define int ll
#define ll long long
#define PII pair<int,int>
#define N 1003
using namespace std;
int n,m,ans;
int st[4*N],dizi[N],sagtosol[N];
int matris[N][N];
void build(int v, int tl,int tr){
if(tl==tr){
st[v]=tl;
}
else{
int tm=(tl+tr)/2;
build( v*2, tl, tm );
build( v*2+1, tm+1, tr );
if(dizi[st[v*2]]<dizi[st[v*2+1]]){
st[v]=st[v*2];
}
else{
st[v]=st[v*2+1];
}
}
}
int query( int v, int tl, int tr, int l, int r ){
if(l>r){
return 0;
}
if(l==tl && r==tr){
return st[v];
}
int tm=(tl+tr)/2;
int allah,belani,versin;
allah=query( v*2, tl, tm,l,min(tm,r) );
belani=query( v*2+1, tm+1, tr,max(l,tm+1),r );
if(dizi[allah]<dizi[belani]){
versin=allah;
}
else{
versin=belani;
}
return versin;
}
void calc(int ss,int se,int last,int kat){
if(ss>se)
return;
if(se==ss){
ans+=dizi[se];
ans-=last;
return;
}
int mn=query(1,1,m,ss,se);
if(mn==0)return;
for(int i=ss;i<=se;i++){
// ans-=last;
ans-=last*min(se-i+1,sagtosol[i]);
if(kat==3){
// printf("%lld\n",se-i+1 );
// printf("+%lld -%lld\n",dizi[mn]*min(se-i+1,sagtosol[i]),last*min(se-i+1,sagtosol[i]) );
}
ans+=dizi[mn]*min(se-i+1,sagtosol[i]);
}
calc(ss,mn-1,dizi[mn],kat);
calc(mn+1,se,dizi[mn],kat);
}
int32_t main(){
// freopen("a.gir","r",stdin);
// freopen("a.cik","w",stdout);
//
scanf("%lld %lld",&n,&m);
dizi[0]=INT_MAX;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%lld",&matris[i][j]);
}
}
for(int i=1;i<=m;i++){
dizi[i]=1;
}
sagtosol[m]=1;
for(int i=m-1;i>0;i--){
if(matris[1][i]==matris[1][i+1]){
sagtosol[i]=sagtosol[i+1]+1;
}
else{
sagtosol[i]=1;
}
}
build(1,1,m);
calc(1,m,0,1);
// printf("%lld\n",ans );
for(int j=2;j<=n;j++){
for(int i=1;i<=m;i++){
if(matris[j-1][i]==matris[j][i])
dizi[i]++;
else
dizi[i]=1;
}
sagtosol[m]=1;
for(int i=m-1;i>0;i--){
if(matris[j][i]==matris[j][i+1]){
sagtosol[i]=sagtosol[i+1]+1;
}
else{
sagtosol[i]=1;
}
}
/*for(int i=1;i<=m;i++){
printf("%lld ",dizi[i] );
}
printf("\n");*/
build(1,1,m);
calc(1,m,0,j);
// printf("%lld\n",ans );
}
printf("%lld",ans );
}
Compilation message
bob.cpp: In function 'int32_t main()':
bob.cpp:82:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld",&n,&m);
~~~~~^~~~~~~~~~~~~~~~~~~
bob.cpp:86:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",&matris[i][j]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
4344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
4584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
4344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
4472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
431 ms |
8604 KB |
Output is correct |
2 |
Correct |
706 ms |
10232 KB |
Output is correct |
3 |
Correct |
822 ms |
10232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
394 ms |
8312 KB |
Output is correct |
2 |
Correct |
488 ms |
10232 KB |
Output is correct |
3 |
Correct |
816 ms |
10232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
429 ms |
8312 KB |
Output is correct |
2 |
Correct |
818 ms |
10360 KB |
Output is correct |
3 |
Correct |
819 ms |
10156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
389 ms |
8572 KB |
Output is correct |
2 |
Correct |
508 ms |
10232 KB |
Output is correct |
3 |
Correct |
814 ms |
10232 KB |
Output is correct |