답안 #412003

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
412003 2021-05-26T12:02:52 Z 반딧불(#7584) Paint (COI20_paint) C++17
0 / 100
3000 ms 15344 KB
#include <bits/stdc++.h>
#define LIM 100000

using namespace std;

typedef long long ll;

int n, m;
int arr[200002];

vector<int> link[200002];
int par[200002], sz[200002];
int bigIndex[200002], bigInfo[400000/LIM+3], bigCnt;
bool chk[400000/LIM+3][200002];
bool nonsense[400000/LIM+3];

int find(int x){
    if(x == par[x]) return x;
    return par[x] = find(par[x]);
}

void merge(int x, int y){
    x = find(x), y = find(y);
    if(x==y) return;
    if(sz[x] < sz[y]) swap(x, y);
    par[y] = x, sz[x] += sz[y];

    int laterSum = (int)link[x].size() + (int)link[y].size();
    if(laterSum <= LIM){
        if((int)link[x].size() < (int)link[y].size()) link[x].swap(link[y]);
        for(auto tmp: link[y]){
            tmp = find(tmp);
            if(tmp == x) continue;
            link[x].push_back(tmp);
        }
        link[y].clear();
        link[y].shrink_to_fit();
    }
    else if(link[x].size() > LIM){
        for(auto tmp: link[y]){
            tmp = find(tmp);
            if(tmp == x) continue;
            link[x].push_back(tmp);
            chk[bigIndex[x]][tmp] = 1;
        }
        if((int)link[y].size() > LIM) nonsense[bigIndex[y]] = 1;
        link[y].clear();
    }
    else if(link[y].size() > LIM){
        bigIndex[x] = bigIndex[y];
        bigInfo[bigIndex[x]] = x;
        link[x].swap(link[y]);
        for(auto tmp: link[y]){
            tmp = find(tmp);
            if(tmp == x) continue;
            link[x].push_back(tmp);
            chk[bigIndex[x]][tmp] = 1;
        }
        link[y].clear();
    }
    else{
        int pnt = bigCnt++;
        bigIndex[x] = pnt;
        bigInfo[pnt] = x;
        for(auto &tmp: link[x]){
            tmp = find(tmp);
            chk[pnt][tmp] = 1;
        }
        for(auto tmp: link[y]){
            tmp = find(tmp);
            if(tmp == x) continue;
            link[x].push_back(tmp);
            chk[pnt][tmp] = 1;
        }
        link[y].clear();
    }

    for(int i=0; i<bigCnt; i++){
        if(chk[i][y]) chk[i][x] = 1;
    }
}

void input(){
    scanf("%d %d", &n, &m);
    for(int i=0; i<n*m; i++) scanf("%d", &arr[i]);

    for(int i=0; i<n*m; i++){
        int r = i/m, c = i%m;
        par[i] = i, sz[i] = 1;
        if(r) link[i].push_back(i-m), link[i-m].push_back(i);
        if(c) link[i].push_back(i-1), link[i-1].push_back(1);
    }

    for(int i=0; i<n*m; i++){
        int r = i/m, c = i%m;
        if(r && arr[i] == arr[i-m]) merge(i, i-m);
        if(c && arr[i] == arr[i-1]) merge(i, i-1);
    }
}

void operate(){
    int q;
    scanf("%d", &q);
    while(q--){
        int p, c;
        scanf("%d %d", &p, &c);
        p = find((p-1)*m+(c-1));
        scanf("%d", &c);

        for(int i=0; i<bigCnt; i++){
            if(!nonsense[i] && chk[i][p] && arr[bigInfo[i]] == arr[p]){
                merge(bigInfo[i], p);
                p = find(p);
            }
        }
        arr[p] = c;

        if((int)link[p].size() <= LIM){
            vector<int> tV = link[p];
            for(auto nxt: tV){
                nxt = find(nxt);
                if(arr[nxt] == arr[p]){
                    merge(nxt, p);
                    p = find(p);
                }
            }
        }
    }
}

void output(){
    for(int i=0; i<n*m; i++){
        printf("%d ", arr[find(i)]);
        if(i%m == m-1) puts("");
    }
}

int main(){
    input();
    operate();
    output();
}

Compilation message

paint.cpp: In function 'void input()':
paint.cpp:84:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
paint.cpp:85:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |     for(int i=0; i<n*m; i++) scanf("%d", &arr[i]);
      |                              ~~~~~^~~~~~~~~~~~~~~
paint.cpp: In function 'void operate()':
paint.cpp:103:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |     scanf("%d", &q);
      |     ~~~~~^~~~~~~~~~
paint.cpp:106:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |         scanf("%d %d", &p, &c);
      |         ~~~~~^~~~~~~~~~~~~~~~~
paint.cpp:108:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |         scanf("%d", &c);
      |         ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 4940 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3058 ms 7560 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 117 ms 15344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 164 ms 13064 KB Output isn't correct
2 Halted 0 ms 0 KB -