답안 #381384

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
381384 2021-03-25T07:14:23 Z Slavita Paint (COI20_paint) C++14
0 / 100
3000 ms 57528 KB
#include <bits/stdc++.h>
#define ve vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi pair<int,int>
#define all(v) v.begin(),v.end()
#define si(v) (int)v.size()
#define en '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

const int N = 2e5 + 228;
const int big = 1e9;
int n, m, b[N], curCol;
map<pi, int> a;
map<pi, int> re;
map<pi, bool> mrk;
queue<int> X, Y;
set<int> sm[N];

void create(int i, int j, int &kol){
    kol++;
    a[mp(i, j)] = kol;
    b[kol] = re[mp(i, j)];
}

/*void update(int i0, int j0, int i, int j){
    a[mp(i, j)] = a[mp(i0, j0)];
}

void check(int i, int j, int i2, int j2){
    if (j2 >= 1 && j2 <= m && i2 >= 1 && i2 <= n && b[a[mp(i2, j2)]] == re[mp(i, j)])
        update(i2, j2, i, j);
}*/

void first(int x, int y, int &kol){
    X.push(x); Y.push(y);
    create(x, y, kol);
}

void put(int x, int y, int kol){
    if (x < 1 || x > n || y < 1 || y > m) return;
    if (mrk[mp(x, y)]) return;
    if (re[mp(x, y)] != curCol) return;
    a[mp(x, y)] = kol;
    X.push(x); Y.push(y);
    mrk[mp(x, y)] = 1;
}

void go(int x, int y, int kol){
    int st[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    for (int i = 0; i < 4; i++) {
        put(x + st[i][0], y + st[i][1], kol);
    }
}

void tryAddSm(int i, int j, int i2, int j2){
    if (i2 >= 1 && i2 <= n && j2 >= 1 && j2 <= m){
        int che = a[mp(i - 1, j)];
        int ad = a[mp(i, j)];
        if (che != ad) sm[ad].insert(che);
    }
}

int main(){
    iostream::sync_with_stdio(false); cin.tie(0); ios_base::sync_with_stdio(false); cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    cin >> n >> m;
    int kol = 0;
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= m; j++){
            int x; cin >> x;
            re[mp(i, j)] = x;
        }
    }


    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= m; j++){
            if (mrk[mp(i, j)]) continue;
            first(i, j, kol);
            curCol = b[a[mp(i, j)]];
            while(!X.empty()){
                int x = X.front(); X.pop();
                int y = Y.front(); Y.pop();
                go(x, y, kol);
            }
        }
    }

    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= m; j++){
            tryAddSm(i, j, i - 1, j);
            tryAddSm(i, j, i, j - 1);
            tryAddSm(i, j, i, j + 1);
            tryAddSm(i, j, i + 1, j);
        }
    }
    /*for (int i = 1; i <= kol; i++){
        cout << b[i] << en;
    }*/
    int q;
    cin >> q;
    while(q--){
        int x, y, col;
        cin >> x >> y >> col;
        int cur = a[mp(x, y)];
        b[cur] = col;
        for (auto iter : sm[cur]){
            if (b[iter] == col){
                for (int i = 1; i <= n; i++){
                    for (int j = 1; j <= m; j++){
                        if (a[mp(i, j)] == iter){
                            a[mp(i, j)] = cur;
                        }
                    }
                }
            }
        }

    }
    //cout << en << en;
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= m; j++){
            cout << b[a[mp(i, j)]] << ' ';
        }
        cout << en;
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 9 ms 9964 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 248 ms 24684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3093 ms 57528 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3066 ms 44012 KB Time limit exceeded
2 Halted 0 ms 0 KB -