結果
| 問題 | 
                            No.1115 二つの数列 / Two Sequences
                             | 
                    
| コンテスト | |
| ユーザー | 
                             Strorkis
                         | 
                    
| 提出日時 | 2020-07-17 22:40:24 | 
| 言語 | Rust  (1.83.0 + proconio)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 13 ms / 2,000 ms | 
| コード長 | 1,448 bytes | 
| コンパイル時間 | 14,211 ms | 
| コンパイル使用メモリ | 401,192 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-11-30 01:37:43 | 
| 合計ジャッジ時間 | 14,845 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 5 | 
| other | AC * 35 | 
ソースコード
struct FenwickTree {
    n: usize,
    v: Vec<usize>,
}
impl FenwickTree {
    fn new(n: usize) -> FenwickTree {
        FenwickTree {
            n,
            v: vec![0; n + 1],
        }
    }
    fn add(&mut self, mut i: usize, x: usize) {
        while i <= self.n {
            self.v[i] += x;
            i += i & (!i + 1);
        }
    }
    fn sum(&self, mut i: usize) -> usize {
        let mut res = 0;
        while i > 0 {
            res += self.v[i];
            i -= i & (!i + 1);
        }
        res
    }
}
fn main() {
    let n: usize = {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        buf.trim_end().parse().unwrap()
    };
    let a: Vec<usize> = {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        let iter = buf.split_whitespace();
        let mut tmp = vec![0; n];
        for (i, x) in iter.map(|x| x.parse::<usize>().unwrap()).enumerate() {
            tmp[x - 1] = i + 1;
        }
        tmp
    };
    let b: Vec<usize> = {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        let iter = buf.split_whitespace();
        iter.map(|x| a[x.parse::<usize>().unwrap() - 1]).collect()
    };
    let mut ans = 0;
    let mut ft = FenwickTree::new(n);
    for i in 0..n {
        ft.add(b[i], 1);
        ans += i + 1 - ft.sum(b[i]);
    }
    println!("{}", ans);
}
            
            
            
        
            
Strorkis