Problem:
Score : 700 points

Problem Statement

We will define the median of a sequence b of length M, as follows:

Let b' be the sequence obtained by sorting b in non-decreasing order. Then, the value of the (M / 2 + 1)-th element of b' is the median of b. Here, / is integer division, rounding down.

For example, the median of (10, 30, 20) is 20; the median of (10, 30, 20, 40) is 30; the median of (10, 10, 10, 20, 30) is 10.

Snuke comes up with the following problem.

You are given a sequence a of length N.
For each pair (l, r) (1 \leq l \leq r \leq N), let m_{l, r} be the median of the contiguous subsequence (a_l, a_{l + 1}, ..., a_r) of a.
We will list m_{l, r} for all pairs (l, r) to create a new sequence m.
Find the median of m.

Constraints

1 \leq N \leq 10^5

a_i is an integer.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the median of m.

Sample Input 1

3
10 30 20

Sample Output 1

30

The median of each contiguous subsequence of a is as follows:

The median of (10) is 10.

The median of (30) is 30.

The median of (20) is 20.

The median of (10, 30) is 30.

The median of (30, 20) is 30.

The median of (10, 30, 20) is 20.

Thus, m = (10, 30, 20, 30, 30, 20) and the median of m is 30.

Sample Input 2

1
10

Sample Output 2

10

Sample Input 3

10
5 9 5 9 8 9 3 5 4 3

Sample Output 3

8

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

use io::*;
use std::collections::BTreeSet;

fn main() {
    let input = read_string();
    let mut sc = Scanner::new(&input);
    let n: usize = sc.next();
    let v: Vec<usize> = sc.next_vec(n);
    let mut l = 0;
    let mut r = 1000000001;
    while l + 1 < r {
        let m = (l + r) / 2;
        let mut s: Vec<i64> = v.iter().map(|&x| if x < m {-1} else {1}).collect();
        s.insert(0,0);
        for i in 1..n {
            s[i] += s[i-1];
        }
        let com = compress(&s);
        let mut bit = BIT::new(n+1);
        let mut count = 0;
        for x in com {
            count += bit.sum(x) as usize;
            bit.add(x, 1);
        }
        if count >= (n*(n+1)+2)/4 {
            l = m;
        } else {
            r = m;
        }
    }
    println!("{}",l);
}

fn compress(v: &Vec<i64>) -> Vec<usize> {
    let mut s: BTreeSet<i64> = BTreeSet::new();
    for x in v {
        s.insert(*x);
    }
    let w: Vec<i64> = s.into_iter().collect();
    let mut ret: Vec<usize> = vec![0;v.len()];
    for i in 0..v.len() {
        let mut l = 0;
        let mut r = w.len();
        while l + 1 < r {
            let m = (l + r) / 2;
            if w[m] > v[i] {
                r = m;
            } else {
                l = m;
            }
        }
        ret[i] = l;
    }
    ret
}

struct BIT {
    v: Vec<i64>,
    n: usize
}

impl BIT {
    fn new(n: usize) -> BIT {
        BIT{v: vec![0;n+1], n: n}
    }

    fn add(&mut self, i: usize, x: i64) {
        let mut id = i as i64 + 1;
        while id <= self.n as i64 {
            self.v[id as usize] += x;
            id += id & -id;
        }
    }

    fn sum(&self, i: usize) -> i64 {
        let mut id = i as i64 + 1;
        let mut ret = 0;
        while id > 0 {
            ret += self.v[id as usize];
            id &= id - 1;
        }
        ret
    }
}

pub mod io {
    use std;
    use std::str::FromStr;

    pub struct Scanner<'a> {
        iter: std::str::SplitWhitespace<'a>,
    }

    impl<'a> Scanner<'a> {
        pub fn new(s: &'a str) -> Scanner<'a> {
            Scanner {
                iter: s.split_whitespace(),
            }
        }

        pub fn next<T: FromStr>(&mut self) -> T {
            let s = self.iter.next().unwrap();
            if let Ok(v) = s.parse::<T>() {
                v
            } else {
                panic!("Parse error")
            }
        }

        pub fn next_vec_len<T: FromStr>(&mut self) -> Vec<T> {
            let n: usize = self.next();
            self.next_vec(n)
        }

        pub fn next_vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
            (0..n).map(|_| self.next()).collect()
        }

        pub fn next_mat<T: FromStr>(&mut self, w: usize, h: usize) -> Vec<Vec<T>> {
            (0..h).map(|_| self.next_vec(w)).collect()
        }

        pub fn next_vec_char(&mut self) -> Vec<char> {
            self.next::<String>().chars().collect()
        }

        pub fn next_mat_char(&mut self, n: usize) -> Vec<Vec<char>> {
            (0..n).map(|_| self.next_vec_char()).collect()
        }
    }

    pub fn read_string() -> String {
        use std::io::Read;

        let mut s = String::new();
        std::io::stdin().read_to_string(&mut s).unwrap();
        s
    }

    pub fn read_line() -> String {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).unwrap();
        s.trim_right().to_owned()
    }
}