답안 #381378

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
381378 2021-03-25T07:08:44 Z NONAME Paint (COI20_paint) C++17
17 / 100
3000 ms 7532 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 a[man], col[man], p[man], l[man], r[man];
bool was[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) {
    x = f(x);
    y = f(y);

    p[y] = x;
    l[x] = min(l[x], l[y]);
    r[x] = max(r[x], r[y]);
}

void btool(int x, int y, int c) {
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            was[to(i, j)] = false;
        }
    }

    queue <array <int, 2> > q;
    q.push({x, y});
    was[to(x, y)] = true;
    while (!q.empty()) {
        auto cur = q.front();
        q.pop();

        for (int i = 0; i < 4; ++i) {
            int cx = cur[0] + steps[i][0];
            int cy = cur[1] + steps[i][1];

            if ((cx >= 0) && (cx < n) && (cy >= 0) && (cy < m) && !was[to(cx, cy)] && (a[to(cx, cy)] == a[to(x, y)])) {
                was[to(cx, cy)] = true;
                q.push({cx, cy});
            }
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            a[to(i, j)] = (was[to(i, j)] ? c : a[to(i, j)]);
        }
    }
}

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

    if (n != 1) {
        int q;
        cin >> q;
        while (q--) {
            int x, y, c;
            cin >> x >> y >> c;
            --x, --y;

            btool(x, y, c);
        }

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

    for (int i = 0; i < m; ++i) {
        p[i] = i;
        l[i] = r[i] = i;
        col[i] = a[i];
    }

    for (int i = 0; (i + 1) < m; ++i) {
        if (col[f(i)] == col[f(i + 1)]) {
            un(i, i + 1);
        }
    }

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

        y = f(y);
        col[y] = c;
        if ((l[y] - 1) >= 0) {
            if (col[f(l[y] - 1)] == col[y]) {
                un(l[y] - 1, y);
            }
        }
        if ((r[y] + 1) < m) {
            if (col[f(r[y] + 1)] == col[y]) {
                un(r[y] + 1, y);
            }
        }
    }

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

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 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 81 ms 492 KB Output is correct
4 Correct 109 ms 364 KB Output is correct
5 Correct 583 ms 584 KB Output is correct
6 Correct 775 ms 492 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 1772 KB Output is correct
2 Correct 50 ms 3692 KB Output is correct
3 Correct 77 ms 7532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3064 ms 1260 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3046 ms 1132 KB Time limit exceeded
2 Halted 0 ms 0 KB -