Submission #411992

# Submission time Handle Problem Language Result Execution time Memory
411992 2021-05-26T11:49:43 Z 반딧불(#7584) Paint (COI20_paint) C++17
0 / 100
92 ms 13972 KB
#include <bits/stdc++.h>
#define LIM 600

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(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]){
            link[x].push_back(find(tmp));
        }
        link[y].clear();
        link[y].shrink_to_fit();
    }
    else if(link[x].size() > LIM){
        for(auto tmp: link[y]){
            tmp = find(tmp);
            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);
            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);
            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){
            for(auto &nxt: link[p]){
                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:80:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
paint.cpp:81:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |     for(int i=0; i<n*m; i++) scanf("%d", &arr[i]);
      |                              ~~~~~^~~~~~~~~~~~~~~
paint.cpp: In function 'void operate()':
paint.cpp:99:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |     scanf("%d", &q);
      |     ~~~~~^~~~~~~~~~
paint.cpp:102:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |         scanf("%d %d", &p, &c);
      |         ~~~~~^~~~~~~~~~~~~~~~~
paint.cpp:104:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |         scanf("%d", &c);
      |         ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 4940 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 58 ms 8052 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 82 ms 13972 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 92 ms 11932 KB Output isn't correct
2 Halted 0 ms 0 KB -