답안 #446614

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
446614 2021-07-22T19:15:40 Z MOUF_MAHMALAT Paint (COI20_paint) C++11
31 / 100
259 ms 85640 KB
#include<bits/stdc++.h>
#define all(s) s.begin(),s.end()
using namespace std;
typedef int ll;
ll n,m,a[200009],p[200009],sz[200009],q;
map<ll,vector<ll> >mp[200009];
ll op(ll x,ll y)
{
    if(x<0||x>=n||y<0||y>=m)
        return -1;
    return x*m+y;
}
ll gp(ll z)
{
    if(p[z]==z)
        return z;
    return p[z]=gp(p[z]);
}
void mrg(ll x,ll y)
{
    x=gp(x),y=gp(y);
    if(x==y||a[x]!=a[y])
        return;
    if(sz[x]<sz[y])
        swap(x,y);
    p[y]=x;
    sz[x]+=sz[y];
    for(auto z:mp[y])
    {
        for(auto u:z.second)
            mp[x][z.first].push_back(u);
    }
}
void go(ll o)
{
    o=gp(o);
    vector<ll>v=mp[o][a[o]];
    mp[o][a[o]].clear();
    sz[o]-=v.size();
    for(auto z:v)
        mrg(z,o);
    if(o==gp(o))
        return;
    go(gp(o));
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    for(ll i=0; i<n*m; i++)
    {
        cin>>a[i];
        p[i]=i;
    }
    for(ll i=0; i<n; i++)
        for(ll j=0; j<m; j++)
        {
            ll o=op(i,j),k;
            k=op(i-1,j);
            if(k>-1)
                mp[o][a[k]].push_back(k),sz[o]++;
            k=op(i,j+1);
            if(k>-1)
                mp[o][a[k]].push_back(k),sz[o]++;
            k=op(i,j-1);
            if(k>-1)
                mp[o][a[k]].push_back(k),sz[o]++;
            k=op(i+1,j);
            if(k>-1)
                mp[o][a[k]].push_back(k),sz[o]++;
        }
    for(ll i=0; i<n*m; i++)
        go(i);
    cin>>q;
    while(q--)
    {
        ll x,y,val,o;
        cin>>x>>y>>val;
        o=op(x-1,y-1);
        o=gp(o);
        a[o]=val;
        go(o);
    }
    for(ll i=0; i<n; i++)
    {
        for(ll j=0; j<m; j++)
        {
            ll o=op(i,j);
            o=gp(o);
            cout<<a[o]<<" ";
        }
        cout<<"\n";
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10060 KB Output is correct
2 Correct 7 ms 10188 KB Output is correct
3 Incorrect 18 ms 15328 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 117 ms 31248 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 188 ms 55144 KB Output is correct
2 Correct 197 ms 54516 KB Output is correct
3 Correct 201 ms 55644 KB Output is correct
4 Correct 230 ms 54948 KB Output is correct
5 Correct 200 ms 52412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 259 ms 85640 KB Output isn't correct
2 Halted 0 ms 0 KB -