#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 T> using Vec = std::vector<T>;
using OptionIter = std::optional<std::list<usize>::iterator>;
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<std::list<usize>> crit(N);
Vec<Vec<OptionIter>> iter(N, Vec<OptionIter>(M));
{
Vec<Vec<int>> state(N, Vec<int>(M));
Vec<Vec<bool>> done(N, Vec<bool>(M));
std::queue<std::pair<usize, usize>> que;
const auto push = [&](const usize i, const usize j) {
if (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;
if (i + 1 < N) {
push(i + 1, j);
}
if (j + 1 < M) {
push(i, j + 1);
}
}
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;
if (i > 0) {
push(i - 1, j);
}
if (j > 0) {
push(i, j - 1);
}
}
for (const auto i : rep(0, N)) {
for (const auto j : rep(0, M)) {
if (state[i][j] == 3) {
iter[i][j] = crit[i].insert(crit[i].end(), j);
}
}
}
}
usize Q;
std::cin >> Q;
while (Q--) {
usize i, j;
std::cin >> i >> j;
i -= 1;
j -= 1;
if (!iter[i][j]) {
std::cout << 1 << '\n';
continue;
}
const usize up = (i == 0 ? 0 : *crit[i - 1].rbegin());
const usize down = (i + 1 == N ? M - 1 : *crit[i + 1].begin());
if (up <= j and j <= down) {
std::cout << 0 << '\n';
continue;
}
std::cout << 1 << '\n';
if (up <= j) {
auto itr = *iter[i][j];
while (itr != crit[i].end()) {
const auto k = *itr;
itr = crit[i].erase(itr);
iter[i][k] = OptionIter();
}
}
if (j <= down) {
const auto right = std::next(*iter[i][j]);
auto itr = crit[i].begin();
while (itr != right) {
const auto k = *itr;
itr = crit[i].erase(itr);
iter[i][k] = OptionIter();
}
}
}
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
JOI20_furniture_main();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
588 KB |
Output is correct |
2 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
588 KB |
Output is correct |
2 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |