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
#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 (stderr)
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]);
^
# | 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... |