Submission #1035273

#TimeUsernameProblemLanguageResultExecution timeMemory
1035273juicyFurniture (JOI20_furniture)C++17
100 / 100
200 ms16724 KiB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 1e3 + 5;
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};

int n, m, q;
int C[N][N], cnt[N * 2];
bool R[N][N];

bool inside(int i, int j) {
  return 1 <= i && i <= n && 1 <= j && j <= m;
}

bool get(int i, int j) {
  if (!inside(i, j)) {
    return 0;
  }
  return !R[i][j];
}

bool check(int i, int j) {
  if (i == 1 && j == 1) {
    return 1;
  }
  if (i == n && j == m) {
    return 1;
  }
  return (get(i - 1, j) || get(i, j - 1)) && (get(i, j + 1) || get(i + 1, j));
}

void bfs(int sr, int sc) {
  queue<array<int, 2>> q;
  R[sr][sc] = 1;
  q.push({sr, sc});
  --cnt[sr + sc];
  while (q.size()) {
    auto [u, v] = q.front(); q.pop();
    for (int dr = 0; dr < 4; ++dr) {
      int i = u + dx[dr], j = v + dy[dr];
      if (inside(i, j) && !R[i][j] && !check(i, j)) {
        R[i][j] = 1;
        --cnt[i + j];
        q.push({i, j});
      }
    }
  }
}

bool tog(int i, int j) {
  if (R[i][j]) {
    return 1;
  }
  if (cnt[i + j] == 1) {
    return 0;
  }
  bfs(i, j);
  return 1;
}

int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);

  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      ++cnt[i + j];
      cin >> C[i][j];
    }
  }  
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (C[i][j]) {
        assert(tog(i, j));
      }
    }
  }
  cin >> q;
  while (q--) {
    int x, y; cin >> x >> y;
    cout << tog(x, y) << "\n";
  }
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...