Submission #402742

#TimeUsernameProblemLanguageResultExecution timeMemory
402742KoDFurniture (JOI20_furniture)C++17
100 / 100
720 ms17396 KiB
#include <bits/stdc++.h>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

class rep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { ++itr; }
        constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
        constexpr usize operator*() const noexcept { return itr; }
    };
    const Iter first, last;

  public:
    explicit constexpr rep(const usize first, const usize last) noexcept : first(first), last(std::max(first, last)) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

template <class F> struct RecursiveLambda : private F {
    explicit constexpr RecursiveLambda(F&& f) : F(std::forward<F>(f)) {}
    template <class... Args> constexpr decltype(auto) operator()(Args&&... args) const {
        return F::operator()(*this, std::forward<Args>(args)...);
    }
};

template <class F> constexpr decltype(auto) rec_lambda(F&& f) {
    using G = std::decay_t<F>;
    return RecursiveLambda<G>(std::forward<G>(f));
}

template <class T> using Vec = std::vector<T>;

constexpr isize dx[] = {0, 1, 0, -1};
constexpr isize dy[] = {1, 0, -1, 0};

void JOI20_furniture_main() {
    usize N, M;
    std::cin >> N >> M;
    Vec<Vec<char>> grid(N, Vec<char>(M));
    for (auto& v : grid) {
        for (auto& x : v) {
            std::cin >> x;
        }
    }
    Vec<Vec<bool>> alive(N, Vec<bool>(M));
    {
        Vec<Vec<int>> state(N, Vec<int>(M));
        Vec<Vec<bool>> done(N, Vec<bool>(M));
        std::queue<std::pair<isize, isize>> que;
        const auto push = [&](const isize i, const isize j) {
            if (i >= 0 and j >= 0 and i < (isize)N and j < (isize)M and grid[i][j] == '0' and !done[i][j]) {
                done[i][j] = true;
                que.emplace(i, j);
            }
        };
        push(0, 0);
        while (!que.empty()) {
            const auto [i, j] = que.front();
            que.pop();
            state[i][j] |= 1;
            for (const auto k : rep(0, 2)) {
                push(i + dx[k], j + dy[k]);
            }
        }
        done = Vec<Vec<bool>>(N, Vec<bool>(M));
        push(N - 1, M - 1);
        while (!que.empty()) {
            const auto [i, j] = que.front();
            que.pop();
            state[i][j] |= 2;
            for (const auto k : rep(2, 4)) {
                push(i + dx[k], j + dy[k]);
            }
        }
        for (const auto i : rep(0, N)) {
            for (const auto j : rep(0, M)) {
                if (state[i][j] == 3) {
                    alive[i][j] = true;
                }
            }
        }
    }
    Vec<usize> min(N), max(N);
    for (const auto i : rep(0, N)) {
        min[i] = 0;
        while (!alive[i][min[i]]) {
            min[i] += 1;
        }
        max[i] = M - 1;
        while (!alive[i][max[i]]) {
            max[i] -= 1;
        }
    }
    const auto disable = rec_lambda([&](auto&& dfs, const isize i, const isize j) -> void {
        if (!alive[i][j]) {
            return;
        }
        alive[i][j] = false;
        while (!alive[i][min[i]]) {
            min[i] += 1;
        }
        max[i] = M - 1;
        while (!alive[i][max[i]]) {
            max[i] -= 1;
        }
        for (const auto k : rep(0, 4)) {
            const auto ni = i + dx[k];
            const auto nj = j + dy[k];
            if (ni < 0 or nj < 0 or ni >= (isize)N or nj >= (isize)M) {
                continue;
            }
            if (ni == 0 and nj == 0) {
                continue;
            }
            if (ni == (isize)N - 1 and nj == (isize)M - 1) {
                continue;
            }
            if ((ni == 0 or !alive[ni - 1][nj]) and (nj == 0 or !alive[ni][nj - 1])) {
                dfs(ni, nj);
            }
            if ((ni == (isize)N - 1 or !alive[ni + 1][nj]) and (nj == (isize)M - 1 or !alive[ni][nj + 1])) {
                dfs(ni, nj);
            }
        }
    });
    usize Q;
    std::cin >> Q;
    while (Q--) {
        usize i, j;
        std::cin >> i >> j;
        i -= 1;
        j -= 1;
        if (!alive[i][j]) {
            std::cout << 1 << '\n';
            continue;
        }
        const usize up = (i == 0 ? 0 : max[i - 1]);
        const usize down = (i + 1 == N ? M - 1 : min[i + 1]);
        if (up <= j and j <= down) {
            std::cout << 0 << '\n';
            continue;
        }
        std::cout << 1 << '\n';
        disable(i, j);
    }
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    JOI20_furniture_main();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...