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 sze(x) (ll)x.size()
#define idx(x, a) get<x>(a)
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef long long ll;
const ll MAXN = 1e3 + 3;
const ll INF = 1e18 + 7;
ll a[MAXN][MAXN], h[MAXN][MAXN], l[MAXN][MAXN], cnt[MAXN][MAXN], m, n, res = 0;
int main()
{
ios_base::sync_with_stdio(0); cout.tie(0);
#ifdef OFFLINE
freopen("input.inp", "r", stdin);
#endif
cin >> m >> n;
for (ll i = 1; i <= m; ++i)
{
for (ll j = 1; j <= n; ++j) cin >> a[i][j];
h[i][0] = h[i][n + 1] = -INF;
}
fill(h[1] + 1, h[1] + n + 1, 1);
for (ll i = 2; i <= m; ++i)
for (ll j = 1; j <= n; ++j)
h[i][j] = (a[i][j] == a[i - 1][j] ? h[i - 1][j] + 1 : 1);
for (ll i = 1; i <= m; ++i)
{
stack<ll> posL, posR;
for (ll j = 1; j <= n; ++j)
{
if (j == 1 || a[i][j] != a[i][j - 1])
{
while (!posL.empty()) posL.pop();
posL.push(j - 1);
}
else while (h[i][j] <= h[i][posL.top()]) posL.pop();
l[i][j] = posL.top();
posL.push(j);
cnt[i][j] = (j - l[i][j]) * h[i][j] + cnt[i][l[i][j]] * (a[i][l[i][j]] == a[i][j]);
//cout << cnt[i][j] << ' ';
res += cnt[i][j];
}
//cout << endl;
}
cout << res;
}
# | 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... |