結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー tonyu0tonyu0
提出日時 2020-07-17 22:42:27
言語 Rust
(1.77.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,563 bytes
コンパイル時間 1,996 ms
コンパイル使用メモリ 143,580 KB
実行使用メモリ 5,572 KB
最終ジャッジ日時 2023-08-20 02:24:00
合計ジャッジ時間 4,260 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 12 ms
4,376 KB
testcase_04 AC 15 ms
5,572 KB
testcase_05 AC 12 ms
4,380 KB
testcase_06 AC 11 ms
4,376 KB
testcase_07 AC 15 ms
5,564 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 8 ms
4,380 KB
testcase_10 AC 12 ms
5,500 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 15 ms
5,528 KB
testcase_13 AC 15 ms
5,540 KB
testcase_14 AC 15 ms
5,564 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 6 ms
4,376 KB
testcase_25 AC 11 ms
4,380 KB
testcase_26 AC 3 ms
4,384 KB
testcase_27 AC 6 ms
4,376 KB
testcase_28 AC 8 ms
4,380 KB
testcase_29 AC 11 ms
4,376 KB
testcase_30 AC 14 ms
5,308 KB
testcase_31 AC 4 ms
4,380 KB
testcase_32 AC 3 ms
4,380 KB
testcase_33 AC 10 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::*;

mod fenwick_tree {
    use std::ops::*;
    pub struct FenwickTree<T> {
        size: usize,
        data: Vec<T>,
        identity: T,
    }
    impl<T: Copy + Clone + AddAssign + Sub<Output = T>> FenwickTree<T> {
        pub fn new(size: usize, id: T) -> FenwickTree<T> {
            FenwickTree {
                size: size,
                data: vec![id; size + 2],
                identity: id,
            }
        }
        pub fn query(&self, i: usize) -> T {
            let mut res = self.identity;
            let mut idx = i as isize - 1;
            while idx >= 0 {
                res += self.data[idx as usize];
                idx = (idx & (idx + 1)) - 1;
            }
            res
        }
        pub fn update(&mut self, i: usize, x: T) {
            let mut idx = i;
            while idx <= self.size {
                self.data[idx] += x;
                idx |= idx + 1;
            }
        }
    }
}

fn main() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.trim().split_whitespace();
    let n: usize = itr.next().unwrap().parse().unwrap();

    let mut map = vec![0; n];
    for i in 0..n {
        let a: usize = itr.next().unwrap().parse().unwrap();
        map[a - 1] = i;
    }

    let mut ans = 0;
    let mut ft = fenwick_tree::FenwickTree::new(n, 0);
    for i in 0..n {
        let b: usize = itr.next().unwrap().parse().unwrap();
        ans += i - ft.query(map[b - 1]);
        ft.update(map[b - 1], 1);
    }
    println!("{}", ans);
}
0