Submission #899410

#TimeUsernameProblemLanguageResultExecution timeMemory
899410KeysFurniture (JOI20_furniture)C++14
100 / 100
255 ms19856 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define mp make_pair
#define ar array
#define all(x) x.begin(), x.end()
#define pb push_back
#define endl '\n'
#define f first
#define s second
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
#define trav(i, a) for (auto& i : a)
template <class T> istream &operator>>(istream& in, vector<T> &v) {trav(i, v) in >> i; return in;}

#ifdef LOCAL
#include "../../lib/debug.h"
#else
#define dbg(...)
#endif

const int N = 1000 + 5;
int a[N][N]; // maintain whether we can reach end
int d[2 * N];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int n, m;

void upd(int x, int y) {
  d[x + y]--;
  a[x][y] = 1;
  FOR(i, 0, 4) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (!(nx <= 0 or nx > n or ny <= 0 or ny > m) and !a[nx][ny] and
        ((a[nx + 1][ny] and a[nx][ny + 1] and mp(nx, ny) != mp(n, m)) or (a[nx - 1][ny] and a[nx][ny - 1] and mp(nx, ny) != mp(1ll, 1ll)))) {
      upd(nx, ny);
    }
  }
}


signed main() {
  ios::sync_with_stdio(0); cin.tie(0);
  cin >> n >> m;
  FOR(i, 0, n + 2) FOR(j, 0, m + 2) a[i][j] = 1;
  FOR(i, 1, n + 1) FOR(j, 1, m + 1) {
    a[i][j] = 0;
    d[i + j]++;
  }

  FOR(i, 1, n + 1) FOR(j, 1, m + 1) {
    int x; cin >> x;
    if (x and !a[i][j]) upd(i, j);
  }
  // FOR(i, 1, n + 1) {FOR(j, 1, m + 1) cout << a[i][j] << " "; cout << endl;}

  int q; cin >> q; while (q--) {
    int i, j; cin >> i >> j; 
    if (a[i][j] == 1) cout << 1 << endl;
    else if (d[i + j] == 1) cout << 0 << endl;
    else cout << 1 << endl, upd(i, j);
  }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...