Submission #854349

# Submission time Handle Problem Language Result Execution time Memory
854349 2023-09-27T01:51:02 Z mzh Bob (COCI14_bob) C++17
120 / 120
92 ms 16536 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define all(x) begin(x), end(x)

int nxt() {
    int x;
    cin >> x;
    return x;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n = nxt(), m = nxt();
    vector<vector<int>> a(n, vector<int>(m));
    for (int i = 0; i < n; i++) {
        generate(all(a[i]), nxt);
    }

    vector<vector<int>> streak(n, vector<int>(m, 1));
    for (int i = 0; i < n; i++) {
        for (int j = 1; j < m; j++) {
            if (a[i][j] == a[i][j - 1]) {
                streak[i][j] += streak[i][j - 1];
            }
        }
    }

    ll ans = 0;
    vector<vector<pair<int, int>>> lengths(m);
    vector<int> sum(m);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (i > 0 && a[i][j] != a[i - 1][j]) {
                lengths[j].clear();
                sum[j] = 0;
            }
            int cnt = 1;
            while (!lengths[j].empty() && lengths[j].back().first > streak[i][j]) {
                sum[j] -= (lengths[j].back().first - streak[i][j]) * lengths[j].back().second;
                cnt += lengths[j].back().second;
                lengths[j].pop_back();
            }
            lengths[j].push_back({streak[i][j], cnt});
            sum[j] += streak[i][j];

            ans += sum[j];
        }
    }

    cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 2740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 83 ms 8500 KB Output is correct
2 Correct 62 ms 11048 KB Output is correct
3 Correct 55 ms 16420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 8484 KB Output is correct
2 Correct 51 ms 12368 KB Output is correct
3 Correct 55 ms 16412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 92 ms 9600 KB Output is correct
2 Correct 52 ms 16432 KB Output is correct
3 Correct 57 ms 16436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 8484 KB Output is correct
2 Correct 55 ms 16536 KB Output is correct
3 Correct 56 ms 16212 KB Output is correct