제출 #1343194

#제출 시각아이디문제언어결과실행 시간메모리
1343194tolbiFurniture (JOI20_furniture)C++20
100 / 100
158 ms18248 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main(){
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<bool>> v(n, vector<bool>(m));
  vector<int> d(n + m, 0);
  array<int,2> qu[1000000];
  int sz = 0;
  for (int i = 0; i < n; ++i)
  {
    for (int j = 0; j < m; ++j) {
      int x;cin>>x;
      if (x == 1) qu[sz++] = {i, j};
      d[i+j]++;
    }
  }
  while (sz > 0) {
    sz--;
    int i = qu[sz][0], j = qu[sz][1];
    if (i >= 0 && i < n && j >= 0 && j < m && v[i][j] == 0) {
      v[i][j] = 1;
      d[i+j]--;
      if (i == 0 || j == m - 1 || v[i-1][j+1] == 1) {
        qu[sz++]={i-1,j};
        qu[sz++]={i,j+1};
      }
      if (i == n-1 || j == 0 || v[i+1][j-1] == 1) {
        qu[sz++]={i+1,j};
        qu[sz++]={i,j-1};
      }
    }
  }
  int q;cin>>q;
  while (q--) {
    int x, y;cin>>x>>y;
    x--, y--;
    if (v[x][y] == 1) {
      cout << 1 << '\n';
      continue;
    }
    if (d[x+y]>1) {
      cout << 1 << '\n';
      sz = 1;
      qu[0] = {x, y};
      while (sz > 0) {
        sz--;
        int i = qu[sz][0], j = qu[sz][1];
        if (i >= 0 && i < n && j >= 0 && j < m && v[i][j] == 0) {
          v[i][j] = 1;
          d[i+j]--;
          if (i == 0 || j == m - 1 || v[i-1][j+1] == 1) {
            qu[sz++]={i-1,j};
            qu[sz++]={i,j+1};
          }
          if (i == n-1 || j == 0 || v[i+1][j-1] == 1) {
            qu[sz++]={i+1,j};
            qu[sz++]={i,j-1};
          }
        }
      }
    } else cout << 0 << '\n';
  }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...