답안 #420821

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
420821 2021-06-08T14:06:32 Z two_sides Paint (COI20_paint) C++17
0 / 100
196 ms 72876 KB
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 200005;
const int MAXC = 100005;
const size_t MAXS = 500;
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};


int N, M;
bool vis[MAXN];

#define valid(x, y) (x) >= 0 && (x) < N && (y) >= 0 && (y) < M
#define flat(x, y) (x) * M + (y)
#define comps(x, y) comps[flat(x, y)]

struct component {
    int col, idx;
    vector <int> big;
    vector <vector <int>> grp;
    vector <pair <int, int>> cell;
} comps[MAXN];

int getCol(int u) {
    return comps[comps[u].idx].col;
}

int getCol(int x, int y) {
    return getCol(flat(x, y));
}

vector <int> get(component &a, int col) {
    static int x, y;
    static vector <int> res;

    res.clear();

    if (a.cell.size() >= MAXS) {
        for (int x : a.grp[col]) {
            x = comps[x].idx;
            if (comps[x].col == col)
                res.push_back(x);
        }
        a.grp[col].clear();
        return res;
    }
    if (col) {
        for (auto pp : a.cell) {
            tie(x, y) = pp;
            for (int k = 0; k < 4; k++)
                if (valid(x + dx[k], y + dy[k]) &&
                getCol(x + dx[k], y + dy[k]) == col)
                    res.push_back(comps(x + dx[k], y + dy[k]).idx);
        }
    } else {
        for (auto pp : a.cell) {
            tie(x, y) = pp;
            for (int k = 0; k < 4; k++)
                if (valid(x + dx[k], y + dy[k]) &&
                getCol(x + dx[k], y + dy[k]) != a.col)
                    res.push_back(comps(x + dx[k], y + dy[k]).idx);
        }
    }
    return res;
}

template <class T>
void destroy(vector <T> &v) {
    v.clear();
    v.shrink_to_fit();
}

bool join(int u, int v) {
    static vector <int> tmp;

    u = comps[u].idx; v = comps[v].idx;
    if (u == v) return false;
    if (comps[u].cell.size() < comps[v].cell.size())
        swap(u, v);
    component &a = comps[u];
    component &b = comps[v];
    for (auto pp : b.cell) {
        a.cell.push_back(pp);
        comps(pp.first, pp.second).idx = u;
    }
    for (auto x : b.big)
        a.big.push_back(comps[x].idx);
    for (auto x : a.big) {
        x = comps[x].idx;
        comps[x].grp[a.col].push_back(u);
    }
    if (a.cell.size() < MAXS) {
        if (a.cell.size() + b.cell.size() >= MAXS) {
            tmp = get(a, 0);
            a.grp.resize(MAXC);
            for (int x : tmp) {
                a.grp[comps[x].col].push_back(x);
                comps[x].big.push_back(u);
            }
            tmp = get(b, 0);
            for (int x : tmp) {
                a.grp[comps[x].col].push_back(x);
                comps[x].big.push_back(u);
            }
        }
    } else if (b.cell.size() < MAXS) {
        tmp = get(b, 0);
        for (int x : tmp) {
            a.grp[comps[x].col].push_back(x);
            comps[x].big.push_back(u);
        }
    } else {
        for (int c = 1; c < MAXC; c++) {
            if (a.grp[c].size() < b.grp[c].size())
                swap(a.grp[c], b.grp[c]);
            for (int x : b.grp[c])
                a.grp[c].push_back(x);
            
        }
    }
    destroy(b.cell);
    destroy(b.big);
    destroy(b.grp);
    return true;
}

void print() {
    for (int i = 0; i < N; i++)
        for (int j = 0; j < M; j++)
            cout << getCol(i, j) << " \n"[j == M - 1];
    cout << "\n";
}

int main() {
    //cin.tie(0)->sync_with_stdio(0);
    cin >> N >> M;
    vector <vector <int>> S(N, vector <int> (M));
    for (int i = 0; i < N; i++)
        for (int j = 0;  j < M; j++)
            cin >> S[i][j];
    queue <pair <int, int>> que;
    vector <int> big;
    for (int i = 0; i < N; i++)
        for (int j = 0; j < M; j++) {
            if (vis[flat(i, j)]) continue;
            que.emplace(i, j); int x, y;
            vis[flat(i, j)] = true;
            comps(i, j).col = S[i][j] + 1;
            while (que.size()) {
                tie(x, y) = que.front(); que.pop();
                comps(i, j).cell.emplace_back(x, y);
                comps(x, y).idx = flat(i, j);
                for (int k = 0; k < 4; k++)
                    if (valid(x + dx[k], y + dy[k]) &&
                    !vis[flat(x + dx[k], y + dy[k])] &&
                    S[x + dx[k]][y + dy[k]] == S[i][j]) {
                        vis[flat(x + dx[k], y + dy[k])] = true;
                        que.emplace(x + dx[k], y + dy[k]);
                    }
            }
            if (comps(i, j).cell.size() >= MAXS)
                big.push_back(flat(i, j));
        }
    for (int u : big) {
        vector <int> neighbor; int x, y;
        for (auto pp : comps[u].cell) {
            tie(x, y) = pp;
            for (int k = 0; k < 4; k++)
                if (valid(x + dx[k], y + dy[k]) &&
                getCol(x + dx[k], y + dy[k]) != comps[u].col)
                    neighbor.push_back(comps(x + dx[k], y + dy[k]).idx);
        }
        comps[u].grp.resize(MAXC);
        for (int x : neighbor) {
            comps[u].grp[comps[x].col].push_back(x);
            comps[x].big.push_back(u);
        }
    }
    int Q; cin >> Q; int currrr = 0;
    while (Q--) {
        int x, y, col; cin >> x >> y >> col;
        x--; y--; col++;
        int u = comps(x, y).idx;
        if (col == comps[u].col) continue;
        comps[u].col = col;
        vector <int> neighbor = get(comps[u], col);
        for (int v : neighbor) join(u, v);
    }
    for (int i = 0; i < N; i++)
        for (int j = 0; j < M; j++)
            cout << getCol(i, j) - 1 << " \n"[j == M - 1];

}

Compilation message

paint.cpp: In function 'int main()':
paint.cpp:181:26: warning: unused variable 'currrr' [-Wunused-variable]
  181 |     int Q; cin >> Q; int currrr = 0;
      |                          ^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 15948 KB Output is correct
2 Correct 11 ms 15948 KB Output is correct
3 Correct 17 ms 16332 KB Output is correct
4 Correct 24 ms 19756 KB Output is correct
5 Incorrect 120 ms 47148 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 148 ms 17868 KB Output is correct
2 Runtime error 196 ms 72876 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 123 ms 59820 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 142 ms 42124 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -