답안 #714052

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
714052 2023-03-23T17:50:53 Z Bliznetc Paint (COI20_paint) C++17
100 / 100
481 ms 51680 KB
#include <bits/stdc++.h>
 
#define f first
#define s second
#define m_p make_pair
#define vec vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x) (int)(x).size()
#define pw(x) (1LL<<(x))
 
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
const int N=2e5+1;
const int K=400;
//vec<vec<int>>who[2*N/K+1];
map<int,vec<int>> who[N];
//map<int,int> mp[N];
int idt;
int c[N];
int id[N],p[N];
vec<int> neig[N];
 
vec<int> with[N];
vec<int> bigs;
 
int n,m;
int ii(int i,int j){
    return i*m+j;
}
void make(int v){
    id[v]=-1;p[v]=v;
}
int get(int v){
    return p[v]=(p[v]==v?v:get(p[v]));
}
 
int used[N],tt;
void get_unique(vec<int> &vc,int x=-1,int y=-1){
    ++tt;
    vec<int> n_vc;
    for(auto &z : vc){
        if(z==x || z==y) continue;
        if(used[z]!=tt)
            n_vc.pb(z);
        used[z]=tt;
    }
    swap(vc,n_vc);
}
void mg(int b,int a,bool type=1){
    a=get(a),b=get(b);
    if(a==b) return;
    if(sz(neig[a])>sz(neig[b])) swap(a,b);
    for(auto &z : neig[a]) z=get(z);
    get_unique(neig[a],a,b);
    for(auto &z : neig[a]){
        if(z==a || z==b) continue;
        neig[b].pb(z);
    }
    if(!type){
        p[a]=b;
        neig[a].clear();
        return;
    }
 
    if(id[b]==-1){
        for(auto &z : neig[b]) z=get(z);
        get_unique(neig[b],a,b);
        if(sz(neig[b])>=K){
            id[b]=idt++;
            bigs.pb(b);
            for(auto &z : neig[b]){
                if(z==b || z==a) continue;
                who[id[b]][c[z]].pb(z);
                with[z].pb(b);
            }
        }
    }
    else if(id[b]!=-1){
        for(auto &z : neig[a]){
            if(z==a || z==b) continue;
            who[id[b]][c[z]].pb(z);
            with[z].pb(b);
        }
    }
    if(sz(with[a])>sz(with[b])) swap(with[a],with[b]);
 
    for(auto &z : with[a])
        with[b].pb(z);
 
    neig[a].clear();p[a]=b;
 
}
 
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i=0;i<n*m;i++)
        make(i);
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++)
            cin>>c[ii(i,j)];
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(i){
                if(c[ii(i-1,j)]==c[ii(i,j)] || get(ii(i-1,j))==get(ii(i,j)))
                    mg(ii(i,j),ii(i-1,j),0);
                else
                    neig[get(ii(i,j))].pb(ii(i-1,j)),neig[get(ii(i-1,j))].pb(ii(i,j));
            }
            if(j){
                if(c[ii(i,j-1)]==c[ii(i,j)] || get(ii(i,j-1))==get(ii(i,j)))
                    mg(ii(i,j),ii(i,j-1),0);
                else
                    neig[get(ii(i,j))].pb(ii(i,j-1)),neig[get(ii(i,j-1))].pb(ii(i,j));
            }
        }
    }
    vec<int> ids(n*m);iota(all(ids),0);
    for(auto &z : ids) z=get(z);
    get_unique(ids);
    for(auto &b : ids){
        for(auto &z : neig[b]) z=get(z);
        get_unique(neig[b]);
 
        if(sz(neig[b])>=K){
            id[b]=idt++;
            bigs.pb(b);
            for(auto &z : neig[b]){
                who[id[b]][c[z]].pb(z);
                with[z].pb(b);
            }
        }
    }
    auto colour=[&](int v,int ct,bool ok){
        c[v]=ct;
//        if(ok)cout<<"V " <<v<<' '<<id[v]<<endl;
        if(id[v]!=-1){
            vec<int> kek=who[id[v]][ct];
            who[id[v]][ct].clear();
            for(auto &z : kek){
//                cout<<"WI "<<z<<
                if(c[v]==c[get(z)])
                   mg(v,z);
            }
        }
        else{
            vec<int> kek=neig[v];
            for(auto &z : kek){
                if(c[v]==c[get(z)])
                    mg(v,z);
            }
        }
//        if(ok)
//            cout<<"ME "<<' '<<sz(with[v])<<endl;
        int b=get(v);
 
        for(auto &z : with[b]){
//            if(ok)cout<<"ZI "<<z<<endl;
            z=get(z);
        }
        get_unique(with[b]);
//        if(ok)
//            cout<<"UNIQ "<<endl;
        for(auto &z : with[b]){
//            if(ok)
//                cout<<"US "<<id[z]<<' '<<sz(neig[z])<<endl;
            if(z!=b)
                who[id[z]][c[b]].pb(b);
        }
//        cout<<"W "<<endl;
    };
    int q;
    cin>>q;
    for(int t=0;t<q;t++){
        int i,j,ct;
        cin>>i>>j>>ct;--i;--j;
//        if()
        colour(get(ii(i,j)),ct,0);
//        cout<<"T "<<t<<endl;
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++)
            cout<<c[get(ii(i,j))]<<' ';
        cout<<'\n';
    }
    return 0;
}
/*
16 5
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19056 KB Output is correct
2 Correct 10 ms 19156 KB Output is correct
3 Correct 17 ms 19796 KB Output is correct
4 Correct 19 ms 19620 KB Output is correct
5 Correct 24 ms 20260 KB Output is correct
6 Correct 18 ms 19540 KB Output is correct
7 Correct 9 ms 19028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 21844 KB Output is correct
2 Correct 116 ms 23756 KB Output is correct
3 Correct 119 ms 27080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 217 ms 35336 KB Output is correct
2 Correct 288 ms 31008 KB Output is correct
3 Correct 265 ms 33460 KB Output is correct
4 Correct 206 ms 30200 KB Output is correct
5 Correct 176 ms 29892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 150 ms 27848 KB Output is correct
2 Correct 416 ms 38836 KB Output is correct
3 Correct 320 ms 38920 KB Output is correct
4 Correct 481 ms 51680 KB Output is correct
5 Correct 289 ms 36144 KB Output is correct
6 Correct 178 ms 37420 KB Output is correct
7 Correct 185 ms 35924 KB Output is correct
8 Correct 174 ms 34752 KB Output is correct
9 Correct 391 ms 36832 KB Output is correct