#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
cerr<<vars<<" = ";
string delim="";
(...,(cerr<<delim<<values,delim=", "));
cerr<<"\n";
}
#else
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {}
#endif
#define pb push_back
#define sz(x) (int)(x.size())
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<typename T> inline void maxa(T& a,T b){a=max(a,b);}
template<typename T> inline void mina(T& a,T b){a=min(a,b);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int mxN=1e3+5; //make sure this is right
const int mod=1e9+7;
ll a[mxN][mxN],up[mxN][mxN],lft[mxN][mxN],res[mxN][mxN];
int main(){
cin.sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int n,m; cin>>n>>m;
ll ans=0;
for(int i=1;i<=n;i++){
stack<int> stk;
for(int j=1;j<=m;j++){
cin>>a[i][j];
if(a[i][j]==a[i-1][j])
up[i][j]=up[i-1][j]+1;
else
up[i][j]=1;
if(a[i][j]==a[i][j-1])
lft[i][j]=lft[i][j-1]+1;
else{
while(sz(stk))
stk.pop();
lft[i][j]=1;
}
while(sz(stk) && up[i][stk.top()]>=up[i][j])
stk.pop();
if(stk.empty()){
res[i][j]=lft[i][j]*up[i][j];
} else{
res[i][j]=(j-stk.top())*up[i][j]+res[i][stk.top()];
}
ans+=res[i][j];
stk.push(j);
}
}
cout<<ans<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
16040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
15956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
15968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
16028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
31708 KB |
Output is correct |
2 |
Correct |
96 ms |
33732 KB |
Output is correct |
3 |
Correct |
93 ms |
33636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
162 ms |
31756 KB |
Output is correct |
2 |
Correct |
98 ms |
33648 KB |
Output is correct |
3 |
Correct |
92 ms |
33604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
151 ms |
31744 KB |
Output is correct |
2 |
Correct |
94 ms |
33592 KB |
Output is correct |
3 |
Correct |
94 ms |
33680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
31872 KB |
Output is correct |
2 |
Correct |
100 ms |
33732 KB |
Output is correct |
3 |
Correct |
92 ms |
33532 KB |
Output is correct |