Submission #381557

# Submission time Handle Problem Language Result Execution time Memory
381557 2021-03-25T09:53:04 Z kartel Paint (COI20_paint) C++14
17 / 100
1027 ms 77348 KB
#include <bits/stdc++.h>
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
//#include <time.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
#define F first
#define S second
#define pb push_back
#define M ll(1e9 + 7)
//#define M ll(998244353)
#define sz(x) (int)x.size()
#define re return
#define oo ll(1e18)
#define el '\n'
#define pii pair <int, int>
#define all(x) (x).begin(), (x).end()
#define arr_all(x, n) (x + 1), (x + 1 + n)
#define vi vector<int>
#define eps (ld)1e-9
using namespace std;
typedef long long ll;
//using namespace __gnu_pbds;
//typedef tree <ll, null_type, less_equal <ll> , rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef double ld;
typedef unsigned long long ull;
typedef short int si;

const int N = 2e5 + 500;

int steps[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int n, m, qq;
vector < vector <int> > a, mk;
int l[N], pr[N], r[N], siz[N], color[N], ans[N];
set <int> ng[N];

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

int f(int x) {return (x == pr[x] ? x : pr[x] = f(pr[x]));}
void link(int x, int y) {
    x = f(x);
    y = f(y);

    if (x == y) {
        return;
    }

    if (siz[x] > siz[y]) {
        swap(x, y);
    }

    l[y] = min(l[y], l[x]);
    r[y] = max(r[y], r[x]);
    pr[x] = y;
    siz[y] += siz[x];
}

void solve() {
    queue <pii> q;
    while (qq--) {
        int i, j, c;
        cin >> i >> j >> c; i--; j--;

        q.push({i, j});
        int old = a[i][j];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                mk[i][j] = 0;
            }
        }

        while (sz(q)) {
            int x = q.front().F;
            int y = q.front().S; q.pop();

            a[x][y] = c;

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

                if (cx < 0 || cy < 0 || cx >= n || cy >= m || a[cx][cy] != old || mk[cx][cy]) {
                    continue;
                }

                mk[cx][cy] = 1;
                q.push({cx, cy});
            }
        }
    }

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

void upd(int x, int nc) {
    x = f(x);
    color[x] = nc;

    if (l[x] && color[f(l[x] - 1)] == color[x]) {
        link(l[x] - 1, x);
    }

    if (r[x] < m - 1 && color[f(r[x] + 1)] == color[x]) {
        link(r[x] + 1, x);
    }
}

void solve1() {
    for (int j = 0; j < m; j++) {
        if (j && a[0][j - 1] == a[0][j]) {
            link(j - 1, j);
        }

        if (j < m - 1 && a[0][j] == a[0][j + 1]) {
            link(j, j + 1);
        }
    }

    for (int j = 0; j < m; j++) {
        color[f(j)] = a[0][j];
    }

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

        if (n == 1) {
            upd(y, c);
        }
    }

    for (int j = 0; j < m; j++) {
        if (mk[0][j]) {
            cout << ans[j] << " ";
            continue;
        }

        int x = f(j);

        int c = color[x];

        for (int i = l[x]; i <= r[x]; i++) {
            ans[i] = c;
            mk[0][i] = 1;
        }

        cout << ans[j] << " ";
    }
    cout << el;
}

void solve2() {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            int d = id(i, j);

            pr[d] = d;
            siz[d] = 1;
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            for (int k = 0; k < 4; k++) {
                int x = i + steps[k][0];
                int y = j + steps[k][1];

                if (x < 0 || y < 0 || x >= n || y >= m || a[x][y] != a[i][j]) {
                    continue;
                }

                link(id(i, j), id(x, y));
            }
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            for (int k = 0; k < 4; k++) {
                int x = i + steps[k][0];
                int y = j + steps[k][1];

                if (x < 0 || y < 0 || x >= n || y >= m || a[x][y] == a[i][j]) {
                    continue;
                }

                int par = f(id(i, j));

                ng[par].insert(id(x, y));
            }
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            color[f(id(i, j))] = a[i][j];
        }
    }

    while (qq--) {
        int i, j, c;
        cin >> i >> j >> c;
        i--; j--;

        assert(c <= 1);

        int ID = id(i, j);
        int par = f(ID);

        if (color[par] == c) {
            continue;
        }

        color[par] = c;

        for (auto x : ng[par]) {
            link(f(ID), f(x));
            ng[f(x)].erase(par);
        }
    }

    for (int i = 0; i < n; i++, cout << el) {
        for (int j = 0; j < m; j++) {
            cout << color[f(id(i, j))] << " ";
        }
    }
}

int main()
{
//    mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());;
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//	in("toys.in");
//	out("toys.out");
//    in("input.txt");
//    out("output.txt");
//    cerr.precision(9); cerr << fixed;

//    clock_t tStart = clock();

    cin >> n >> m;

    a.resize(n);
    mk.resize(n);

    for (int i = 0; i < n; i++) {
        a[i].resize(m);
        mk[i].resize(m);

        for (int j = 0; j < m; j++) {
            cin >> a[i][j];

            pr[j] = j;
            siz[j] = 1;
            l[j] = j;
            r[j] = j;
        }
    }

    cin >> qq;

    if (n * m * 1ll * qq <= (ll)100000000) {
        solve();
    }
    else if (n == 1) {
        solve1();
    }
    else {
        solve2();
    }
}


/*

7
4 6 7 2 3 1 5
*/
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9836 KB Output is correct
2 Correct 7 ms 9836 KB Output is correct
3 Correct 37 ms 9964 KB Output is correct
4 Correct 53 ms 9836 KB Output is correct
5 Correct 586 ms 9928 KB Output is correct
6 Correct 1027 ms 9964 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 11500 KB Output is correct
2 Correct 52 ms 13164 KB Output is correct
3 Correct 73 ms 16748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 121 ms 52460 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 105 ms 77348 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -