Submission #381600

# Submission time Handle Problem Language Result Execution time Memory
381600 2021-03-25T10:47:55 Z ne4eHbKa Paint (COI20_paint) C++17
0 / 100
116 ms 20568 KB
#include <bits/stdc++.h>
using namespace std;
#ifndef _LOCAL
//#pragma GCC optimize("O3,Ofast")
#else
#pragma GCC optimize("O0")
#endif
template<typename t> inline void umin(t &a, const t b) {a = min(a, b);}
template<typename t> inline void umax(t &a, const t b) {a = max(a, b);}
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
typedef int8_t byte;
ll time() {return chrono::system_clock().now().time_since_epoch().count();}
mt19937 rnd(time());
#define ft first
#define sd second
#define len(f) int((f).size())
#define bnd(f) (f).begin(), (f).end()
#define _ <<' '<<
const int inf = 1e9 + 5;
const ll inf64 = 4e18 + 5;
const int md = 998244353;
namespace MD {
    void add(int &a, const int b) {if((a += b) >= md) a -= md;}
    void sub(int &a, const int b) {if((a -= b) < 0) a += md;}
    int prod(const int a, const int b) {return ll(a) * b % md;}
};

int n, m, k;
vector<vector<int>> a, id;
const int N = 2e5 + 5;
unordered_map<int, int> f[N];
int p[N], cl[N], lst[N];

int get(int i) {return p[i] == i ? i : p[i] = get(p[i]);}

void unite(int i, int j) {
    i = get(i);
    j = get(j);
    if(i == j) return;
    if(len(f[i]) < len(f[j])) swap(i, j);
    p[j] = i;
    umin(lst[i], lst[j]);
    for(auto &t : f[j]) {
        if(f[i].find(t.ft) == f[i].end())
            f[i][t.ft] = t.sd;
        else
            umin(f[i][t.ft], t.sd);
    }
    f[j].clear();
}

struct ev {
    int i, j, c, t;
    void perform(int x, int y) {
        if(x < 0 || y < 0 || x >= n || y >= m)
            return;
        int u = id[i][j];
        int v = id[x][y];
        auto &e = f[get(v)];
        if(e.find(c) == e.end())
            return;
        if(lst[get(u)] > e[c])
            unite(u, v);
    }
    void perform() {
        int u = id[i][j];
        for(int d : {-1, +1})
            perform(i + d, j),
            perform(i, j + d);
        f[get(u)][c] = t;
        lst[get(u)] = t;
    }
};

vector<ev> es;

void solve() {
    cin >> n >> m;
    a.assign(n, vector<int>(m, 0));
    id = a;
    k = 0;
    for(int i = 0; i < n; ++i)
        for(int j = 0; j < m; ++j)
            cin >> a[i][j], id[i][j] = k++;
    iota(p, p + k, 0);
    for(int i = 0; i < k; ++i)
        lst[i] = +inf;
    int q; cin >> q;
    es.clear();
    for(int i = 0; i < n; ++i)
        for(int j = 0; j < m; ++j)
            es.push_back({i, j, a[i][j], i});
    for(int i = 1; i <= q; ++i) {
        int x, y, c;
        cin >> x >> y >> c;
        es.push_back({--x, --y, c, i + k});
    }
    for(int t = len(es) - 1; ~t; --t)
        es[t].perform();
    for(int i = 0; i < len(es); ++i)
        cl[get(id[es[i].i][es[i].j])] = es[i].c;
    for(int i = 0; i < n; ++i, cout << '\n')
        for(int j = 0; j < m; ++j)
            cout << cl[get(id[i][j])] << ' ';
#ifdef KEK
    for(int i = 0; i < N; ++i)
        f[i].clear();
#endif // KEK
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifndef _LOCAL
//    freopen("file.in", "r", stdin);
//    freopen("file.out", "w", stdout);
#else
    system("color a");
    freopen("in.txt", "r", stdin);
    int t; cin >> t;
    while(t--)
#endif
    solve();
}
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 11372 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 108 ms 18452 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 86 ms 19164 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 116 ms 20568 KB Output isn't correct
2 Halted 0 ms 0 KB -