Submission #924714

#TimeUsernameProblemLanguageResultExecution timeMemory
924714boris_mihovFurniture (JOI20_furniture)C++17
100 / 100
248 ms26864 KiB
#include <algorithm> #include <iostream> #include <numeric> #include <cassert> #include <vector> #include <queue> #include <cmath> #include <set> typedef long long llong; const int MAXN = 1000 + 10; const int INF = 1e9; int n, m, q; int a[MAXN][MAXN]; int b[MAXN][MAXN]; int e[MAXN][MAXN]; int cntWithB[2 * MAXN]; bool empty[MAXN][MAXN]; std::vector <std::pair <int,int>> deltaB = {{1, 0}, {0, 1}}; std::vector <std::pair <int,int>> deltaE = {{-1, 0}, {0, -1}}; bool inside(int row, int col) { return (row >= 1 && row <= n && col >= 1 && col <= m); } bool checkCell(int row, int col, std::vector <std::pair <int,int>> &delta, int d[][MAXN]) { int cnt = 0; for (const auto &[dx, dy] : delta) { if (inside(row - dx, col - dy) && d[row - dx][col - dy] == d[row][col] - 1 && !empty[row - dx][col - dy]) { cnt++; } } return cnt == 0; } void runBFS(int row, int col, std::vector <std::pair <int,int>> &delta, int d[][MAXN]) { std::vector <std::pair <int,int>> toErase; std::queue <std::pair <int,int>> q; q.push({row, col}); while (q.size()) { auto [currR, currC] = q.front(); toErase.push_back({currR, currC}); if (!empty[currR][currC]) { cntWithB[b[currR][currC]]--; } empty[currR][currC] = true; q.pop(); for (const auto &[dx, dy] : delta) { if (!inside(currR + dx, currC + dy) || empty[currR + dx][currC + dy] || !checkCell(currR + dx, currC + dy, delta, d)) { continue; } q.push({currR + dx, currC + dy}); } } } bool destroy(int row, int col) { if (empty[row][col]) { return true; } if (cntWithB[b[row][col]] == 1) { return false; } runBFS(row, col, deltaB, b); runBFS(row, col, deltaE, e); return true; } void solve() { for (int i = 1 ; i <= n ; ++i) { for (int j = 1 ; j <= m ; ++j) { b[i][j] = i - 1 + j - 1; e[i][j] = n - i + m - j; cntWithB[b[i][j]]++; } } for (int i = 1 ; i <= n ; ++i) { for (int j = 1 ; j <= m ; ++j) { if (a[i][j] == 1) { destroy(i, j); } } } for (int i = 1 ; i <= q ; ++i) { int row, col; std::cin >> row >> col; std::cout << destroy(row, col) << '\n'; } } void input() { std::cin >> n >> m; for (int i = 1 ; i <= n ; ++i) { for (int j = 1 ; j <= m ; ++j) { std::cin >> a[i][j]; } } std::cin >> q; } void fastIOI() { std::ios_base :: sync_with_stdio(0); std::cout.tie(nullptr); std::cin.tie(nullptr); } int main() { fastIOI(); input(); solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...