Submission #576945

# Submission time Handle Problem Language Result Execution time Memory
576945 2022-06-13T20:45:46 Z timmyfeng Paint (COI20_paint) C++17
100 / 100
1033 ms 492600 KB
#include <bits/stdc++.h>
using namespace std;
 
const int D[] = {0, 1, 0, -1};
const int MAGIC = 480;
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int n, m; cin >> n >> m;
 
    vector<int> color(n * m);
    for (auto &i : color) cin >> i;
 
    vector<vector<int>> neighbors(n * m);
    vector<vector<int>> members(n * m);
    vector<int> leader(n * m);
 
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            int id = i * m + j;
            leader[id] = id;
            members[id].push_back(id);
            for (int dir = 0; dir < 4; ++dir) {
                int i_new = i + D[dir], j_new = j + D[(dir + 1) % 4];
                if (0 <= i_new && i_new < n && 0 <= j_new && j_new < m) {
                    neighbors[id].push_back(i_new * m + j_new);
                }
            }
        }
    }
 
    vector<vector<set<int>>> neighbors_by_color(n * m);
    vector<bool> in_vector(n * m);
    set<int> big_groups;
 
    auto normalize = [&](int a) {
        vector<int> neighbors_new;
        for (auto b : neighbors[a]) {
            b = leader[b];
            if (leader[b] != a && !in_vector[b]) {
                neighbors_new.push_back(b);
                in_vector[b] = true;
            }
        }
        swap(neighbors[a], neighbors_new);
    };
 
    auto update = [&](int a) {
        if ((int) members[a].size() >= MAGIC) {
            for (auto b : big_groups) {
                if (a != b && neighbors_by_color[a][color[b]].count(b))
                    neighbors_by_color[b][color[a]].insert(a);
            }
        } else {
            normalize(a);
            for (auto b : big_groups) {
                if (in_vector[b])
                    neighbors_by_color[b][color[a]].insert(a);
            }
            for (auto b : neighbors[a]) in_vector[b] = false;
        }
    };
 
    auto merge = [&](int a, int b) {
        if (members[a].size() < members[b].size()) swap(a, b);
 
        if ((int) members[a].size() < MAGIC && (int) (members[a].size() + members[b].size()) >= MAGIC) {
            neighbors_by_color[a].resize(1e5);
            for (auto u : neighbors[a])
                neighbors_by_color[a][color[leader[u]]].insert(leader[u]);
            big_groups.insert(a);
        }
 
        if ((int) (members[a].size() + members[b].size()) >= MAGIC) {
            for (auto u : neighbors[b])
                neighbors_by_color[a][color[leader[u]]].insert(leader[u]);
            neighbors_by_color[b].clear();
            big_groups.erase(b);
        }
 
        for (auto i : members[b]) leader[i] = a;
 
        members[a].insert(members[a].end(), members[b].begin(), members[b].end());
        members[b].clear();
 
        neighbors[a].insert(neighbors[a].end(), neighbors[b].begin(), neighbors[b].end());
        neighbors[b].clear();
    };
 
    auto fill = [&](int a, int c) {
        a = leader[a];
 
        while (true) {
            if ((int) members[a].size() < MAGIC) {
                bool merged = false;
                auto initial_neighbors = neighbors[a];
                for (auto b : initial_neighbors) {
                    b = leader[b];
                    if (b != a && color[b] == c) {
                        merged = true;
                        merge(a, b);
                        a = leader[a];
                    }
                }
                if (!merged) break;
            } else {
                if (neighbors_by_color[a][c].empty()) break;
 
                int b = *neighbors_by_color[a][c].begin();
                neighbors_by_color[a][c].erase(b);
                b = leader[b];
 
                if (b != a && color[b] == c) {
                    merge(a, b);
                    a = leader[a];
                }
            }
        }
 
        color[a] = c;
        update(a);
    };
 
    for (int i = 0; i < n * m; ++i)
        fill(i, color[i]);
 
    int q; cin >> q;
 
    while (q--) {
        int i, j, c; cin >> i >> j >> c; --i, --j;
        fill(i * m + j, c);
    }
 
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cout << color[leader[i * m + j]] << " ";
        }
        cout << "\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 7 ms 1744 KB Output is correct
4 Correct 14 ms 6316 KB Output is correct
5 Correct 13 ms 6868 KB Output is correct
6 Correct 32 ms 16132 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 102 ms 7556 KB Output is correct
2 Correct 275 ms 302892 KB Output is correct
3 Correct 137 ms 36816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 230 ms 62944 KB Output is correct
2 Correct 257 ms 61192 KB Output is correct
3 Correct 246 ms 97428 KB Output is correct
4 Correct 371 ms 287188 KB Output is correct
5 Correct 285 ms 238340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 192 ms 38136 KB Output is correct
2 Correct 303 ms 43484 KB Output is correct
3 Correct 1033 ms 492600 KB Output is correct
4 Correct 833 ms 457856 KB Output is correct
5 Correct 532 ms 281844 KB Output is correct
6 Correct 344 ms 157344 KB Output is correct
7 Correct 309 ms 119964 KB Output is correct
8 Correct 250 ms 91952 KB Output is correct
9 Correct 387 ms 58500 KB Output is correct