Problem:
Score : 300 points

Problem Statement

The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower.

He has N parts for each of the three categories. The size of the i-th upper part is A_i, the size of the i-th middle part is B_i, and the size of the i-th lower part is C_i.

To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar.

How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9(1\leq i\leq N)

1 \leq B_i \leq 10^9(1\leq i\leq N)

1 \leq C_i \leq 10^9(1\leq i\leq N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N
B_1 ... B_N
C_1 ... C_N

Output

Print the number of different altars that Ringo can build.

Sample Input 1

2
1 5
2 4
3 6

Sample Output 1

3

The following three altars can be built:

Upper: 1-st part, Middle: 1-st part, Lower: 1-st part

Upper: 1-st part, Middle: 1-st part, Lower: 2-nd part

Upper: 1-st part, Middle: 2-nd part, Lower: 2-nd part

Sample Input 2

3
1 1 1
2 2 2
3 3 3

Sample Output 2

27

Sample Input 3

6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288

Sample Output 3

87

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::cmp::*;

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

#[allow(unused_macros)]
macro_rules! max {
    ($x:expr) => {
      $x
    };
    ($x:expr, $($xs:tt)+) => {
      max($x,max!($($xs)+))
    };
  }

#[allow(unused_macros)]
macro_rules! min {
    ($x:expr) => {
      $x
    };
    ($x:expr, $($xs:tt)+) => {
      min($x,min!($($xs)+))
    };
  }

use std::ascii::AsciiExt;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::io::Write;
use std::iter::Iterator;

use std::fmt;
use std::ops::{Add, Div, Mul, Sub};

#[allow(dead_code)]
const MODLL: i64 = 1000_000_007;

#[derive(Copy, Clone)]
struct Modulo {
    n: i64,
}

// precondition: p is prime and mod
fn mod_inverse(a: i64, p: i64) -> i64 {
    if a == 1 {
        1
    } else {
        (1 - p * mod_inverse(p % a, a)) / a + p
    }
}

#[allow(dead_code)]
impl Modulo {
    fn new(n: i64) -> Modulo {
        Modulo {
            n: (n % MODLL + MODLL) % MODLL,
        }
    }

    fn inv(&self) -> Modulo {
        Modulo {
            n: mod_inverse(self.n, MODLL),
        }
    }
}

impl fmt::Debug for Modulo {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.n)
    }
}

impl Add for Modulo {
    type Output = Modulo;
    fn add(self, other: Modulo) -> Modulo {
        Modulo {
            n: (self.n + other.n) % MODLL,
        }
    }
}
impl Add<i32> for Modulo {
    type Output = Modulo;
    fn add(self, other: i32) -> Modulo {
        Modulo {
            n: (self.n + other as i64) % MODLL,
        }
    }
}
impl Add<i64> for Modulo {
    type Output = Modulo;
    fn add(self, other: i64) -> Modulo {
        Modulo {
            n: (self.n + other) % MODLL,
        }
    }
}
impl Sub for Modulo {
    type Output = Modulo;
    fn sub(self, other: Modulo) -> Modulo {
        Modulo {
            n: (self.n - other.n + MODLL) % MODLL,
        }
    }
}
impl Sub<i32> for Modulo {
    type Output = Modulo;
    fn sub(self, other: i32) -> Modulo {
        self - Modulo::new(other as i64)
    }
}
impl Sub<i64> for Modulo {
    type Output = Modulo;
    fn sub(self, other: i64) -> Modulo {
        self - Modulo::new(other)
    }
}
impl Mul for Modulo {
    type Output = Modulo;
    fn mul(self, other: Modulo) -> Modulo {
        Modulo {
            n: (self.n * other.n) % MODLL,
        }
    }
}
impl Mul<i32> for Modulo {
    type Output = Modulo;
    fn mul(self, other: i32) -> Modulo {
        self * Modulo::new(other as i64)
    }
}
impl Mul<i64> for Modulo {
    type Output = Modulo;
    fn mul(self, other: i64) -> Modulo {
        self * Modulo::new(other)
    }
}
impl Div for Modulo {
    type Output = Modulo;
    fn div(self, other: Modulo) -> Modulo {
        self * other.inv()
    }
}
impl Div<i32> for Modulo {
    type Output = Modulo;
    fn div(self, other: i32) -> Modulo {
        self * mod_inverse(other as i64, MODLL)
    }
}
impl Div<i64> for Modulo {
    type Output = Modulo;
    fn div(self, other: i64) -> Modulo {
        self * mod_inverse(other, MODLL)
    }
}

struct Combination {
    fact: Vec<Modulo>,
    ifact: Vec<Modulo>,
}

#[allow(dead_code)]
impl Combination {
    fn new(n: usize) -> Combination {
        let mut fact = vec![Modulo::new(1); n + 1];
        let mut ifact = vec![Modulo::new(1); n + 1];
        for i in 1..n + 1 {
            fact[i] = fact[i - 1] * i as i64;
            ifact[i] = ifact[i - 1] / i as i64;
        }
        Combination {
            fact: fact,
            ifact: ifact,
        }
    }

    /// n!
    fn fact(&self, n: usize) -> Modulo {
        self.fact[n]
    }
    /// 1/n!
    fn inv_fact(&self, n: usize) -> Modulo {
        self.ifact[n]
    }
    /// nCr
    fn comb(&self, n: usize, r: usize) -> Modulo {
        if r > n {
            Modulo::new(1)
        } else {
            self.fact(n) * self.inv_fact(n - r) * self.inv_fact(r)
        }
    }
}

#[derive(PartialEq, Debug, Clone)]
pub struct UnionFindNode {
    pub par: usize,
    pub rank: usize,
}

#[derive(PartialEq, Debug, Clone)]
pub struct UnionFind(pub Vec<UnionFindNode>);

impl UnionFind {
    //初期化
    pub fn new(size: usize) -> UnionFind {
        let mut vec = Vec::new();
        for i in 0..size {
            vec.push(UnionFindNode { par: i, rank: 0 });
        }

        UnionFind(vec)
    }

    //根を求める
    pub fn find(&mut self, x: usize) -> usize {
        if self.0[x].par == x {
            x
        } else {
            let par = self.0[x].par;
            let v = self.find(par);
            self.0[x].par = v;
            v
        }
    }

    //xとyの集合を併合
    pub fn unite(&mut self, x: usize, y: usize) {
        let x = self.find(x);
        let y = self.find(y);
        if x == y {
            return;
        }

        if self.0[x].rank < self.0[y].rank {
            self.0[x].par = y;
        } else {
            self.0[y].par = x;
            if self.0[x].rank == self.0[y].rank {
                self.0[x].rank += 1;
            }
        }
    }

    //xとyが同じ集合に属するか
    pub fn same(&mut self, x: usize, y: usize) -> bool {
        self.find(x) == self.find(y)
    }
}

pub trait LexicalPermutation {
    fn next_permutation(&mut self) -> bool;
    fn prev_permutation(&mut self) -> bool;
}

impl<T> LexicalPermutation for [T]
where
    T: Ord,
{
    fn next_permutation(&mut self) -> bool {
        if self.len() < 2 {
            return false;
        }
        let mut i = self.len() - 1;
        while i > 0 && self[i - 1] >= self[i] {
            i -= 1;
        }
        if i == 0 {
            return false;
        }
        let mut j = self.len() - 1;
        while j >= i && self[j] <= self[i - 1] {
            j -= 1;
        }
        self.swap(j, i - 1);
        self[i..].reverse();
        true
    }
    fn prev_permutation(&mut self) -> bool {
        if self.len() < 2 {
            return false;
        }
        let mut i = self.len() - 1;
        while i > 0 && self[i - 1] <= self[i] {
            i -= 1;
        }
        if i == 0 {
            return false;
        }
        self[i..].reverse();
        let mut j = self.len() - 1;
        while j >= i && self[j - 1] < self[i - 1] {
            j -= 1;
        }
        self.swap(i - 1, j);
        true
    }
}

//(小さい,同じ,大きい)
pub fn bound_count<T: Ord>(vec: &Vec<T>, val: &T) -> (usize, usize, usize) {
    let lb = lower_bound(vec, 0, vec.len(), val);
    let ub = upper_bound(vec, 0, vec.len(), val);
    (lb, ub - lb, vec.len() - ub)
}

//より小さい
pub fn less_count<T: Ord>(vec: &Vec<T>, val: &T) -> usize {
    lower_bound(vec, 0, vec.len(), val)
}

//より大きい
pub fn larger_count<T: Ord>(vec: &Vec<T>, val: &T) -> usize {
    vec.len() - upper_bound(vec, 0, vec.len(), val)
}

//同じ
pub fn eq_count<T: Ord>(vec: &Vec<T>, val: &T) -> usize {
    upper_bound(vec, 0, vec.len(), val) - lower_bound(vec, 0, vec.len(), val)
}

//vecは昇順ソート済み
//以上
pub fn lower_bound<T: Ord>(vec: &Vec<T>, mut first: usize, mut last: usize, val: &T) -> usize {
    let mut mid;
    while last - first > 1 {
        mid = (first + last) / 2;
        if &vec[mid] < val {
            first = mid;
        } else {
            last = mid;
        }
    }
    if &vec[first] < val {
        last
    } else {
        first
    }
}

//より大きい
pub fn upper_bound<T: Ord>(vec: &Vec<T>, mut first: usize, mut last: usize, val: &T) -> usize {
    let mut mid;
    while last - first > 1 {
        mid = (first + last) / 2;
        if &vec[mid] <= val {
            first = mid;
        } else {
            last = mid;
        }
    }
    if &vec[first] <= val {
        last
    } else {
        first
    }
}

fn main() {
    input! {
        n: usize,
        a:[i64;n],
        b:[i64;n],
        c:[i64;n],
    }

    let mut vec_a = a;
    let vec_b = b;
    let mut vec_c = c;

    vec_a.sort();
    vec_c.sort();

    let mut ans = 0;
    for j in 0..n {
        let a_max = lower_bound(&vec_a, 0, n, &vec_b[j]);
        let c_min = upper_bound(&vec_c, 0, n, &vec_b[j]);

        if a_max == 0 && n-c_min ==0{
            break;
        }
        ans += a_max * (n-c_min);

    } 
    println!("{}", ans);
}