結果

問題 No.404 部分門松列
ユーザー koba-e964koba-e964
提出日時 2017-03-04 03:08:33
言語 Rust
(1.77.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 6,006 bytes
コンパイル時間 1,725 ms
コンパイル使用メモリ 188,792 KB
実行使用メモリ 22,516 KB
最終ジャッジ日時 2023-11-01 16:12:35
合計ジャッジ時間 26,234 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 732 ms
9,968 KB
testcase_14 AC 708 ms
12,340 KB
testcase_15 AC 655 ms
10,272 KB
testcase_16 AC 1,155 ms
19,332 KB
testcase_17 AC 1,223 ms
13,116 KB
testcase_18 AC 384 ms
4,348 KB
testcase_19 AC 411 ms
6,528 KB
testcase_20 AC 1,017 ms
21,864 KB
testcase_21 AC 1,235 ms
18,712 KB
testcase_22 AC 466 ms
4,348 KB
testcase_23 AC 1,124 ms
22,504 KB
testcase_24 AC 1,431 ms
22,500 KB
testcase_25 TLE -
testcase_26 AC 1,862 ms
22,456 KB
testcase_27 AC 307 ms
4,348 KB
testcase_28 AC 748 ms
6,192 KB
testcase_29 AC 1,471 ms
18,600 KB
testcase_30 AC 791 ms
10,428 KB
testcase_31 AC 158 ms
4,348 KB
testcase_32 AC 1,235 ms
17,432 KB
testcase_33 AC 1,018 ms
10,584 KB
testcase_34 AC 994 ms
12,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::Read;
#[allow(dead_code)]
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok().unwrap();
    ret
}
fn get_word() -> String {
    let mut stdin = std::io::stdin();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

/**
 * 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<I, BiOp> {
    n: usize,
    dat: Vec<I>,
    op: BiOp,
    e: I,
}

impl<I, BiOp> SegTree<I, BiOp>
    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)
    }
}



/// Coordinate compression
/// Returns a vector of usize, with i-th element the "rank" of a[i] in a.
/// The property forall i. inv_map[ret[i]] == a[i] holds.
fn coord_compress<T: Ord + std::fmt::Debug>(a: &[T])
                                            -> (Vec<usize>, Vec<&T>) {
    let n = a.len();
    let mut cp: Vec<(&T, usize)> = (0 .. n).map(|i| (&a[i], i)).collect();
    cp.sort();
    let mut inv_map = Vec::new();
    let mut prev: Option<&T> = None;
    let mut ret = vec![0; n];
    let mut cnt = 0;
    for (v, i) in cp {
        if prev == Some(v) {
            ret[i] = cnt - 1;
            continue;
        }
        ret[i] = cnt;
        inv_map.push(v);
        prev = Some(v);
        cnt += 1;
    }
    for i in 0 .. n {
        assert_eq!(*inv_map[ret[i]], a[i]);
    }
    (ret, inv_map)
}


fn calc_three_steps(a: &[usize]) -> Vec<i64> {
    let n = a.len();
    // Shifted by 1 (right) to avoid subtraction underflow
    let mut st = SegTree::new(n + 1, |x, y| x + y, 0);
    let mut st_sq = SegTree::new(n + 1, |x, y| x + y, 0);
    let mut ret = vec![0; n];
    for i in 0 .. n {
        let tmp = st.query(a[i] + 1, a[i] + 1) + 1;
        let stsum = st.query(1, a[i]);
        ret[i] = (stsum * stsum - st_sq.query(1, a[i])) / 2;
        st.update(a[i] + 1, tmp);
        st_sq.update(a[i] + 1, tmp * tmp);
    }
    ret
}
fn calc_three_any(a: &[usize]) -> Vec<i64> {
    let n = a.len();
    // Shifted by 1 (right) to avoid subtraction underflow
    let mut st = SegTree::new(n + 1, |x, y| x + y, 0);
    let mut st_sq = SegTree::new(n + 1, |x, y| x + y, 0);
    let mut ret = vec![0; n];
    for i in 0 .. n {
        let tmp = st.query(a[i] + 1, a[i] + 1) + 1;
        st.update(a[i] + 1, tmp);
        st_sq.update(a[i] + 1, tmp * tmp);
    }
    for i in 0 .. n {
        let stsum = st.query(1, a[i]);
        ret[i] = (stsum * stsum - st_sq.query(1, a[i])) / 2;
    }
    ret
}

// Finds #{(j, k) | j < i < k, a[j] < a[i] > a[k], a[j] != a[k]} for every i. 
fn calc_max_aux(a: &[i64]) -> Vec<i64> {
    let n = a.len();
    let (mut a, _) = coord_compress(a);
    let three = calc_three_steps(&a);
    let mut ret = calc_three_any(&a);
    a.reverse();
    let three_rev = calc_three_steps(&a);
    for i in 0 .. n {
        ret[i] += -three[i] - three_rev[n - 1 - i];
    }
    ret
}

// Precomputation
// Counts how many kadomatsu seqs. with the center a[i] can be made.
// O(n * log(n))
fn calc_aux(a: &[i64]) -> Vec<i64> {
    let n = a.len();
    let mut aux: Vec<i64> = calc_max_aux(a);
    let mut a = a.to_vec();
    for v in a.iter_mut() {
        *v *= -1;
    }
    let res2 = calc_max_aux(&a);
    for i in 0 .. n {
        aux[i] += res2[i];
    }
    aux
}


fn solve() {
    let n = get();
    let a: Vec<i64> = (0 .. n).map(|_| get()).collect();
    let aux = calc_aux(&a);
    const INF: i64 = 1 << 60;
    let mut acc = vec![(0, 0); n];
    for i in 0 .. n {
        acc[i] = (a[i], aux[i]);
    }
    acc.push((-INF, 0));
    acc.sort();
    for i in 0 .. n + 1 {
        acc[i].1 += if i == 0 { 0 } else { acc[i - 1].1 };
    }
    
    let q = get();
    for _ in 0 .. q {
        let l: i64 = get();
        let h: i64 = get();
        let upper = acc.binary_search(&(h, INF)).unwrap_err();
        let lower = acc.binary_search(&(l, -INF)).unwrap_err();
        println!("{}", acc[upper - 1].1 - acc[lower - 1].1);
    }
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}
0