Submission #1267220

#TimeUsernameProblemLanguageResultExecution timeMemory
1267220ngunguoi45Bob (COCI14_bob)C++17
120 / 120
129 ms18236 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int maxn = (int)1e3+5;
int n, m;
int a[maxn][maxn];

namespace sub1 {
    ll ans = 0;
    int prv = -1;
    bool check () {
        return (n <= 50 && m <= 50);
    }
    void solve () {
        for (int top = 1; top <= n; top++) {
            for (int bot = top; bot <= n; bot++) {
                for (int lft = 1; lft <= m; lft++) {
                    prv = -1;
                    for (int rht = lft; rht <= m; rht++) {
                        bool ok = 1;
                        for (int s = top; s <= bot; s++) {
                            if (a[s][rht] != prv && prv != -1) {
                                ok = 0;
                                break;
                            }
                            else if (prv == -1) {
                                prv = a[s][rht];
                            }
                        }
                        ans += ok;
                        if (!ok) break;
                    }
                }
            }
        }
        cout << ans << "\n";
    }
}

namespace sub2 {
    ll ans = 0;
    bitset<505> valid[505][505];
    bool check () {
        return (n <= 500 && m <= 500);
    }
    void solve () {
        for (int col = 1; col <= m; col++) {
            for (int top = 1; top <= n; top++) {
                valid[col][top][top] = 1;
                for (int bot = top+1; bot <= n; bot++) {
                    if (a[bot][col] != a[bot-1][col]) break;
                    valid[col][top][bot] = 1;
                }
            }
        }
        for (int top = 1; top <= n; top++) {
            for (int bot = top; bot <= n; bot++) {
                ll tmp = 0;
                for (int rht = 1; rht <= m; rht++) {
                    if (!valid[rht][top][bot]) {
                        tmp = 0;
                    }
                    else if (valid[rht][top][bot] && rht > 1 && a[bot][rht] != a[bot][rht-1]) {
                        tmp = 1;
                        ans += tmp;
                    }
                    else {
                        tmp++;
                        ans += tmp;
                    }
                }
            }
        }
        cout << ans << "\n";
    }
}

namespace sub3 {
    stack<int> st;
    int h[maxn][maxn];
    int lft[maxn][maxn];
    int rht[maxn][maxn];
    ll ans = 0;

    ll cal (int row, int l, int r) {
        ll sum = 0;
        st = stack<int>();
        for (int i = l;i <= r; i++) {
            while (st.size() && h[row][st.top()] >= h[row][i]) st.pop();
            lft[row][i] = (st.size() ? st.top() + 1 : l);
            st.push(i);
        }
        st = stack<int>();
        for (int i = r;i >= l; i--) {
            while (st.size() && h[row][st.top()] > h[row][i]) st.pop();
            rht[row][i] = (st.size() ? st.top() - 1 : r);
            st.push(i);
        }
        for (int i = l;i <= r; i++) {
            sum += 1LL * (i-lft[row][i] + 1) * (rht[row][i] - i + 1) * h[row][i];
        }
        return sum;
    }
    void solve () {
        for (int row = 1;row <= n; row++) {
            for (int col = 1; col <= m; col++) {
                if (a[row][col] != a[row-1][col]) {
                    h[row][col] = 1;
                }
                else {
                    h[row][col] = h[row-1][col] + 1;
                }
            }
        }
        for (int row = 1; row <= n; row++) {
            int fst = 1;
            for (int col = 2; col <= m; col++) {
                if (a[row][fst] != a[row][col]) {
                    ans += cal(row, fst, col-1);
                    fst = col;
                }
            }
            ans += cal(row, fst, m);
        }
        cout << ans << "\n";
    }
}

void read_and_prep() {
    cin >> n >> m;
    for (int i = 1;i <= n; i++) {
        for (int j = 1;j <= m; j++) cin >> a[i][j];
    }
}

int main () {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    read_and_prep();
    if (sub1::check()) sub1::solve();
    else if (sub2::check()) sub2::solve();
    else sub3::solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...