답안 #1003961

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1003961 2024-06-20T20:56:38 Z mariaclara Paint (COI20_paint) C++17
8 / 100
3000 ms 4436 KB
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 5e5+5;
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define fr first
#define sc second

int n, m, q, c, aux[4] = {0,0,1,-1};
vector<vector<bool>> vis;
vector<vector<int>> v;
vector<pii> V;

void dfs(int x, int y) {
    V.pb({x,y});
    vis[x][y] = 1;

    for(int i = 0; i < 4; i++) {
        int a = x+aux[i], b = y+aux[3-i];
        if(a >= 0 and b >= 0 and a < n and b < m and v[a][b] == v[x][y] and !vis[a][b])
            dfs(a,b);
    }
    
    v[x][y] = c;
}

void bfs(int v1, int v2) {
    queue<pii> fila;
    fila.push({v1,v2});
    vis[v1][v2] = 1;

    while(!fila.empty()) {
        auto [x,y] = fila.front();
        fila.pop();
        V.pb({x,y});
        
        for(int i = 0; i < 4; i++) {
            int a = x+aux[i], b = y+aux[3-i];
            if(a >= 0 and b >= 0 and a < n and b < m and v[a][b] == v[x][y] and !vis[a][b])
                fila.push({a,b}), vis[a][b] = 1;
        }

        v[x][y] = c;
    }
}
int main() {

    cin >> n >> m;
    v.resize(n);
    vis.resize(n);
    
    for(int i = 0; i < n; i++) {
        v[i].resize(m);
        vis[i].resize(m);
        for(int j = 0; j < m; j++)
            cin >> v[i][j];
    }

    cin >> q;

    if(n == 1) {
        set<pair<pii,int>> s;

        int last = 0;
        for(int i = 0; i < m; i++)
            if(i > 0 and v[0][i-1] != v[0][i])
                s.insert({{last, i-1}, v[0][last]}), last = i;
        
        s.insert({{last, m-1}, v[0][last]});
        
        while(q--) {
            int ind;
            cin >> ind >> ind >> c;
            ind--;
            
            auto ptr = s.upper_bound({{ind,m},m});

            if(ptr != s.end()) {
                if((*ptr).sc == c) {
                    int b = (*ptr).fr.sc;
                    ptr--;
                    int a = (*ptr).fr.fr;
                    ptr = s.erase(ptr);
                    s.erase(ptr);
                    s.insert({{a,b},c});
                    ptr = s.upper_bound({{ind,m},m});
                }
            }

            ptr--;

            if(ptr != s.begin()) {
                int b = (*ptr).fr.sc;
                ptr--;
                if((*ptr).sc == c) {
                    int a = (*ptr).fr.fr;
                    ptr = s.erase(ptr);
                    s.insert({{a,b},c});
                }
            }

            ptr = s.upper_bound({{ind,m},m});
            ptr--;
            int a = (*ptr).fr.fr, b = (*ptr).fr.sc;
            s.erase(ptr);
            s.insert({{a,b},c});
        }

        for(auto [a,b] : s) {
            int x = a.sc - a.fr + 1;
            while(x--)
                cout << b << " ";
        }
    }
    else{
        while(q--) {
            int a, b;
            cin >> a >> b >> c;
            a--;
            b--;

            bfs(a,b);

            for(auto [x,y] : V) vis[x][y] = 0;
            V.clear();
        }
        
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                cout << v[i][j] << " \n"[j==m-1];
    }

}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 3 ms 556 KB Output is correct
4 Correct 4 ms 348 KB Output is correct
5 Correct 303 ms 680 KB Output is correct
6 Correct 598 ms 728 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 82 ms 4436 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3064 ms 3936 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 157 ms 2700 KB Output is correct
2 Execution timed out 3049 ms 3528 KB Time limit exceeded
3 Halted 0 ms 0 KB -