Submission #217678

# Submission time Handle Problem Language Result Execution time Memory
217678 2020-03-30T12:11:21 Z mayhoubsaleh Bob (COCI14_bob) C++14
120 / 120
654 ms 22008 KB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back

using namespace std;
const int maxn=1010;
const int inf=1e9+100;
int n,m;
int a[maxn][maxn],up[maxn][maxn],ans[maxn][maxn];
ll tot;
stack<pair<int,int>>s;

inline void clr(){
    while(s.size())s.pop();
}

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

    for(int col=1;col<=m;col++){
        for(int row=1;row<=n;row++){
            up[row][col]=1+up[row-1][col]*(a[row-1][col]==a[row][col]);
        }
    }

    /*for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cout<<up[i][j]<<' ';

        }
        cout<<endl;
    }*/

    for(int row=1;row<=n;row++){
        clr();
        int curclr=0;
        int last=0;
        for(int col=1;col<=m;col++){
            if(a[row][col]!=curclr){
                clr();
                s.push({col,up[row][col]});
                last=col-1;
                curclr=a[row][col];
                ans[row][col]=up[row][col];
                continue;
            }
            while(s.size()&&s.top().second>=up[row][col])s.pop();
            if(!s.size()){
                ans[row][col]=(col-last)*up[row][col];
            }
            else{
                ans[row][col]=ans[row][s.top().first]+(col-s.top().first)*up[row][col];
            }
            s.push({col,up[row][col]});
        }
    }

    tot=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            tot+=ans[i][j];
        }
    }
    cout<<tot<<endl;
    return 0;
}


# Verdict Execution time Memory Grader output
1 Correct 5 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 6904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 7164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 7544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 7420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 495 ms 18936 KB Output is correct
2 Correct 237 ms 14200 KB Output is correct
3 Correct 240 ms 14200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 654 ms 22008 KB Output is correct
2 Correct 239 ms 14200 KB Output is correct
3 Correct 240 ms 14200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 627 ms 21752 KB Output is correct
2 Correct 244 ms 14200 KB Output is correct
3 Correct 233 ms 14200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 636 ms 21880 KB Output is correct
2 Correct 245 ms 14200 KB Output is correct
3 Correct 243 ms 14200 KB Output is correct