Submission #1134731

#TimeUsernameProblemLanguageResultExecution timeMemory
1134731AlmontherBob (COCI14_bob)C++20
0 / 120
72 ms31816 KiB
#include <bits/stdc++.h>

#define ll long long
#define co cout<<
using namespace std;
//stuff
ll n,m;
const ll maxn=1005;
ll arr[maxn][maxn]={};
ll l[maxn][maxn]={},r[maxn][maxn]={},u[maxn][maxn]={},board[maxn][maxn]={},h[maxn]={};
void solve(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>arr[i][j];
        }
    }
    ll ans=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(arr[i][j]==arr[i-1][j]) u[i][j]=u[i-1][j]+1;
            else u[i][j]=1;
            if(arr[i][j]==arr[i][j-1]&&u[i][j]<=u[i][j-1]) l[i][j]=l[i][j-1]+1;
            else l[i][j]=1;
        }
        for(int j=m;j>=1;j--){
            if(arr[i][j]==arr[i][j+1]&&u[i][j]<u[i][j+1]) r[i][j]=r[i][j+1]+1;
            else r[i][j]=1;
            ans+=u[i][j]*l[i][j]*r[i][j];
        }
    }
    co ans;
}
ll sub4()
{
    ll nRow=n;
    ll nCol=m;
    ll ans = 0;
    for(int i=1; i<=nRow; i++){
        for(int j=1; j<=nCol; j++){
            board[i][j]=arr[i][j];
            if(board[i][j] == board[i-1][j]) h[j]++;
            else h[j] = 1;
        }
        vector<int> L(nCol + 3, 0), R(nCol + 3, 0);
        stack<int> st;
        int tmp = 0;
        for(int j=1; j<=nCol; j++){
            while(!st.empty() && h[st.top()] >= h[j]) st.pop();
            if(st.empty()) L[j] = tmp;
            else L[j] = st.top();
            st.push(j);
            if(j == nCol || board[i][j] != board[i][j + 1]){
                while(!st.empty()) st.pop();
                tmp = j;
            }
        }

        tmp = nCol + 1;
        for(int j=nCol; j>=1; j--){
            while(!st.empty() && h[st.top()] > h[j]) st.pop();
            if(st.empty()) R[j] = tmp;
            else R[j] = st.top();
            st.push(j);
            if(j == 1 || board[i][j] != board[i][j-1]){
                while(!st.empty()) st.pop();
                tmp = j;
            }
        }

        for(int j=1; j<=nCol; j++){
            ans += 1ll * h[j] * (j - L[j]) * (R[j] - j);
        }
    }
    return ans;
}
void test(){
    n=rand()%(maxn-2)+1;
    m=rand()%(maxn-2)+1;
    for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) arr[i][j]=rand()+1;
    // if(sub4()!=solve()) co "NO\n";
    for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) arr[i][j]=l[i][j]=r[i][j]=u[i][j]=board[i][j]=h[i]=0;
}
int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int _=1;
    // cin>>_;
    while(_--) 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...