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>
using namespace std;
vector<long long> hist(1001,0),col(1001,0);
long long n,m;
long long sum_area = 0;
void getMaxArea(){
stack<long long> s;long long tp;long long i=0;
long long la = 0;
while(i<m){
if(i>0&&col[i]!=col[i-1]){
while(s.empty()==false) {
tp = s.top();
s.pop();
sum_area+=hist[tp]*(s.empty()?i-la:i-s.top()-1);
}
la = i;
}
if(s.empty()||hist[s.top()]<=hist[i])s.push(i++);
else{
tp = s.top();s.pop();
sum_area+=hist[tp]*(s.empty()?i-la:i-s.top()-1);
}
}
while(!s.empty()){
tp = s.top();s.pop();
sum_area+= hist[tp] * (s.empty() ? i-la : i - s.top() - 1);
}
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>m;
long long arr[n][m];
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
cin>>arr[i][j];
}
}
for(int i = 0;i<n;i++){
if(i==0){
for(int j = 0;j<m;j++){col[j]=arr[i][j];hist[j]=1;}
}else{
for(int j = 0;j<m;j++){
col[j] = arr[i][j];
if(arr[i][j]==arr[i-1][j])hist[j]++;
else hist[j] = 1;
}
}
getMaxArea();
}
cout<<sum_area;
}
# | 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... |