#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 |
1 |
Correct |
4 ms |
512 KB |
Output is correct |
2 |
Correct |
11 ms |
384 KB |
Output is correct |
3 |
Correct |
14 ms |
384 KB |
Output is correct |
4 |
Correct |
26 ms |
432 KB |
Output is correct |
5 |
Correct |
31 ms |
512 KB |
Output is correct |
6 |
Correct |
33 ms |
384 KB |
Output is correct |
7 |
Correct |
32 ms |
384 KB |
Output is correct |
8 |
Correct |
33 ms |
384 KB |
Output is correct |
9 |
Correct |
32 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
512 KB |
Output is correct |
2 |
Correct |
11 ms |
384 KB |
Output is correct |
3 |
Correct |
14 ms |
384 KB |
Output is correct |
4 |
Correct |
26 ms |
432 KB |
Output is correct |
5 |
Correct |
31 ms |
512 KB |
Output is correct |
6 |
Correct |
33 ms |
384 KB |
Output is correct |
7 |
Correct |
32 ms |
384 KB |
Output is correct |
8 |
Correct |
33 ms |
384 KB |
Output is correct |
9 |
Correct |
32 ms |
384 KB |
Output is correct |
10 |
Correct |
97 ms |
640 KB |
Output is correct |
11 |
Correct |
21 ms |
384 KB |
Output is correct |
12 |
Correct |
1240 ms |
4728 KB |
Output is correct |
13 |
Correct |
290 ms |
3964 KB |
Output is correct |
14 |
Correct |
2874 ms |
5660 KB |
Output is correct |
15 |
Correct |
2961 ms |
5468 KB |
Output is correct |
16 |
Correct |
3275 ms |
5932 KB |
Output is correct |
17 |
Correct |
3425 ms |
6124 KB |
Output is correct |
18 |
Correct |
3324 ms |
6352 KB |
Output is correct |
19 |
Correct |
3426 ms |
6600 KB |
Output is correct |
20 |
Correct |
3353 ms |
6504 KB |
Output is correct |
21 |
Correct |
3450 ms |
6700 KB |
Output is correct |