#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair < ll, ll >;
const ll N = 1e3 + 2;
ll a[N][N] = {0}, deesh[N][N] = {0};
int main() {
ll n, m, r, x, y, lo1, hi1, cnt, s, i, j, ans, t;
cin >> n >> m;
for (i = 1; i <= n; i ++) {
for (j = 1; j <= m; j ++) {
cin >> a[i][j];
deesh[i][j] = 1;
}
}
for (i = 1; i <= n; i ++) {
for (j= 1; j <= m; j ++) {
if ( a[i][j] == a[i - 1][j]) deesh[i][j] = deesh[i- 1][j] + 1;
}
}
// cout << "\n";
ans = 0;
for (i = 1; i <= n; i ++) {
s = 0;
stack < pll > st;
for (j = 1; j <= m; j ++) {
if ( j != 1 && a[i][j] != a[i][j - 1]) {
while(!st.empty()) st.pop();
s= 0;
}
r = deesh[i][j];
cnt = 1;
while ( !st.empty() && st.top().first >= r ) {
s -= (st.top().first * st.top().second);
cnt += st.top().second;
st.pop();
}
s = s + (cnt * r);
st.push(make_pair(r, cnt));
ans += s;
}
}
cout << ans << endl;
}
# | 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... |