Problem:
Score : 600 points

Problem Statement

Given are an integer N and arrays S, T, U, and V, each of length N.
Construct an N×N matrix a that satisfy the following conditions:

a_{i,j} is an integer.

0 \leq a_{i,j} \lt 2^{64}.

If S_{i} = 0, the bitwise AND of the elements in the i-th row is U_{i}.

If S_{i} = 1, the bitwise OR of the elements in the i-th row is U_{i}.

If T_{i} = 0, the bitwise AND of the elements in the i-th column is V_{i}.

If T_{i} = 1, the bitwise OR of the elements in the i-th column is V_{i}.

However, there may be cases where no matrix satisfies the conditions.

Constraints

All values in input are integers.

1 \leq N \leq 500

0 \leq S_{i} \leq 1

0 \leq T_{i} \leq 1

0 \leq U_{i} \lt 2^{64}

0 \leq V_{i} \lt 2^{64}

Input

Input is given from Standard Input in the following format:

N
S_{1} S_{2} ... S_{N}
T_{1} T_{2} ... T_{N}
U_{1} U_{2} ... U_{N}
V_{1} V_{2} ... V_{N}

Output

If there exists a matrix that satisfies the conditions, print one such matrix in the following format:

a_{1,1} ... a_{1,N}
:
a_{N,1} ... a_{N,N}

Note that any matrix satisfying the conditions is accepted.

If no matrix satisfies the conditions, print -1.

Sample Input 1

2
0 1
1 0
1 1
1 0

Sample Output 1

1 1
1 0

In Sample Input 1, we need to find a matrix such that:

the bitwise AND of the elements in the 1-st row is 1;

the bitwise OR of the elements in the 2-nd row is 1;

the bitwise OR of the elements in the 1-st column is 1;

the bitwise AND of the elements in the 2-nd column is 0.

Sample Input 2

2
1 1
1 0
15 15
15 11

Sample Output 2

15 11
15 11

Solution in Rust (1.42.0) (judge verdict: Wrong Answer)

#[macro_use]
mod input_mcr {
    // ref: tanakh <https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8>
    // diff: using Parser
    #[macro_export(local_inner_macros)]
    macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut parser = Parser::from_str($s);
        input_inner!{parser, $($r)*}
    };
    (parser = $parser:ident, $($r:tt)*) => {
        input_inner!{$parser, $($r)*}
    };
    (new_stdin_parser = $parser:ident, $($r:tt)*) => {
        let stdin = std::io::stdin();
        let reader = std::io::BufReader::new(stdin.lock());
        let mut $parser = Parser::new(reader);
        input_inner!{$parser, $($r)*}
    };
    ($($r:tt)*) => {
        input!{new_stdin_parser = parser, $($r)*}
    };
}

    #[macro_export(local_inner_macros)]
    macro_rules! input_inner {
    ($parser:ident) => {};
    ($parser:ident, ) => {};
    ($parser:ident, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($parser, $t);
        input_inner!{$parser $($r)*}
    };
}

    #[macro_export(local_inner_macros)]
    macro_rules! read_value {
    ($parser:ident, ( $($t:tt),* )) => {
        ( $(read_value!($parser, $t)),* )
    };
    ($parser:ident, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($parser, $t)).collect::<Vec<_>>()
    };
    ($parser:ident, chars) => {
        read_value!($parser, String).chars().collect::<Vec<char>>()
    };
    ($parser:ident, char_) => {
        read_value!($parser, String).chars().collect::<Vec<char>>()[0]
    };
    ($parser:ident, usize1) => {
        read_value!($parser, usize) - 1
    };
    ($parser:ident, line) => {
        $parser.next_line()
    };
    ($parser:ident, line_) => {
        $parser.next_line().chars().collect::<Vec<char>>()
    };
    ($parser:ident, i64_) => {
        $parser.fast_i64()
    };
    ($parser:ident, usize_) => {
        $parser.fast_i64() as usize
    };
    ($parser:ident, usize1_) => {
        ($parser.fast_i64() - 1) as usize
    };
    ($parser:ident, $t:ty) => {
        $parser.next::<$t>().expect("Parse error")
    };
}

    use std::io;
    use std::io::BufRead;
    use std::str;

    // ref: tatsuya6502 <https://qiita.com/tatsuya6502/items/cd448486f7ef7b5b8c7e>
    // ref: wariuni <https://qiita.com/tatsuya6502/items/cd448486f7ef7b5b8c7e#comment-7040a5ae96305e884eb9>
    // diff: using std::io::BufRead::fill_buf()
    pub struct Parser<R> {
        pub reader: R,
        buf: Vec<u8>,
        pos: usize,
    }

    impl Parser<io::Empty> {
        pub fn from_str(s: &str) -> Parser<io::Empty> {
            Parser {
                reader: io::empty(),
                buf: s.as_bytes().to_vec(),
                pos: 0,
            }
        }
    }

    impl<R: BufRead> Parser<R> {
        pub fn new(reader: R) -> Parser<R> {
            Parser {
                reader: reader,
                buf: vec![],
                pos: 0,
            }
        }
        pub fn update_buf(&mut self) {
            self.buf.clear();
            self.pos = 0;
            loop {
                let (len, complete) = {
                    let buf2 = self.reader.fill_buf().unwrap();
                    self.buf.extend_from_slice(buf2);
                    let len = buf2.len();
                    (len, buf2[len - 1] <= 0x20)
                };
                self.reader.consume(len);
                if complete {
                    break;
                }
            }
        }
        pub fn next<T: str::FromStr>(&mut self) -> Result<T, T::Err> {
            loop {
                let mut begin = self.pos;
                while begin < self.buf.len() && (self.buf[begin] <= 0x20) {
                    begin += 1;
                }
                let mut end = begin;
                while end < self.buf.len() && (self.buf[end] > 0x20) {
                    end += 1;
                }
                if begin != self.buf.len() {
                    self.pos = end;
                    return unsafe { str::from_utf8_unchecked(&self.buf[begin..end]) }.parse::<T>();
                } else {
                    self.update_buf();
                }
            }
        }
        pub fn next_line(&mut self) -> String {
            loop {
                let mut begin = self.pos;
                if begin < self.buf.len() && (self.buf[begin] < 0x20) {
                    begin += 1;
                }
                let mut end = begin;
                while end < self.buf.len() && (self.buf[end] >= 0x20) {
                    end += 1;
                }
                if begin != self.buf.len() {
                    self.pos = end;
                    return unsafe { str::from_utf8_unchecked(&self.buf[begin..end]) }.to_string();
                } else {
                    self.update_buf();
                }
            }
        }
        pub fn fast_i64(&mut self) -> i64 {
            loop {
                let mut begin = self.pos;
                while begin < self.buf.len() && (self.buf[begin] <= 0x20) {
                    begin += 1;
                }
                if begin == self.buf.len() {
                    self.update_buf();
                    continue;
                }
                let mut res = 0;
                let (is_positive, mut end) = match self.buf[begin] {
                    b'+' => (true, begin + 1),
                    b'-' => (false, begin + 1),
                    _ => (true, begin),
                };
                while end < self.buf.len() && (self.buf[end] > 0x20) {
                    res = res * 10 + (self.buf[end] as i64 - '0' as i64);
                    end += 1;
                }
                if begin != self.buf.len() {
                    self.pos = end;
                    return if is_positive { res } else { -res };
                } else {
                    self.update_buf();
                }
            }
        }
    }
}

use input_mcr::*;
use std::cmp::*;

fn main() {
    input! {
        n: usize,
        ss: [u64; n],
        ts: [u64; n],
        us: [u64; n],
        vs: [u64; n],
    }
    let mut res = vec![vec![0; n]; n];
    for i in 0..64 {
        let mut map: Vec<Vec<Option<u64>>> = vec![vec![None; 4]; 4];
        map[0][0] = Some(0);
        map[0][1] = Some(0);
        map[0][2] = Some(0);
        map[1][0] = Some(0);
        map[2][0] = Some(0);
        map[3][3] = Some(1);
        map[3][2] = Some(1);
        map[3][1] = Some(1);
        map[2][3] = Some(1);
        map[1][3] = Some(1);
        let mut count_r = vec![0; 4];
        for k in 0..n {
            let idx = match (ss[k], (us[k] >> i) & 1) {
                (0, 0) => 2,
                (0, 1) => 3,
                (1, 0) => 0,
                (1, 1) => 1,
                _ => panic!(),
            };
            count_r[idx] += 1;
        }
        let mut count_c = vec![0; 4];
        for k in 0..n {
            let idx = match (ts[k], (vs[k] >> i) & 1) {
                (0, 0) => 2,
                (0, 1) => 3,
                (1, 0) => 0,
                (1, 1) => 1,
                _ => panic!(),
            };
            count_c[idx] += 1;
        }
        // eprintln!("count_r = {:?}", count_r);
        // eprintln!("count_c = {:?}", count_c);
        map[1][1] = Some(1);
        map[2][2] = Some(0);
        if count_r[1] != 0 && count_r[2] != 0 {
            if count_c[1] != 0 && count_c[2] != 0 {
                map[1][2] = Some(1);
                map[2][1] = Some(1);
            } else if count_c[1] != 0 && count_c[2] == 0 {
                map[2][1] = Some(0);
            } else if count_c[1] == 0 && count_c[2] != 1 {
                map[1][2] = Some(1);
            } else {
            }
        } else if count_r[1] != 0 && count_r[2] == 0 {
            if count_c[1] != 0 && count_c[2] != 0 {
                map[1][2] = Some(0);
            } else if count_c[1] != 0 && count_c[2] == 0 {
            } else if count_c[1] == 0 && count_c[2] != 1 {
                // unimplemented!()
            } else {
            }
        } else if count_r[1] == 0 && count_r[2] != 0 {
            if count_c[1] != 0 && count_c[2] != 0 {
                map[2][1] = Some(1);
            } else if count_c[1] != 0 && count_c[2] == 0 {
                // unimplemented!()
            } else if count_c[1] == 0 && count_c[2] != 1 {
            } else {
            }
        } else {
        }
        let get_r_idx = |y: usize| match (ss[y], (us[y] >> i) & 1) {
            (0, 0) => 2,
            (0, 1) => 3,
            (1, 0) => 0,
            (1, 1) => 1,
            _ => panic!(),
        };
        let get_c_idx = |y: usize| match (ts[y], (vs[y] >> i) & 1) {
            (0, 0) => 2,
            (0, 1) => 3,
            (1, 0) => 0,
            (1, 1) => 1,
            _ => panic!(),
        };
        for y in 0..n {
            let r_idx = get_r_idx(y);
            for x in 0..n {
                let c_idx = get_c_idx(x);
                // eprintln!("r_idx = {}, c_idx = {}", r_idx, c_idx);
                if let Some(v) = map[r_idx][c_idx] {
                    res[y][x] |= v << i;
                }
            }
        }
        if count_r[1] == 0 && count_r[2] != 0 && count_c[1] != 0 && count_c[2] == 0 {
            let mut must_zero = false;
            let mut must_one = false;
            if count_c[0] == 0 {
                must_zero = true;
            }
            if count_r[3] == 0 {
                must_one = true;
            }
            if must_zero && must_one {
                if count_r[2] == 1 || count_c[1] == 1 {
                    println!("-1");
                    return;
                }
                let mut cs = vec![];
                let mut rs = vec![];
                for y in 0..n {
                    let r_idx = get_r_idx(y);
                    if r_idx == 2 {
                        rs.push(y);
                    }
                }
                for x in 0..n {
                    let c_idx = get_c_idx(x);
                    if c_idx == 1 {
                        cs.push(x);
                    }
                }
                for (ri, &r) in rs.iter().enumerate() {
                    for (ci, &c) in cs.iter().enumerate() {
                        if ri == ci || ri >= count_c[1] || ci >= count_r[2] {
                            res[r][c] |= 1 << i;
                        }
                    }
                }
            } else if must_zero {
                for y in 0..n {
                    let r_idx = get_r_idx(y);
                    for x in 0..n {
                        let c_idx = get_c_idx(x);
                        // eprintln!("r_idx = {}, c_idx = {}", r_idx, c_idx);
                        if map[r_idx][c_idx].is_none() {
                            res[y][x] |= 0 << i;
                        }
                    }
                }
            } else if must_one {
                for y in 0..n {
                    let r_idx = get_r_idx(y);
                    for x in 0..n {
                        let c_idx = get_c_idx(x);
                        // eprintln!("r_idx = {}, c_idx = {}", r_idx, c_idx);
                        if map[r_idx][c_idx].is_none() {
                            res[y][x] |= 1 << i;
                        }
                    }
                }
            } else {
            }
        }
        if count_r[1] != 0 && count_r[2] == 0 && count_c[1] == 0 && count_c[2] != 0 {
            let mut must_zero = false;
            let mut must_one = false;
            if count_r[0] == 0 {
                must_zero = true;
            }
            if count_c[3] == 0 {
                must_one = true;
            }
            if must_zero && must_one {
                if count_c[2] == 1 || count_r[1] == 1 {
                    println!("-1");
                    return;
                }
                let mut cs = vec![];
                let mut rs = vec![];
                for y in 0..n {
                    let r_idx = get_r_idx(y);
                    if r_idx == 1 {
                        rs.push(y);
                    }
                }
                for x in 0..n {
                    let c_idx = get_c_idx(x);
                    if c_idx == 2 {
                        cs.push(x);
                    }
                }
                for (ri, &r) in rs.iter().enumerate() {
                    for (ci, &c) in cs.iter().enumerate() {
                        if ri == ci || ri >= count_c[2] || ci >= count_r[1] {
                            res[r][c] |= 1 << i;
                        }
                    }
                }
            } else if must_zero {
                for y in 0..n {
                    let r_idx = get_r_idx(y);
                    for x in 0..n {
                        let c_idx = get_c_idx(x);
                        // eprintln!("r_idx = {}, c_idx = {}", r_idx, c_idx);
                        if map[r_idx][c_idx].is_none() {
                            res[y][x] |= 0 << i;
                        }
                    }
                }
            } else if must_one {
                for y in 0..n {
                    let r_idx = get_r_idx(y);
                    for x in 0..n {
                        let c_idx = get_c_idx(x);
                        // eprintln!("r_idx = {}, c_idx = {}", r_idx, c_idx);
                        if map[r_idx][c_idx].is_none() {
                            res[y][x] |= 1 << i;
                        }
                    }
                }
            } else {
            }
        }
    }
    for y in 0..n {
        for x in 0..n {
            print!("{} ", res[y][x]);
        }
        println!();
    }
}