結果

問題 No.1282 Display Elements
ユーザー StrorkisStrorkis
提出日時 2020-11-06 22:15:05
言語 Rust
(1.77.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 2,253 bytes
コンパイル時間 1,187 ms
コンパイル使用メモリ 158,384 KB
実行使用メモリ 6,368 KB
最終ジャッジ日時 2023-09-29 18:56:57
合計ジャッジ時間 2,757 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 53 ms
6,204 KB
testcase_16 AC 20 ms
4,376 KB
testcase_17 AC 44 ms
5,468 KB
testcase_18 AC 27 ms
4,376 KB
testcase_19 AC 20 ms
4,924 KB
testcase_20 AC 20 ms
5,072 KB
testcase_21 AC 58 ms
6,348 KB
testcase_22 AC 39 ms
5,064 KB
testcase_23 AC 58 ms
6,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

pub mod procon_input {
    use std::{str::FromStr, iter::FromIterator};

    pub fn read_line() -> String {
        let mut input = String::new();
        std::io::stdin().read_line(&mut input).ok();
        input
    }

    pub fn parse<T: FromStr>(s: &str) -> T {
        s.parse().ok().unwrap()
    }
    
    pub fn read<T: FromStr>() -> T {
        parse(read_line().trim_end())
    }
    
    pub fn read_collection<T: FromStr, C: FromIterator<T>>() -> C {
        read_line().split_whitespace().map(parse).collect()
    }

    #[macro_export]
    macro_rules! read_tuple {
        ( $( $t:ty ),* ) => {{
            let input = read_line();
            let mut iter = input.split_whitespace();
            ( $( parse::<$t>(iter.next().unwrap()) ),* )
        }};
    }
}

struct FenwickTree<T> {
    n: usize,
    init: T,
    data: Vec<T>,
}

impl<T: Copy + std::ops::AddAssign> FenwickTree<T> {
    fn new(n: usize, init: T) -> FenwickTree<T> {
        FenwickTree { n, init, data: vec![init; n] }
    }

    fn add(&mut self, mut p: usize, x: T) {
        p += 1;
        while p <= self.n {
            self.data[p - 1] += x;
            p += p & (!p + 1);
        }
    }

    fn sum(&self, mut r: usize) -> T {
        let mut res = self.init;
        while r > 0 {
            res += self.data[r - 1];
            r -= r & (!r + 1);
        }
        res
    }
}

use procon_input::*;

fn solve(writer: &mut std::io::BufWriter<std::io::StdoutLock>) {
    use std::io::Write;

    let n: usize = read();
    let mut a: Vec<i32> = read_collection();
    a.sort();
    let b: Vec<i32> = read_collection();

    let mut v: Vec<i32> = a.iter().chain(b.iter()).cloned().collect();
    v.sort();
    v.dedup();

    let a: Vec<usize> = {
        a.into_iter().map(|x| v.binary_search(&x).unwrap()).collect()
    };
    let b: Vec<usize> = {
        b.into_iter().map(|x| v.binary_search(&x).unwrap()).collect()
    };

    let mut ans: i64 = 0;
    let mut ft = FenwickTree::new(2 * n, 0);
    for i in 0..n {
        ft.add(b[i], 1);
        ans += ft.sum(a[i]);
    }
    writeln!(writer, "{}", ans).ok();
}

fn main() {
    let stdout = std::io::stdout();
    let mut writer = std::io::BufWriter::new(stdout.lock());
    solve(&mut writer);
}
0