Submission #1215982

#TimeUsernameProblemLanguageResultExecution timeMemory
1215982Robert_juniorBob (COCI14_bob)C++20
84 / 120
1097 ms8264 KiB
#include<bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ins insert
#define pb push_back
#define F first
#define S second
const int N = 1e6 + 7, M = 5e5 + 7;
const int mod = 1e9 + 7;
int h[1010][1010], a[1010][1010], R[1010], dp[1010];
void solve(){
    int n, m;
    cin>>n>>m;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            cin>>a[i][j];
        }
    }
    long long ans = 0;
    for(int i = n; i >= 1; i--){
        for(int j = m; j >= 1; j--){
            if(a[i + 1][j] == a[i][j]){
                h[i][j] = (h[i + 1][j] + 1);
            }
            else{
                h[i][j] = 1;
            }
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            int mn = h[i][j];
            int k = j; 
            while(k <= n && a[i][j] == a[i][k]){
                k++;
            }
            k--;
            stack<int>s;
            dp[k + 1] = 0;
            for(int l = k; l >= j; l--){
                while(s.size() && h[i][s.top()] >= h[i][l]) s.pop();
                if(!s.size()) R[l] = k + 1;
                else R[l] = s.top();
                s.push(l);
                dp[l] = dp[R[l]] + (R[l] - l) * h[i][l];
                ans += dp[l];
            }
            j = k;
        }
    }
    cout<<ans<<'\n';
}
signed main(){
    ios_base :: sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    //cin>>t; 
    for(int i = 1; i <= t; i++){
        //cout<<"Case "<<i<<": ";
        solve();
    }
}
#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...