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: Accepted)

macro_rules !read_value {($iter :expr ,($($t :tt ) ,*) ) =>{($(read_value !($iter ,$t ) ) ,*) } ;($iter :expr ,[$t :tt ;$len :expr ] ) =>{(0 ..$len ) .map (|_ |read_value !($iter ,$t ) ) .collect ::<Vec <_ >>() } ;($iter :expr ,{chars :$base :expr } ) =>{read_value !($iter ,String ) .chars () .map (|c |(c as u8 -$base as u8 ) as usize ) .collect ::<Vec <usize >>() } ;($iter :expr ,{char :$base :expr } ) =>{read_value !($iter ,{chars :$base } ) [0 ] } ;($iter :expr ,chars ) =>{read_value !($iter ,String ) .chars () .collect ::<Vec <char >>() } ;($iter :expr ,char ) =>{read_value !($iter ,chars ) [0 ] } ;($iter :expr ,usize1 ) =>{read_value !($iter ,usize ) -1 } ;($iter :expr ,$t :ty ) =>{$iter .next () .unwrap () .parse ::<$t >() .unwrap () } ;}
macro_rules !input_inner {($iter :expr ) =>{} ;($iter :expr ,) =>{} ;($iter :expr ,mut $var :ident :$t :tt $($r :tt ) *) =>{let mut $var =read_value !($iter ,$t ) ;input_inner !{$iter $($r ) *} } ;($iter :expr ,mut $var :ident $($r :tt ) *) =>{input_inner !{$iter ,mut $var :usize $($r ) *} } ;($iter :expr ,$var :ident :$t :tt $($r :tt ) *) =>{let $var =read_value !($iter ,$t ) ;input_inner !{$iter $($r ) *} } ;($iter :expr ,$var :ident $($r :tt ) *) =>{input_inner !{$iter ,$var :usize $($r ) *} } ;}
macro_rules !input {(source =$s :expr ,$($r :tt ) *) =>{let mut iter =$s .split_whitespace () ;input_inner !{iter ,$($r ) *} } ;(iter =$iter :ident ,$($r :tt ) *) =>{let s ={use std ::io ::Read ;let mut s =String ::new () ;std ::io ::stdin () .read_to_string (&mut s ) .unwrap () ;s } ;let mut $iter =s .split_whitespace () ;input_inner !{$iter ,$($r ) *} } ;($($r :tt ) *) =>{let s ={use std ::io ::Read ;let mut s =String ::new () ;std ::io ::stdin () .read_to_string (&mut s ) .unwrap () ;s } ;let mut iter =s .split_whitespace () ;input_inner !{iter ,$($r ) *} } ;}
#[allow(unused_imports)]
use std::io::Write;
pub fn with_bufwriter<F>(f: F) -> std::io::Result<()>
where
    F: FnOnce(&mut std::io::BufWriter<std::io::StdoutLock>) -> std::io::Result<()>,
{
    let stdout = std::io::stdout();
    let mut writer = std::io::BufWriter::new(stdout.lock());
    f(&mut writer)
}
fn fail() {
    println!("-1");
}
fn main() {
    input! { n, s:[usize;n], t:[usize;n], u:[usize;n], v:[usize;n] };
    const M: usize = std::usize::MAX;
    let mut ans = vec![vec![0; n]; n];
    for d in 0..64 {
        let b = 1 << d;
        let mut v1 = vec![];
        let mut v2 = vec![];
        for i in 0..n {
            if s[i] == 0 && u[i] & b != 0 {
                for j in 0..n {
                    ans[i][j] |= b;
                }
            } else if s[i] == 1 && u[i] & b == 0 {
            } else {
                v1.push(i);
            }
            if t[i] == 0 && v[i] & b != 0 {
                for j in 0..n {
                    ans[j][i] |= b;
                }
            } else if t[i] == 1 && v[i] & b == 0 {
            } else {
                v2.push(i);
            }
        }
        if std::cmp::min(v1.len(), v2.len()) > 0 {
            if v1.len() == 1 {
                if s[v1[0]] == 0 {
                    for &j in v2.iter() {
                        if t[j] == 1 {
                            ans[v1[0]][j] |= b;
                            for i in 0..n {
                                if i == v1[0] {
                                    continue;
                                }
                                if ans[i][j] & b != 0 {
                                    ans[v1[0]][j] &= !b;
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    for &j in v2.iter() {
                        if t[j] == 0 {
                            for i in 0..n {
                                if i == v1[0] {
                                    continue;
                                }
                                if ans[i][j] & b == 0 {
                                    ans[v1[0]][j] |= b;
                                    break;
                                }
                            }
                        } else {
                            ans[v1[0]][j] |= b;
                        }
                    }
                }
            } else if v2.len() == 1 {
                if t[v2[0]] == 0 {
                    for &i in v1.iter() {
                        if s[i] == 1 {
                            ans[i][v2[0]] |= b;
                            for j in 0..n {
                                if j == v2[0] {
                                    continue;
                                }
                                if ans[i][j] & b != 0 {
                                    ans[i][v2[0]] &= !b;
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    for &i in v1.iter() {
                        if s[i] == 0 {
                            for j in 0..n {
                                if j == v2[0] {
                                    continue;
                                }
                                if ans[i][j] & b == 0 {
                                    ans[i][v2[0]] |= b;
                                    break;
                                }
                            }
                        } else {
                            ans[i][v2[0]] |= b;
                        }
                    }
                }
            } else {
                for i in 0..std::cmp::max(v1.len(), v2.len()) {
                    ans[v1[i % v1.len()]][v2[i % v2.len()]] |= b;
                }
            }
        }
    }
    for i in 0..n {
        let mut and = M;
        let mut or = 0;
        for j in 0..n {
            and &= ans[i][j];
            or |= ans[i][j];
        }
        if s[i] == 0 {
            if u[i] != and {
                fail();
                return;
            }
        } else {
            if u[i] != or {
                fail();
                return;
            }
        }
        let mut and = M;
        let mut or = 0;
        for j in 0..n {
            and &= ans[j][i];
            or |= ans[j][i];
        }
        if t[i] == 0 {
            if v[i] != and {
                fail();
                return;
            }
        } else {
            if v[i] != or {
                fail();
                return;
            }
        }
    }
    with_bufwriter(|out| {
        for i in 0..n {
            for j in 0..n {
                write!(out, "{}{}", ans[i][j], if j + 1 == n { "\n" } else { " " })?;
            }
        }
        Ok(())
    })
    .unwrap();
}