Problem p02704 (C++) — buggy submission, verdict: Runtime Error

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

int h, w;
bool opy[500], opx[500];
uint64_t y[500], x[500], by[500], bx[500];
uint64_t ans[500][500], b[500][500];

bool solvebit() {
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      b[i][j] = -1;
  vector<int> ry, rx;
  for (int i = 0; i < h; i++) {
    if (opy[i] == by[i])
      for (int j = 0; j < w; j++)
        b[i][j] = by[i];
    else
      ry.push_back(i);
  }
  for (int i = 0; i < w; i++) {
    if (opx[i] == bx[i]) {
      for (int j = 0; j < h; j++) {
        if (b[j][i] == !bx[i])
          return false;
        b[j][i] = bx[i];
      }
    } else
      rx.push_back(i);
  }
  int rh = ry.size(), rw = rx.size();
  if (min(rh, rw) >= 2) {
    if (rw <= rh) {
      for (int i = 0; i < rh; i++) {
        b[ry[i]][rx[i % rw]] = 1;
        b[ry[i]][rx[(i + 1) % rw]] = 0;
      }
    } else {
      for (int i = 0; i < rw; i++) {
        b[ry[i % rh]][rx[i]] = 1;
        b[ry[(i + 1) % rh]][rx[i]] = 0;
      }
    }
  } else if (min(rh, rw) == 1) {
    if (rw == 1) {
      for (int i = 0; i < rh; i++) {
        bool g = false;
        for (int j = 0; j < w; j++)
          if (b[ry[i]][j] == by[ry[i]])
            g = true;
        if (!g)
          b[ry[i]][rx[0]] = by[ry[i]];
      }
      bool g = false;
      for (int i = 0; i < h; i++) {
        if (b[i][rx[0]] == bx[rx[0]]) {
          g = true;
          break;
        }
        if (b[i][rx[0]] == -1) {
          b[i][rx[0]] = bx[rx[0]];
          g = true;
          break;
        }
      }
      if (!g)
        return false;
    } else {
      for (int i = 0; i < rw; i++) {
        bool g = false;
        for (int j = 0; j < h; j++)
          if (b[j][rx[i]] == bx[rx[i]])
            g = true;
        if (!g)
          b[ry[0]][rx[i]] = bx[rx[i]];
      }
      bool g = false;
      for (int i = 0; i < w; i++) {
        if (b[ry[0]][i] == by[ry[0]]) {
          g = true;
          break;
        }
        if (b[ry[0]][i] == -1) {
          b[ry[0]][i] = by[ry[0]];
          g = true;
          break;
        }
      }
      if (!g)
        return false;
    }
  } else if (min(rh, rw) == 0) {
    for (int i = 0; i < h; i++) {
      int beetand = b[i][0], beetor = b[i][0];
      for (int j = 1; j < w; j++) {
        beetand &= b[i][j];
        beetor |= b[i][j];
      }
      if ((opy[i] && beetand != by[i]) || (!opy[i] && beetor != by[i]))
        return false;
    }
    for (int i = 0; i < w; i++) {
      int beetand = b[0][i], beetor = b[0][i];
      for (int j = 1; j < h; j++) {
        beetand &= b[j][i];
        beetor |= b[j][i];
      }
      if ((opx[i] && beetand != bx[i]) || (!opx[i] && beetor != bx[i]))
        return false;
    }
  }
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (b[i][j] == -1)
        b[i][j] = 0;
  return true;
}

int main() {
  cin >> h;
  w = h;
  for (int i = 0; i < h; i++) {
    cin >> opy[i];
    opy[i] = !opy[i];
    // opy[i] = op == "and";
  }
  for (int i = 0; i < w; i++) {
    cin >> opx[i];
    opx[i] = !opx[i];
    // opx[i] = op == "and";
  }
  for (int i = 0; i < h; i++) {
    cin >> y[i];
  }
  for (int i = 0; i < w; i++) {
    cin >> x[i];
  }
  bool f = true;
  for (int k = 0; k < 64; k++) {
    bool g = false;
    for (int i = 0; i < h; i++) {
      by[i] = (y[i] & (1uLL << k)) != 0uLL;
      g |= by[i];
    }
    for (int i = 0; i < w; i++) {
      bx[i] = (x[i] & (1uLL << k)) != 0uLL;
      g |= bx[i];
    }
    if (!g)
      continue;
    if (!solvebit())
      f = false;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        ans[i][j] += b[i][j] * (1uLL << k);
      }
  }
  if (!f) {
    cout << -1 << endl;
    // cout << "Impossible" << endl;
    return 0;
  }
  // cout << "Possible" << endl;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (j)
        cout << " ";
      cout << ans[i][j];
    }
    cout << endl;
  }
}

Error: 0

Fixed submission:

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

int h, w;
bool opy[110], opx[110];
uint64_t y[110], x[110], by[110], bx[110];
uint64_t ans[110][110], b[110][110];

bool solvebit() {
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      b[i][j] = -1;
  vector<int> ry, rx;
  for (int i = 0; i < h; i++) {
    if (opy[i] == by[i])
      for (int j = 0; j < w; j++)
        b[i][j] = by[i];
    else
      ry.push_back(i);
  }
  for (int i = 0; i < w; i++) {
    if (opx[i] == bx[i]) {
      for (int j = 0; j < h; j++) {
        if (b[j][i] == !bx[i])
          return false;
        b[j][i] = bx[i];
      }
    } else
      rx.push_back(i);
  }
  int rh = ry.size(), rw = rx.size();
  if (min(rh, rw) >= 2) {
    if (rw <= rh) {
      for (int i = 0; i < rh; i++) {
        b[ry[i]][rx[i % rw]] = 1;
        b[ry[i]][rx[(i + 1) % rw]] = 0;
      }
    } else {
      for (int i = 0; i < rw; i++) {
        b[ry[i % rh]][rx[i]] = 1;
        b[ry[(i + 1) % rh]][rx[i]] = 0;
      }
    }
  } else if (min(rh, rw) == 1) {
    if (rw == 1) {
      for (int i = 0; i < rh; i++) {
        bool g = false;
        for (int j = 0; j < w; j++)
          if (b[ry[i]][j] == by[ry[i]])
            g = true;
        if (!g)
          b[ry[i]][rx[0]] = by[ry[i]];
      }
      bool g = false;
      for (int i = 0; i < h; i++) {
        if (b[i][rx[0]] == bx[rx[0]]) {
          g = true;
          break;
        }
        if (b[i][rx[0]] == -1) {
          b[i][rx[0]] = bx[rx[0]];
          g = true;
          break;
        }
      }
      if (!g)
        return false;
    } else {
      for (int i = 0; i < rw; i++) {
        bool g = false;
        for (int j = 0; j < h; j++)
          if (b[j][rx[i]] == bx[rx[i]])
            g = true;
        if (!g)
          b[ry[0]][rx[i]] = bx[rx[i]];
      }
      bool g = false;
      for (int i = 0; i < w; i++) {
        if (b[ry[0]][i] == by[ry[0]]) {
          g = true;
          break;
        }
        if (b[ry[0]][i] == -1) {
          b[ry[0]][i] = by[ry[0]];
          g = true;
          break;
        }
      }
      if (!g)
        return false;
    }
  } else if (min(rh, rw) == 0) {
    for (int i = 0; i < h; i++) {
      int beetand = b[i][0], beetor = b[i][0];
      for (int j = 1; j < w; j++) {
        beetand &= b[i][j];
        beetor |= b[i][j];
      }
      if ((opy[i] && beetand != by[i]) || (!opy[i] && beetor != by[i]))
        return false;
    }
    for (int i = 0; i < w; i++) {
      int beetand = b[0][i], beetor = b[0][i];
      for (int j = 1; j < h; j++) {
        beetand &= b[j][i];
        beetor |= b[j][i];
      }
      if ((opx[i] && beetand != bx[i]) || (!opx[i] && beetor != bx[i]))
        return false;
    }
  }
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (b[i][j] == -1)
        b[i][j] = 0;
  return true;
}

int main() {
  cin >> h;
  w = h;
  for (int i = 0; i < h; i++) {
    cin >> opy[i];
    opy[i] = !opy[i];
    // opy[i] = op == "and";
  }
  for (int i = 0; i < w; i++) {
    cin >> opx[i];
    opx[i] = !opx[i];
    // opx[i] = op == "and";
  }
  for (int i = 0; i < h; i++) {
    cin >> y[i];
  }
  for (int i = 0; i < w; i++) {
    cin >> x[i];
  }
  bool f = true;
  for (int k = 0; k < 64; k++) {
    bool g = false;
    for (int i = 0; i < h; i++) {
      by[i] = (y[i] & (1uLL << k)) != 0;
      g |= by[i];
    }
    for (int i = 0; i < w; i++) {
      bx[i] = (x[i] & (1uLL << k)) != 0;
      g |= bx[i];
    }
    if (!g)
      continue;
    if (!solvebit())
      f = false;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        ans[i][j] += b[i][j] * (1uLL << k);
      }
  }
  if (!f) {
    cout << -1 << endl;
    // cout << "Impossible" << endl;
    return 0;
  }
  // cout << "Possible" << endl;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (j)
        cout << " ";
      cout << ans[i][j];
    }
    cout << endl;
  }
}