Submission #559283

# Submission time Handle Problem Language Result Execution time Memory
559283 2022-05-09T15:01:51 Z Redhood Paint (COI20_paint) C++14
48 / 100
3000 ms 16816 KB
#include<bits/stdc++.h>

#define fi first
#define se second
#define sz(x) (int)(x).size()
#define pb push_back
#define mkp make_pair
using namespace std;

typedef long long ll;
typedef long double ld;



vector < vector < int > > g;

const int N = (int)2e5 + 10;

vector < int > st[N];
int p[N], cur_color[N];
void make(int v, int C){
    p[v] = v;
    cur_color[v] = C;
}

int fin(int v){
    if(p[v] == v)return v;
    return p[v] = fin(p[v]);
}
void un(int a , int b){
    a = fin(a) , b = fin(b);
    if(a == b)
        return;
    if(sz(st[a]) < sz(st[b]))
        swap(a , b);
    p[b] = a;
    for(auto &u : st[b]){
        if(fin(u) != a)
            st[a].pb(u);
    }
}


vector < pair < int , int > > dir = {{-1,0},{+1,0},{0,-1},{0,+1}};
int r , s;
bool valid(int x , int y){
    if(x >= 0 && x < r && y >= 0 && y < s)
        return true;
    return false;
}
bool onezero = 1;
void makeit(int cell, int clr){

    if(onezero){
        cell = fin(cell);
        if(cur_color[cell] == clr)
            return;

        vector < int > unite_with;
        for(auto &u : st[cell]){
            if(fin(u) != cell && cur_color[fin(u)] == clr){
                unite_with.pb(u);
            }
        }
        st[cell].clear();

        for(auto &u : unite_with)
            un(u , cell);


        cur_color[fin(cell)] = clr;
    }else{
        cell = fin(cell);
        if(cur_color[cell] == clr)
            return;

        vector < int > unite_with;
        vector<int>save;
        for(auto &u : st[cell]){
            if(fin(u) == cell)
                continue;
            if(cur_color[fin(u)] == clr){
                unite_with.pb(u);
            }else
                save.pb(u);
        }
        st[cell] = save;
        for(auto &t : unite_with)
            un(cell, t);
        cur_color[fin(cell)] = clr;
    }
}
signed main(){
    ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
    cin >> r >> s;
    g.resize(r , vector < int > (s));
    onezero = 1;
    for(int i = 0; i < r; ++i)
        for(int j = 0; j < s; ++j){
            cin >> g[i][j];
            if(g[i][j] > 1)
                onezero = 0;
        }

    for(int i = 0; i < r * s; ++i)
        make(i, g[i / s][i % s]);
    for(int i = 0; i < r; ++i)
        for(int j = 0; j < s; ++j){
            for(auto &u : dir){
                int i1 = i + u.fi , j1 = j + u.se;
                if(valid(i1, j1)){
                    if(g[i1][j1] == g[i][j])
                        un(i1*s + j1, i*s + j);
                }
            }
        }
    for(int i = 0; i < r; ++i)
        for(int j = 0; j < s; ++j){
            for(auto &u : dir){
                int i1 = i + u.fi , j1 = j + u.se;
                if(valid(i1, j1)){
                    if(g[i1][j1] != g[i][j]){
                        st[fin(i*s+j)].pb(fin(i1*s+j1));
                    }
                }
            }
        }


    int q;
    cin >> q;

    vector < pair < int , int > > memory;
    for(int i = 0; i < q; ++i){
        /// online lol
        int x , y, c;
        cin >> x >> y >> c;
        --x,--y;
        memory.pb({x*s + y, c});
        if(c > 1){
            onezero = 0;
        }

    }

    for(auto &i : memory){
        int x , y;
        tie(x , y) = i;
        makeit(x , y);

    }


    for(int i = 0; i < r; ++i){
        for(int j = 0; j < s; ++j){
            cout << cur_color[fin(i * s + j)] << ' ';
        }
        cout << '\n';
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 10 ms 5588 KB Output is correct
4 Correct 12 ms 5464 KB Output is correct
5 Correct 105 ms 5528 KB Output is correct
6 Correct 12 ms 5532 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 8288 KB Output is correct
2 Correct 94 ms 9776 KB Output is correct
3 Correct 94 ms 14680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 16444 KB Output is correct
2 Correct 98 ms 16180 KB Output is correct
3 Correct 142 ms 16816 KB Output is correct
4 Correct 113 ms 16776 KB Output is correct
5 Correct 104 ms 15732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 114 ms 12364 KB Output is correct
2 Execution timed out 3068 ms 14792 KB Time limit exceeded
3 Halted 0 ms 0 KB -