This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <stack>
#include <utility>
#include <vector>
int main() {
int N, M;
std::cin >> N >> M;
std::vector<std::vector<int>> R(N + 2, std::vector<int>(M + 2, 1));
std::vector<int> L(N + M + 1, 0);
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= M; ++j) {
R[i][j] = 0;
++L[i + j];
}
}
const auto set = [&](const int X, const int Y) -> int {
if (R[X][Y] == 1) {
return 1;
}
if (L[X + Y] == 1) {
return 0;
}
std::stack<std::pair<int, int>> st;
const auto push = [&](const int x, const int y) {
if (R[x][y] == 0) {
R[x][y] = 1;
--L[x + y];
st.emplace(x, y);
}
};
push(X, Y);
while (!st.empty()) {
const int x = st.top().first;
const int y = st.top().second;
st.pop();
if (R[x - 1][y + 1] == 1) {
push(x - 1, y);
push(x, y + 1);
}
if (R[x + 1][y - 1] == 1) {
push(x, y - 1);
push(x + 1, y);
}
}
return 1;
};
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= M; ++j) {
int C;
std::cin >> C;
if (C == 1) {
set(i, j);
}
}
}
int Q;
std::cin >> Q;
for (int k = 1; k <= Q; ++k) {
int X, Y;
std::cin >> X >> Y;
std::cout << set(X, Y) << "\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |