Submission #379897

#TimeUsernameProblemLanguageResultExecution timeMemory
379897VROOM_VARUNFurniture (JOI20_furniture)C++14
100 / 100
1918 ms12632 KiB
/*
ID: varunra2
LANG: C++
TASK: furniture
*/

#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
  cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif

#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second

const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions

int n, m;
vector<vector<bool>> grid, fromstart, fromend;

void init() {
  grid.resize(n);
  trav(x, grid) x.assign(m, false);
  fromstart.resize(n);
  trav(x, fromstart) { x.assign(m, true); }
  fromend = fromstart;
}

bool inbnds(int x, int y) { return x >= 0 and y >= 0 and x < n and y < m; }

bool addable(int x, int y) {
  bool ret = false;
  if (inbnds(0, y + 1)) {
    for (int i = 0; i < x; i++) {
      if (fromend[i][y + 1] and fromstart[i][y + 1]) {
        ret = true;
        break;
      }
    }
  }
  if (!ret and inbnds(n - 1, y - 1)) {
    for (int i = x + 1; i < n; i++) {
      if (fromend[i][y - 1] and fromstart[i][y - 1]) {
        ret = true;
        break;
      }
    }
  }
  return ret;
}

void addend(int x, int y) {
  fromend[x][y] = false;
  // so if we are coming from the end, we need to look at (x - 1, y), (x, y - 1)
  if (inbnds(x - 1, y) and fromend[x - 1][y]) {
    // we need to check if we can make it false, and then remove it
    bool ok = false;
    if (inbnds(x - 1, y + 1)) {
      ok = fromend[x - 1][y + 1];
    }
    if (!ok) {
      addend(x - 1, y);
    }
  }
  if (inbnds(x, y - 1) and fromend[x][y - 1]) {
    bool ok = false;
    if (inbnds(x + 1, y - 1)) {
      ok = fromend[x + 1][y - 1];
    }
    if (!ok) {
      addend(x, y - 1);
    }
  }
}

void addfront(int x, int y) {
  fromstart[x][y] = false;
  if (inbnds(x + 1, y) and fromstart[x + 1][y]) {
    bool ok = false;
    if (inbnds(x + 1, y - 1)) {
      ok = fromstart[x + 1][y - 1];
    }
    if (!ok) {
      addfront(x + 1, y);
    }
  }
  if (inbnds(x, y + 1) and fromstart[x][y + 1]) {
    bool ok = false;
    if (inbnds(x - 1, y + 1)) {
      ok = fromstart[x - 1][y + 1];
    }
    if (!ok) {
      addfront(x, y + 1);
    }
  }
}

bool add(int x, int y) {
  bool ok = addable(x, y);
  if (!ok) return false;
  // we checked if we are allowed to add it
  addfront(x, y);
  addend(x, y);
  return true;
}

void pr() {
  debug("fromend");
  trav(x, fromend) debug(x);
  debug("fromstart");
  trav(x, fromstart) debug(x);
}

int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("furniture.in", "r", stdin);
  // freopen("furniture.out", "w", stdout);
  // #endif
  cin.sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> m;

  init();
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int val;
      cin >> val;
      if (val) {
        assert(add(i, j));
      }
    }
  }

  int q;
  cin >> q;

  while (q--) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    if (add(x, y))
      cout << '1';
    else
      cout << '0';
    cout << '\n';
  }

  return 0;
}

Compilation message (stderr)

furniture.cpp: In function 'void pr()':
furniture.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define debug(...) 42
      |                    ^~
furniture.cpp:145:3: note: in expansion of macro 'debug'
  145 |   debug("fromend");
      |   ^~~~~
furniture.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define debug(...) 42
      |                    ^~
furniture.cpp:146:20: note: in expansion of macro 'debug'
  146 |   trav(x, fromend) debug(x);
      |                    ^~~~~
furniture.cpp:31:11: warning: unused variable 'first' [-Wunused-variable]
   31 | #define x first
      |           ^~~~~
furniture.cpp:48:31: note: in definition of macro 'trav'
   48 | #define trav(a, x) for (auto& a : x)
      |                               ^
furniture.cpp:146:8: note: in expansion of macro 'x'
  146 |   trav(x, fromend) debug(x);
      |        ^
furniture.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define debug(...) 42
      |                    ^~
furniture.cpp:147:3: note: in expansion of macro 'debug'
  147 |   debug("fromstart");
      |   ^~~~~
furniture.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define debug(...) 42
      |                    ^~
furniture.cpp:148:22: note: in expansion of macro 'debug'
  148 |   trav(x, fromstart) debug(x);
      |                      ^~~~~
furniture.cpp:31:11: warning: unused variable 'first' [-Wunused-variable]
   31 | #define x first
      |           ^~~~~
furniture.cpp:48:31: note: in definition of macro 'trav'
   48 | #define trav(a, x) for (auto& a : x)
      |                               ^
furniture.cpp:148:8: note: in expansion of macro 'x'
  148 |   trav(x, fromstart) debug(x);
      |        ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...