結果

問題 No.404 部分門松列
ユーザー koba-e964koba-e964
提出日時 2017-03-04 01:31:33
言語 Rust
(1.77.0)
結果
RE  
実行時間 -
コード長 2,381 bytes
コンパイル時間 1,873 ms
コンパイル使用メモリ 160,036 KB
実行使用メモリ 8,760 KB
最終ジャッジ日時 2023-09-04 06:37:14
合計ジャッジ時間 11,067 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 6 ms
4,380 KB
testcase_05 AC 6 ms
4,376 KB
testcase_06 AC 5 ms
4,376 KB
testcase_07 AC 6 ms
4,380 KB
testcase_08 AC 6 ms
4,380 KB
testcase_09 AC 5 ms
4,376 KB
testcase_10 AC 6 ms
4,380 KB
testcase_11 AC 6 ms
4,376 KB
testcase_12 AC 6 ms
4,380 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 TLE -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

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() }

fn solve() {
    let n = get();
    assert!(n <= 4000);
    let a: Vec<i64> = (0 .. n).map(|_| get()).collect();
    // Precomputation
    // Counts how many kadomatsu seqs. with the center a[i] can be made.
    // TODO very slow, O(n^2)
    let mut aux: Vec<i64> = vec![0; n];
    for i in 0 .. n {
        let mut cnt = 0;
        for j in 0 .. i {
            if a[j] == a[i] { continue; }
            for k in i + 1 .. n {
                if a[k] == a[i] { continue; }
                if a[j] != a[k] &&
                    ((a[j] < a[i] && a[i] > a[k]) ||
                     (a[j] > a[i] && a[i] < a[k])) {
                        cnt += 1;
                    }
            }
        }
        aux[i] = cnt;
    }
    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