Submission #554565

# Submission time Handle Problem Language Result Execution time Memory
554565 2022-04-28T18:37:25 Z Yazan_Alattar Bob (COCI14_bob) C++14
120 / 120
132 ms 18024 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 1007;
const ll inf = 2e9;
const ll mod = 2e18;
const double pi = acos(-1);
const double eps = 1e-6;
const int dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0};
const int block = 320;

ll Add(ll x, ll y){
    x %= mod;
    y %= mod;
    return (x + y) % mod;
}

ll Mul(ll x, ll y){
    x %= mod;
    y %= mod;
    return (x * y) % mod;
}

ll n, m, a[M][M], len[M][M], ans;

void rid(vector < pair <ll,ll> > v){
    ll w = 0;
    while(v.size()){
        ans = Add(ans, Mul(w, Mul(v.back().F, v.back().S)));
        w += v.back().S;
        v.pop_back();
    }
    return;
}


int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; ++i) for(int j = 1; j <= m; ++j) cin >> a[i][j];

    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= m; ++j){
            if(a[i][j] == a[i - 1][j]) len[i][j] = len[i - 1][j] + 1;
            else len[i][j] = 1;
        }
    }

    for(int i = n; i; --i){
        vector < pair <ll, ll> > v; //h, w

        for(int j = 1; j <= m; ++j){
            if(a[i][j] != a[i][j - 1]) rid(v), v.clear();

            ll w = 0;
            while(v.size() && v.back().F >= len[i][j]){
                ans = Add(ans, Mul(w, Mul(v.back().F, v.back().S)));
                w += v.back().S;
                v.pop_back();
            }
            ans = Add(ans, Mul(w + 1, len[i][j]));

            v.pb({len[i][j], w + 1});
        }

        rid(v);
    }

    cout << ans << endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 8324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 8328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 8320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 8280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 118 ms 16264 KB Output is correct
2 Correct 93 ms 16212 KB Output is correct
3 Correct 86 ms 17952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 128 ms 16168 KB Output is correct
2 Correct 82 ms 16328 KB Output is correct
3 Correct 87 ms 18024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 131 ms 16100 KB Output is correct
2 Correct 79 ms 17964 KB Output is correct
3 Correct 79 ms 17896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 132 ms 16188 KB Output is correct
2 Correct 86 ms 16144 KB Output is correct
3 Correct 92 ms 17968 KB Output is correct