#include <bits/stdc++.h>
#define F first
#define S second
#define P push
#define pb push_back
#define MEM(dp,i) memset(dp,i,sizeof(dp))
#define W while
#define R return
#define C continue
#define SI size()
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define pii pair<int,int>
#define SF(x) scanf("%Id",&x)
#define SF2(x,y) scanf("%Id%Id",&x,&y)
#define SF3(x,y,z) scanf("%I64d%I64d%I64d",&x,&y,&z)
#define SF4(x,y,z,o) scanf("%I64d%I64d%I64d%I64d",&x,&y,&z,&o)
#define all(v) v.begin(),v.end()
using namespace std;
const long long INF = 1e9+1;
const long long MOD = 1e9+7;
const int MX=200015;
int a[1005][1005];
int dp[1005][1005];
int DP(int x,int y){
int &ret=dp[x][y];
if(ret!=-1)R ret;
ret=1;
if(a[x][y]==a[x][y+1])ret+=DP(x,y+1);
R ret;
}
pii seg[4005];
void u(int node,int l,int r,int ind,int val){
if(ind<l||r<ind)R;
if(l==r){
seg[node]={val,l};
R;
}
int mid=(l+r)/2;
u(node*2,l,mid,ind,val);
u(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];
}
}
pii q(int node,int l,int r,int s,int e){
if(r<s||e<l)R {INF,0};
if(s<=l&&r<=e){
R seg[node];
}
int mid=(l+r)/2;
pii p1,p2;
p1=q(node*2,l,mid,s,e);
p2=q(node*2+1,mid+1,r,s,e);
if(p1.F<p2.F)R p1;
else R p2;
}
vector<int> v;
vector<pii> vec;
int n,m;
ll pro(){
ll ret=0;
for(int i=0;i<v.size();i++){
u(1,0,n-1,i,v[i]);
}
int x,y;
pii p;
vec.pb({0,v.size()-1});
pii pp;
W(vec.size()){
p=vec.back();
vec.pop_back();
if(p.F>p.S)continue;
pp=q(1,0,n-1,p.F,p.S);
x=pp.S;
y=pp.F;
// cout<<p.F<<" "<<x<<" "<<p.S<<" "<<y<<endl;
ret+=y*(x-p.F)*(p.S-x);
ret+=y*(p.S-p.F+1);
vec.pb({p.F,x-1});
vec.pb({x+1,p.S});
}
// cout<<ret<<endl;
R ret;
}
int main(){
MEM(dp,-1);
cin>>n>>m;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
scanf("%d",&a[i][j]);
ll res=0;
for(int j=0;j<m;j++){
int i=0;
W(i<n){
v.pb(DP(i,j));
W(a[i][j]==a[i+1][j]){
i++;
v.pb(DP(i,j));
}
res+=pro();
v.clear();
i++;
}
}
cout<<res;
}
Compilation message
bob.cpp: In function 'long long int pro()':
bob.cpp:69:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v.size();i++){
^
bob.cpp: In function 'int main()':
bob.cpp:97:33: 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 |
5 ms |
4472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
6948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
7916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
9156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
10276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
438 ms |
19196 KB |
Output is correct |
2 |
Correct |
437 ms |
21080 KB |
Output is correct |
3 |
Correct |
360 ms |
23140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
475 ms |
32780 KB |
Output is correct |
2 |
Correct |
415 ms |
34812 KB |
Output is correct |
3 |
Correct |
364 ms |
36688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
479 ms |
46312 KB |
Output is correct |
2 |
Correct |
344 ms |
48360 KB |
Output is correct |
3 |
Correct |
370 ms |
50160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
502 ms |
60012 KB |
Output is correct |
2 |
Correct |
380 ms |
61820 KB |
Output is correct |
3 |
Correct |
363 ms |
63828 KB |
Output is correct |