답안 #381468

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
381468 2021-03-25T08:18:02 Z Slavita Paint (COI20_paint) C++14
0 / 100
3000 ms 67180 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);
            sm[che].insert(ad);
        }
    }
}

void changeSm(int cur, int col){
    ve v; v.clear();
    set<int> s; s.clear();
    //cout << "SMcur(" << cur << "): ";
    //for (auto iter : sm[cur]) cout << iter << ' '; cout << en;
    for (auto iter : sm[cur]){
        if (b[iter] == col){
            //cout << "kley: " << cur << ' ' << iter << en;
            //changeSm(iter, col, iter);
            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;
                    }
                }
            }
            v.pb(iter);
            //s.insert(iter);
            //cout << "iter add:";
            //cout << "SMiter(" << iter << "): ";
            //for (auto iter2 : sm[iter]) cout << iter2 << ' '; cout << en;
            for (auto iter2 : sm[iter]){
                s.insert(iter2);
                //cout << iter2 << ' ';
            }
            //cout << en;
        }
    }
    //for (int i = 0; i < si(v); i++) sm[cur].erase(v[i]);
    //cout << "iter add2:";
    for (auto iter2 : s){
        sm[cur].insert(iter2);
        sm[iter2].insert(cur);
        //cout << iter2 << ' ';
    }
    //cout << en;
}

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;
        changeSm(cur, col);
        /*for (int i = 1; i <= n; i++){
            for (int j = 1; j <= m; j++){
                cout << a[mp(i, j)] << ' ';
            }
            cout << "            ";
            for (int j = 1; j <= m; j++){
                cout << b[a[mp(i, j)]] << ' ';
            }
            cout << en;
        }
        cout << en << en;*/
    }
    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;
}
/*
12 11
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 0 0 0 1 1 1 1
1 1 1 0 0 0 0 0 1 1 1
1 1 0 0 0 0 0 0 0 1 1
1 0 0 0 2 2 2 0 0 0 1
1 0 0 0 2 2 2 0 0 0 1
1 0 0 0 2 2 2 0 0 0 1
1 0 0 0 0 0 0 0 0 0 1
1 1 0 0 0 9 0 0 0 1 1
0 1 1 0 0 9 0 0 1 1 0
0 0 1 1 0 0 0 1 1 0 0
0 0 0 1 1 1 1 1 0 0 0
4
12 11 1
5 11 2
4 4 2
6 6 4
*/
# 결과 실행 시간 메모리 Grader output
1 Incorrect 9 ms 9964 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 264 ms 26720 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3072 ms 67180 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3097 ms 50176 KB Time limit exceeded
2 Halted 0 ms 0 KB -