#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 |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
12 ms |
384 KB |
Output is correct |
3 |
Correct |
14 ms |
384 KB |
Output is correct |
4 |
Correct |
25 ms |
512 KB |
Output is correct |
5 |
Correct |
27 ms |
384 KB |
Output is correct |
6 |
Correct |
32 ms |
384 KB |
Output is correct |
7 |
Correct |
32 ms |
428 KB |
Output is correct |
8 |
Correct |
32 ms |
384 KB |
Output is correct |
9 |
Correct |
33 ms |
416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
12 ms |
384 KB |
Output is correct |
3 |
Correct |
14 ms |
384 KB |
Output is correct |
4 |
Correct |
25 ms |
512 KB |
Output is correct |
5 |
Correct |
27 ms |
384 KB |
Output is correct |
6 |
Correct |
32 ms |
384 KB |
Output is correct |
7 |
Correct |
32 ms |
428 KB |
Output is correct |
8 |
Correct |
32 ms |
384 KB |
Output is correct |
9 |
Correct |
33 ms |
416 KB |
Output is correct |
10 |
Correct |
89 ms |
632 KB |
Output is correct |
11 |
Correct |
20 ms |
384 KB |
Output is correct |
12 |
Correct |
1223 ms |
4732 KB |
Output is correct |
13 |
Correct |
295 ms |
3840 KB |
Output is correct |
14 |
Correct |
2814 ms |
5592 KB |
Output is correct |
15 |
Correct |
2918 ms |
5496 KB |
Output is correct |
16 |
Correct |
3165 ms |
6264 KB |
Output is correct |
17 |
Correct |
3316 ms |
6280 KB |
Output is correct |
18 |
Correct |
3275 ms |
6216 KB |
Output is correct |
19 |
Correct |
3429 ms |
6492 KB |
Output is correct |
20 |
Correct |
3351 ms |
6776 KB |
Output is correct |
21 |
Correct |
3422 ms |
6392 KB |
Output is correct |