답안 #381480

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
381480 2021-03-25T08:28:01 Z NONAME Paint (COI20_paint) C++17
8 / 100
3000 ms 48748 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];
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]) {
        s[px].insert(i);
    }
    s[py].clear();
    sz[py] = 0;
}

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

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))].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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9964 KB Output is correct
2 Correct 8 ms 9964 KB Output is correct
3 Correct 17 ms 11904 KB Output is correct
4 Correct 21 ms 11244 KB Output is correct
5 Correct 1062 ms 11116 KB Output is correct
6 Correct 993 ms 10912 KB Output is correct
7 Correct 8 ms 9708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 92 ms 14956 KB Output is correct
2 Correct 1070 ms 18480 KB Output is correct
3 Execution timed out 3065 ms 22264 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3040 ms 48748 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1006 ms 36908 KB Output is correct
2 Execution timed out 3047 ms 33772 KB Time limit exceeded
3 Halted 0 ms 0 KB -