답안 #467271

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
467271 2021-08-22T11:12:53 Z phathnv Paint (COI20_paint) C++11
0 / 100
3000 ms 25516 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 200000;
const int BLOCKSIZE = 444;

int m, n, q, curInd;
vector<vector<int>> ind;
vector<int> adj[N];
set<pair<int, int>> s[N];
int root[N], sz[N], col[N];

int FindRoot(int u) {
    if (u == root[u])
        return u;
    return root[u] = FindRoot(root[u]);
}

void Update(int u, int type) {
    if (sz[u] < BLOCKSIZE)
        return;
    for (int v : adj[u]) {
        v = FindRoot(v);
        if (sz[v] >= BLOCKSIZE) {
            if (type == -1)
                s[v].erase({col[u], u});
            else
                s[v].insert({col[u], u});
        }
    }
}

void Merge(int u, int v) {
    u = FindRoot(u);
    v = FindRoot(v);
    if (u == v)
        return;
    Update(u, -1);
    Update(v, -1);
    col[u] = col[v];
    if (sz[u] < sz[v])
        swap(u, v);
    if (s[u].size() < s[v].size())
        swap(s[u], s[v]);
    for (int x : adj[v])
        adj[u].push_back(x);
    adj[v].clear();
    for (auto p : s[v])
        s[u].insert(p);
    s[v].clear();
    root[v] = u;
    sz[u] += sz[v];
    if (sz[u] >= BLOCKSIZE && sz[u] - sz[v] < BLOCKSIZE) {
        vector<int> newAdj;
        for (int v : adj[u]) {
            v = FindRoot(v);
            if (v == u)
                continue;
            if (sz[v] >= BLOCKSIZE) {
                newAdj.push_back(v);
                adj[v].push_back(u);
            } else {
                s[u].insert({col[v], v});
            }
        }
        adj[u] = newAdj;
    }
    Update(u, 1);
}

void Color(int u, int c) {
    u = FindRoot(u);
    vector<pair<int, int>> shouldMerge, shouldDel;
    for (int v : adj[u])
        if (col[FindRoot(v)] == c)
            shouldMerge.push_back({u, v});
    auto it = s[u].lower_bound({c, -1});
    while (it != s[u].end()) {
        if ((*it).first > c)
            break;
        int v = (*it).second;
        shouldMerge.push_back({u, v});
        shouldDel.push_back({u, v});
        ++it;
    }
    for (auto p : shouldDel)
        s[u].erase(p);
    for (auto p : shouldMerge)
        Merge(p.first, p.second);
    col[FindRoot(u)] = c;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> m >> n;
    ind.assign(m, vector<int>(n, 0));
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            ind[i][j] = curInd++;
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++) {
            if (i + 1 < m) {
                adj[ind[i][j]].push_back(ind[i + 1][j]);
                adj[ind[i + 1][j]].push_back(ind[i][j]);
            }
            if (j + 1 < n) {
                adj[ind[i][j]].push_back(ind[i][j + 1]);
                adj[ind[i][j + 1]].push_back(ind[i][j]);
            }
        }
    for (int i = 0; i < m * n; i++) {
        root[i] = i;
        sz[i] = 1;
        col[i] = -1;
    }
    for (int u = 0; u < m * n; u++) {
        int c;
        cin >> c;
        Color(u, c);
    }
    cin >> q;
    while (q--) {
        int x, y, c;
        cin >> x >> y >> c;
        x--, y--;
        Color(ind[x][y], c);
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++)
            cout << col[FindRoot(ind[i][j])] << ' ';
        cout << '\n';
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14412 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 13 ms 14924 KB Output is correct
4 Incorrect 41 ms 15000 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 17188 KB Output is correct
2 Correct 618 ms 23084 KB Output is correct
3 Execution timed out 3074 ms 24740 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3015 ms 25516 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3053 ms 24040 KB Time limit exceeded
2 Halted 0 ms 0 KB -