Submission #381456

# Submission time Handle Problem Language Result Execution time Memory
381456 2021-03-25T08:05:25 Z NONAME Paint (COI20_paint) C++17
0 / 100
252 ms 78700 KB
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, const T b) {a = min(a, b); return (a == b);}
template <typename T> inline bool chmax(T& a, const T b) {a = max(a, b); return (a == b);}

const int steps[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int man = (int)(2e5 + 500);

int n, m;
int p[man], sz[man], col[man];
map <int, set <int> > s[man];

int to(int x, int y) {
    return (x * m) + y;
}

int f(int x) {
    return (x == p[x]) ? x : (p[x] = f(p[x]));
}

void un(int x, int y) {
    int px, py;

    px = f(x);
    py = f(y);

    p[py] = px;
    for (auto& i : s[py]) {
        for (auto& j : i.second) {
            s[px][i.first].insert(j);
        }
    }
    s[py].clear();
    sz[py] = 0;
}

void letsTry(int x, int y, int c) {
    int pr = f(to(x, y));
    col[pr] = c;
    set <int> cur = s[pr][c];
    while (!cur.empty()) {
        if (col[f(*cur.begin())] == c) {
            un(pr, *cur.begin());
        }
        cur.erase(cur.begin());
    }
}

void solve() {
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            int c;
            cin >> c;
            p[to(i, j)] = to(i, j);
            col[to(i, j)] = c;
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if ((i - 1) >= 0) {
                int p1 = f(to(i, j));
                int p2 = f(to(i - 1, j));
                if (col[p1] == col[p2]) {
                    p[p1] = p2;
                }
            }
            if ((j - 1) >= 0) {
                int p1 = f(to(i, j));
                int p2 = f(to(i, j - 1));
                if (col[p1] == col[p2]) {
                    p[p1] = p2;
                }
            }
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            for (int t = 0; t < 4; ++t) {
                int cx = i + steps[t][0];
                int cy = j + steps[t][1];

                if ((cx >= 0) && (cx < n) && (cy >= 0) && (cy < m)) {
                    s[f(to(i, j))][col[f(to(cx, cy))]].insert(f(to(cx, cy)));
                }
            }
        }
    }

//    for (int i = 0; i < n; ++i, cerr << "\n") {
//        for (int j = 0; j < m; ++j) {
//            cerr << f(to(i, j)) << " ";
//        }
//    }
//    cerr << "\n";

    int q;
    cin >> q;
    while (q--) {
        int x, y, c;
        cin >> x >> y >> c;
        --x, --y;

        letsTry(x, y, c);

//        for (int i = 0; i < n; ++i, cerr << "\n") {
//            for (int j = 0; j < m; ++j) {
////                cerr << col[f(to(i, j))] << " ";
//                cerr << f(to(i, j)) << " ";
//            }
//        }
//        cerr << "\n";
    }

    for (int i = 0; i < n; ++i, cout << "\n") {
        for (int j = 0; j < m; ++j) {
            cout << col[f(to(i, j))] << " ";
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    #ifdef _LOCAL
        system("color a");
        freopen("in.txt", "r", stdin);
    #endif

    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 10092 KB Output is correct
2 Incorrect 8 ms 10220 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 116 ms 27116 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 189 ms 67820 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 252 ms 78700 KB Output isn't correct
2 Halted 0 ms 0 KB -