Submission #854346

# Submission time Handle Problem Language Result Execution time Memory
854346 2023-09-27T01:11:40 Z mzh Bob (COCI14_bob) C++17
120 / 120
135 ms 10656 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<map<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;
            }
            while (!lengths[j].empty() && lengths[j].rbegin()->first > streak[i][j]) {
                sum[j] -= (lengths[j].rbegin()->first - streak[i][j]) * lengths[j].rbegin()->second;
                lengths[j][streak[i][j]] += lengths[j].rbegin()->second;
                lengths[j].erase(prev(lengths[j].end()));
            }
            lengths[j][streak[i][j]]++;
            sum[j] += streak[i][j];

            ans += sum[j];
        }
    }

    cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 2480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 119 ms 8548 KB Output is correct
2 Correct 62 ms 10656 KB Output is correct
3 Correct 61 ms 10392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 135 ms 8544 KB Output is correct
2 Correct 69 ms 10404 KB Output is correct
3 Correct 48 ms 10144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 112 ms 8528 KB Output is correct
2 Correct 49 ms 10396 KB Output is correct
3 Correct 47 ms 10324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 8532 KB Output is correct
2 Correct 53 ms 10552 KB Output is correct
3 Correct 48 ms 10140 KB Output is correct