#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
348 KB |
Output is correct |
3 |
Correct |
6 ms |
520 KB |
Output is correct |
4 |
Correct |
11 ms |
348 KB |
Output is correct |
5 |
Correct |
13 ms |
344 KB |
Output is correct |
6 |
Correct |
15 ms |
348 KB |
Output is correct |
7 |
Correct |
15 ms |
448 KB |
Output is correct |
8 |
Correct |
15 ms |
580 KB |
Output is correct |
9 |
Correct |
15 ms |
584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
348 KB |
Output is correct |
3 |
Correct |
6 ms |
520 KB |
Output is correct |
4 |
Correct |
11 ms |
348 KB |
Output is correct |
5 |
Correct |
13 ms |
344 KB |
Output is correct |
6 |
Correct |
15 ms |
348 KB |
Output is correct |
7 |
Correct |
15 ms |
448 KB |
Output is correct |
8 |
Correct |
15 ms |
580 KB |
Output is correct |
9 |
Correct |
15 ms |
584 KB |
Output is correct |
10 |
Correct |
38 ms |
952 KB |
Output is correct |
11 |
Correct |
9 ms |
348 KB |
Output is correct |
12 |
Correct |
516 ms |
8532 KB |
Output is correct |
13 |
Correct |
101 ms |
5564 KB |
Output is correct |
14 |
Correct |
1255 ms |
13416 KB |
Output is correct |
15 |
Correct |
1241 ms |
13492 KB |
Output is correct |
16 |
Correct |
1385 ms |
14540 KB |
Output is correct |
17 |
Correct |
1451 ms |
15264 KB |
Output is correct |
18 |
Correct |
1459 ms |
14912 KB |
Output is correct |
19 |
Correct |
1476 ms |
15956 KB |
Output is correct |
20 |
Correct |
1386 ms |
15956 KB |
Output is correct |
21 |
Correct |
1429 ms |
15968 KB |
Output is correct |