답안 #467274

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

const int N = 200000;
const int BLOCKSIZE = 10 * 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) {
    u = FindRoot(u);
    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;
    assert(col[u] == col[v]);
    Update(u, -1);
    Update(v, -1);
    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) {
        set<int> newAdj;
        for (int v : adj[u]) {
            v = FindRoot(v);
            if (v == u)
                continue;
            if (sz[v] >= BLOCKSIZE) {
                newAdj.insert(v);
                adj[v].push_back(u);
            } else {
                s[u].insert({col[v], v});
            }
        }
        adj[u] = vector<int>(newAdj.begin(), newAdj.end());
    }
    Update(u, 1);
}

void Color(int u, int c) {
    u = FindRoot(u);
    Update(u, -1);
    col[u] = c;
    Update(u, 1);
    vector<pair<int, int>> shouldMerge, shouldDel;
    for (int v : adj[u]) {
        v = FindRoot(v);
        if (col[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;
        assert(v == FindRoot(v));
        shouldMerge.push_back({u, v});
        shouldDel.push_back(*it);
        ++it;
    }
    for (auto p : shouldDel)
        s[u].erase(p);
    for (auto p : shouldMerge)
        Merge(p.first, p.second);
}

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 10 ms 14524 KB Output is correct
3 Correct 13 ms 14920 KB Output is correct
4 Correct 35 ms 15016 KB Output is correct
5 Correct 565 ms 15612 KB Output is correct
6 Correct 1694 ms 15748 KB Output is correct
7 Correct 8 ms 14412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 17212 KB Output is correct
2 Correct 760 ms 23344 KB Output is correct
3 Execution timed out 3061 ms 28828 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3064 ms 35436 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 724 ms 28464 KB Output is correct
2 Execution timed out 3052 ms 35528 KB Time limit exceeded
3 Halted 0 ms 0 KB -