結果

問題 No.1282 Display Elements
ユーザー StrorkisStrorkis
提出日時 2020-11-06 22:13:14
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 2,248 bytes
コンパイル時間 3,391 ms
コンパイル使用メモリ 164,592 KB
実行使用メモリ 5,612 KB
最終ジャッジ日時 2023-09-29 18:55:47
合計ジャッジ時間 3,645 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 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,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 0 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 WA -
testcase_16 AC 18 ms
4,376 KB
testcase_17 AC 40 ms
4,824 KB
testcase_18 AC 24 ms
4,376 KB
testcase_19 AC 17 ms
5,000 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
権限があれば一括ダウンロードができます

ソースコード

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 = 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