#[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; use std::io::*; #[allow(dead_code)] fn getline() -> String { let mut ret = String::new(); std::io::stdin().read_line(&mut ret).ok(); return ret; } fn get_word() -> String { let mut stdin = std::io::stdin(); let mut u8b: [u8; 1] = [0]; loop { let mut buf: Vec = Vec::with_capacity(16); loop { let res = stdin.read(&mut u8b); if res.is_err() || res.ok().unwrap() == 0 || u8b[0] <= ' ' as u8 { break; } else { buf.push(u8b[0]); } } if buf.len() >= 1 { let ret = std::string::String::from_utf8(buf).unwrap(); return ret; } } } fn parse(s: &str) -> T { s.parse::().ok().unwrap() } #[allow(dead_code)] fn get() -> T { parse(&get_word()) } /** * Segment Tree. This data structure is useful for fast folding on intervals of an array * whose elements are elements of monoid M. Note that constructing this tree requires the identity * element of M and the operation of M. * Verified by: yukicoder No. 259 (http://yukicoder.me/submissions/100581) */ struct SegTree { n: usize, dat: Vec, op: BiOp, e: I, } impl SegTree where BiOp: Fn(I, I) -> I, I: Copy { pub fn new(n_: usize, op: BiOp, e: I) -> Self { let mut n = 1; while n < n_ { n *= 2; } // n is a power of 2 SegTree {n: n, dat: vec![e; 2 * n - 1], op: op, e: e} } /* ary[k] <- v */ pub fn update(&mut self, idx: usize, v: I) { let mut k = idx + self.n - 1; self.dat[k] = v; while k > 0 { k = (k - 1) / 2; self.dat[k] = (self.op)(self.dat[2 * k + 1], self.dat[2 * k + 2]); } } /* l,r are for simplicity */ fn query_sub(&self, a: usize, b: usize, k: usize, l: usize, r: usize) -> I { // [a,b) and [l,r) intersects? if r <= a || b <= l { return self.e; } if a <= l && r <= b { return self.dat[k]; } let vl = self.query_sub(a, b, 2 * k + 1, l, (l + r) / 2); let vr = self.query_sub(a, b, 2 * k + 2, (l + r) / 2, r); (self.op)(vl, vr) } /* [a, b] (note: inclusive) */ pub fn query(&self, a: usize, b: usize) -> I { self.query_sub(a, b + 1, 0, 0, self.n) } } fn solve(v: &[u32]) -> u64 { // coordinate compression let n = v.len(); let mut uniq = v.to_vec(); uniq.sort(); uniq.dedup(); let mut inv_uniq = HashMap::::new(); let m = uniq.len(); for i in 0 .. m { inv_uniq.insert(uniq[i], i); } let mut st = SegTree::new(m + 2, |x, y| x + y, 0); let mut tot = 0u64; for i in 0 .. n { let w = inv_uniq.get(&v[i]).unwrap() + 1; tot += st.query(0, w) as u64 * st.query(w + 2, m + 1) as u64; let tmp = st.query(w + 1, w + 1); st.update(w + 1, tmp + 1); } tot } fn main() { let n = get(); let mut a: Vec = (0..n).map(|_| get()).collect(); let tot = solve(&a); a.reverse(); let tot = tot + solve(&a); println!("{}", tot); }