Submission #1086845

# Submission time Handle Problem Language Result Execution time Memory
1086845 2024-09-11T15:00:38 Z codexistent Bob (COCI14_bob) C++14
120 / 120
281 ms 34480 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXNM 1005
#define FOR(i, a, b) for(ll i = a; i <= b; i++)
#define RFOR(i, a, b) for(ll i = a; i >= b; i--)

ll n, m, gn = 0, g[MAXNM][MAXNM], rr[MAXNM][MAXNM], s[MAXNM][MAXNM], r = 0;
stack<pair<ll, ll>> st[MAXNM];

int main(){
    cin >> n >> m;
    FOR(i, 1, n) FOR(j, 1, m) cin >> g[i][j];
    FOR(i, 1, n) RFOR(j, m, 1){
        rr[i][j] = 1 + (j != m && g[i][j] == g[i][j + 1]) * rr[i][j + 1];
    }

    // cout << endl;
    FOR(i, 1, n){
        FOR(j, 1, m){
            if(i == 1 || g[i][j] != g[i - 1][j]){
                s[i][j] = rr[i][j];
                while(!st[j].empty()) st[j].pop();

                st[j].push({0, i - 1}), st[j].push({rr[i][j], i});
            }else{
                s[i][j] = s[i - 1][j] + rr[i][j];
                while(st[j].size() >= 2 && st[j].top().first >= rr[i][j]){
                    pair<ll, ll> sti = st[j].top(); st[j].pop();
                    s[i][j] -= (sti.first - rr[i][j]) * (sti.second - st[j].top().second);
                }
                st[j].push({rr[i][j], i});
            }
            // cout << s[i][j] << " ";
            r += s[i][j];
        }
        // cout << endl;
    }

    cout << r << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 13496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 13724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 13904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 13908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 203 ms 31188 KB Output is correct
2 Correct 101 ms 26584 KB Output is correct
3 Correct 100 ms 26452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 281 ms 34320 KB Output is correct
2 Correct 108 ms 26468 KB Output is correct
3 Correct 108 ms 26400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 271 ms 34260 KB Output is correct
2 Correct 100 ms 26448 KB Output is correct
3 Correct 104 ms 26532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 272 ms 34480 KB Output is correct
2 Correct 131 ms 26412 KB Output is correct
3 Correct 99 ms 26448 KB Output is correct