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

#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define SZ(a) (int)(a).size()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define iFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define make_unique(a)                                                         \\
  sort(all((a))), (a).resize(unique(all((a))) - (a).begin())

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long UL; // ULLONG_MAX, %llu
typedef unsigned int UI;       // UINT_MAX %u
typedef double DD;
typedef long double LD;
typedef pair<LL, LL> PLL;
typedef pair<DD, DD> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;

void termi() {
  printf("-1");
  exit(0);
}
const int N = 505;
bool lockrow[64][N], lockcol[64][N];
UL ans[N][N], tmp[64][N][N];
int cmd[N][2], cnt_row[64][N], cnt_col[64][N];
UL val[N][2];
void eval(int k, int i, int j, int ch) {
  cnt_row[k][i] += ch;
  cnt_col[k][j] += ch;
}
int main() {
  int n;
  scanf("%d", &n);
  FOR(i, 0, 1) {
    FOR(j, 1, n) { scanf(" %d", &cmd[j][i]); }
  }
  FOR(i, 0, 1) {
    FOR(j, 1, n) { scanf(" %llu", &val[j][i]); }
  }
  memset(tmp, -1, sizeof tmp); // undetermine
  // Fundamental determination
  int bit_max = 63;
  FOR(k, 0, bit_max) {
    FOR(i, 1, n) {
      int a = ((val[i][0] >> k) & 1);
      if (cmd[i][0] != a) {
        lockrow[k][i] = true;
        FOR(j, 1, n) {
          if (tmp[k][i][j] == -1)
            tmp[k][i][j] = a;
          else if (tmp[k][i][j] != a)
            termi();
          if (!a)
            eval(k, i, j, 1);
        }
      }
      int b = ((val[i][1] >> k) & 1);
      if (cmd[i][1] != b) {
        lockcol[k][i] = true;
        FOR(j, 1, n) {
          if (tmp[k][j][i] == -1)
            tmp[k][j][i] = b;
          else if (tmp[k][j][i] != b)
            termi();
          if (!b)
            eval(k, j, i, 1);
        }
      }
    }
    FOR(i, 1, n) {
      FOR(j, 1, n) {
        if (lockrow[k][i] || lockcol[k][j])
          continue;
        if (cmd[i][0] == cmd[j][1])
          tmp[k][i][j] = cmd[i][0];
        else
          tmp[k][i][j] = 0;
        if (!tmp[k][i][j])
          eval(k, i, j, 1);
      }
    }
    FOR(i, 1, n) {
      if (lockrow[k][i] || cmd[i][0] == 0)
        continue;
      int ch = 0;
      FOR(j, 1, n) {
        if (tmp[k][i][j])
          ch = 1;
      }
      if (ch)
        continue;
      FOR(j, 1, n) {
        if (lockcol[k][j] || cnt_col[k][j] == 1)
          continue;
        tmp[k][i][j] = 1;
        eval(k, i, j, -1);
        break;
      }
    }
    FOR(j, 1, n) {
      if (lockcol[k][j] || cmd[j][1] == 0)
        continue;
      int ch = 0;
      FOR(i, 1, n) {
        if (tmp[k][i][j])
          ch = 1;
      }
      if (ch)
        continue;
      FOR(i, 1, n) {
        if (lockrow[k][i] || cnt_row[k][i] == 1)
          continue;
        tmp[k][i][j] = 1;
        eval(k, i, j, -1);
        break;
      }
    }
    FOR(i, 1, n) {
      FOR(j, 1, n) { ans[i][j] |= ((UL)tmp[k][i][j] << k); }
    }
  }
  FOR(i, 1, n) {
    UL res[2];
    res[0] = ULLONG_MAX;
    res[1] = 0;
    FOR(j, 1, n) res[0] &= ans[i][j], res[1] |= ans[i][j];
    if (res[cmd[i][0]] != val[i][0])
      termi();
    res[0] = ULLONG_MAX, res[1] = 0;
    FOR(j, 1, n) res[0] &= ans[j][i], res[1] |= ans[j][i];
    if (res[cmd[i][1]] != val[i][1])
      termi();
  }
  FOR(i, 1, n) {
    FOR(j, 1, n) { printf("%llu ", ans[i][j]); }
    printf("\\n");
  }
  return 0;
}
/*
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define SZ(a) (int)(a).size()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define iFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define make_unique(a)                                                         \\
  sort(all((a))), (a).resize(unique(all((a))) - (a).begin())

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;
typedef double DD;
typedef long double LD;
typedef pair<LL, LL> PLL;
typedef pair<DD, DD> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef unsigned long long UL; // %llu ULLONG_MAX

const int N = 504, B = 64;
UL ans[N][N];
void termi() {
  printf("-1");
  exit(0);
}
void print(int n) {
  FOR(i, 1, n) {
    FOR(j, 1, n) { printf("%llu ", ans[i][j]); }
    printf("\\n");
  }
}
int t[B][N][N], rlock[B][N], llock[B][N];
int S[N], T[N];
UL U[N], V[N];
int main() {
  int n;
  int bit_max = 63;
  scanf("%d", &n);
  FOR(i, 1, n) scanf("%d", &S[i]);   // row
  FOR(i, 1, n) scanf("%d", &T[i]);   // col
  FOR(i, 1, n) scanf("%llu", &U[i]); // row
  FOR(i, 1, n) scanf("%llu", &V[i]); // col
  // check conflict and locking
  memset(t, -1, sizeof t);
  FOR(k, 0, bit_max) {
    FOR(i, 1, n) {
      FOR(j, 1, n) {
        int a = ((U[i] >> k) & (UL)1);
        int b = ((V[j] >> k) & (UL)1);
        if (S[i] == 0) { // And
          if (a)
            t[k][i][j] = 1;
        } else {
          if (!a)
            t[k][i][j] = 0;
        }
        if (T[j] == 0) { // And
          if (b) {
            if (t[k][i][j] == 0)
              termi();
            t[k][i][j] = 1;
          }
        } else {
          if (!b) {
            if (t[k][i][j] == 1)
              termi();
            t[k][i][j] = 0;
          }
        }
        if (t[k][i][j] == a)
          rlock[k][i] = 1;
        if (t[k][i][j] == b)
          llock[k][j] = 1;
      }
    }
  }
  FOR(k, 0, bit_max) {
    FOR(i, 1, n) {
      if (!rlock[k][i])
        continue;
      FOR(j, 1, n) {
        if (t[k][i][j] == -1) {
          t[k][i][j] = V[j];
          llock[k][j] = 1;
        }
      }
    }
    FOR(j, 1, n) {
      if (!llock[k][j])
        continue;
      FOR(i, 1, n) {
        if (t[k][i][j] == -1) {
          t[k][i][j] = U[i];
        }
      }
    }
  }

  FOR(k, 0, bit_max) {
    int ti = 0;
    int ch = 0;
    FOR(i, 1, n) {
      if (rlock[k][i])
        continue;
      FOR(j, 1, n) {
        if (llock[k][j])
          continue;
        if (S[i] == T[j]) {
          FOR(l, 1, n) {
            if (t[k][i][l] == -1)
              t[k][i][l] = T[l];
            if (t[k][l][j] == -1)
              t[k][l][j] = S[l];
          }
          ch = 1;
          break;
        }
      }
      if (ch)
        break;
    }
    FOR(i, 1, n) {
      if (rlock[k][i])
        continue;
      int tj = ti;
      int cch = 0;
      FOR(j, 1, n) {
        if (llock[k][j])
          continue;
        if (t[k][i][j] == -1) {
          cch = 1;
          if (!ch)
            t[k][i][j] = tj;
          else
            t[k][i][j] = 0;
          tj ^= 1;
        }
      }
      ti ^= cch;
    }
  }
  FOR(k, 0, bit_max) {
    FOR(i, 1, n) {
      FOR(j, 1, n) {
        assert(t[k][i][j] >= 0);
        if (t[k][i][j])
          ans[i][j] |= ((UL)1 << k);
      }
    }
  }

  // final check
  FOR(i, 1, n) {
    UL a = ULLONG_MAX, o = 0;
    FOR(j, 1, n) a &= ans[i][j], o |= ans[i][j];
    if (S[i] == 0 && U[i] != a)
      termi();
    else if (S[i] == 1 && U[i] != o)
      termi();
  }
  FOR(j, 1, n) {
    UL a = ULLONG_MAX, o = 0;
    FOR(i, 1, n) a &= ans[i][j], o |= ans[i][j];
    if (T[j] == 0 && V[j] != a)
      termi();
    else if (T[j] == 1 && V[j] != o)
      termi();
  }

  print(n);
  return 0;
}
/*
22
0 1
1 0
1 1
1 0
0 1
1 0
2 11
14 1

2
0 0
1 1
0 0
1 0
  */