結果

問題 No.1209 XOR Into You
ユーザー ikdikd
提出日時 2020-09-02 22:41:50
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 150 ms / 2,000 ms
コード長 4,472 bytes
コンパイル時間 14,926 ms
コンパイル使用メモリ 378,532 KB
実行使用メモリ 15,232 KB
最終ジャッジ日時 2024-05-01 18:06:48
合計ジャッジ時間 19,984 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 53 ms
5,376 KB
testcase_05 AC 52 ms
5,376 KB
testcase_06 AC 70 ms
6,864 KB
testcase_07 AC 129 ms
12,856 KB
testcase_08 AC 117 ms
11,872 KB
testcase_09 AC 117 ms
11,772 KB
testcase_10 AC 93 ms
10,096 KB
testcase_11 AC 114 ms
11,732 KB
testcase_12 AC 109 ms
11,008 KB
testcase_13 AC 120 ms
12,404 KB
testcase_14 AC 100 ms
10,812 KB
testcase_15 AC 106 ms
11,360 KB
testcase_16 AC 106 ms
11,044 KB
testcase_17 AC 100 ms
10,752 KB
testcase_18 AC 150 ms
14,620 KB
testcase_19 AC 106 ms
11,168 KB
testcase_20 AC 122 ms
12,444 KB
testcase_21 AC 89 ms
9,856 KB
testcase_22 AC 85 ms
15,232 KB
testcase_23 AC 84 ms
15,204 KB
testcase_24 AC 87 ms
15,232 KB
testcase_25 AC 140 ms
15,188 KB
testcase_26 AC 143 ms
15,016 KB
testcase_27 AC 144 ms
15,180 KB
testcase_28 AC 143 ms
15,180 KB
testcase_29 AC 138 ms
15,180 KB
testcase_30 AC 144 ms
15,180 KB
testcase_31 AC 98 ms
8,532 KB
testcase_32 AC 96 ms
8,524 KB
testcase_33 AC 96 ms
8,656 KB
testcase_34 AC 99 ms
8,532 KB
testcase_35 AC 95 ms
8,528 KB
testcase_36 AC 97 ms
8,652 KB
testcase_37 AC 66 ms
8,704 KB
testcase_38 AC 114 ms
12,844 KB
testcase_39 AC 98 ms
11,840 KB
testcase_40 AC 85 ms
15,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

pub struct ProconReader<R: std::io::Read> {
    reader: R,
}

impl<R: std::io::Read> ProconReader<R> {
    pub fn new(reader: R) -> Self {
        Self { reader }
    }
    pub fn get<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .reader
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&byte| byte == b' ' || byte == b'\n' || byte == b'\r')
            .take_while(|&byte| byte != b' ' && byte != b'\n' && byte != b'\r')
            .collect::<Vec<_>>();
        std::str::from_utf8(&buf)
            .unwrap()
            .parse()
            .ok()
            .expect("Parse Error.")
    }
}

pub struct FenwickTree {
    n: usize,
    dat: Vec<i64>,
}

impl FenwickTree {
    pub fn new(n: usize) -> Self {
        let n = n.next_power_of_two();
        Self {
            n,
            dat: vec![0; n + 1],
        }
    }
    pub fn add(&mut self, k: usize, x: i64) {
        assert!(1 <= k && k <= self.n);
        let n = self.n as i32;
        let mut k = k as i32;
        while k <= n {
            self.dat[k as usize] += x;
            k += k & (-k);
        }
    }
    pub fn _sum(&self, r: usize) -> i64 {
        assert!(r <= self.n);
        let mut result = 0;
        let mut k = r as i32;
        while k >= 1 {
            result += self.dat[k as usize];
            k -= k & (-k);
        }
        result
    }
    pub fn sum(&self, l: usize, r: usize) -> i64 {
        if l > r {
            return 0;
        }
        assert!(1 <= l && l <= self.n);
        assert!(1 <= r && r <= self.n);
        self._sum(r) - self._sum(l - 1)
    }
}

mod binary_search {
    use std::ops::Range;
    pub trait BinarySearch<T> {
        fn lower_bound(&self, x: &T) -> usize;
        fn upper_bound(&self, x: &T) -> usize;
        fn split_by(&self, x: &T) -> (Range<usize>, Range<usize>, Range<usize>);
    }

    // min index self[i] >= x
    // that is, any j (j < i) holds self[j] < x
    impl<T: Ord> BinarySearch<T> for [T] {
        fn lower_bound(&self, x: &T) -> usize {
            if self[0] >= *x {
                return 0;
            }
            let mut lf = 0;
            let mut rg = self.len();
            // self[lf] < x
            while rg - lf > 1 {
                let md = (rg + lf) / 2;
                if self[md] < *x {
                    lf = md;
                } else {
                    rg = md;
                }
            }
            rg
        }

        // min index self[i] > x
        // that is, any j (j < i) holds self[j] <= x
        fn upper_bound(&self, x: &T) -> usize {
            if self[0] > *x {
                return 0;
            }
            let mut lf = 0;
            let mut rg = self.len();
            // self[lf] <= x
            while rg - lf > 1 {
                let md = (rg + lf) / 2;
                if self[md] <= *x {
                    lf = md;
                } else {
                    rg = md;
                }
            }
            rg
        }

        fn split_by(&self, x: &T) -> (Range<usize>, Range<usize>, Range<usize>) {
            let i = self.lower_bound(x);
            let j = self.upper_bound(x);
            (0..i, i..j, j..self.len())
        }
    }
}

use binary_search::BinarySearch;

fn main() {
    let stdin = std::io::stdin();
    let mut rd = ProconReader::new(stdin.lock());

    let n: usize = rd.get();
    let a: Vec<i64> = (0..n).map(|_| rd.get()).collect();
    let b: Vec<i64> = (0..n).map(|_| rd.get()).collect();

    if a[0] != b[0] || a[n - 1] != b[n - 1] {
        println!("-1");
        return;
    }

    let a = a.windows(2).map(|w| w[0] ^ w[1]).collect::<Vec<_>>();
    let b = b.windows(2).map(|w| w[0] ^ w[1]).collect::<Vec<_>>();

    let mut c = a.clone();
    c.sort();
    let mut d = b.clone();
    d.sort();
    if c.iter().zip(&d).any(|(cc, dd)| cc != dd) {
        println!("-1");
        return;
    }
    c.dedup();
    let idx = |x: i64| c.lower_bound(&x);
    let m = c.len();
    let mut ques = vec![std::collections::VecDeque::new(); m];
    for (i, &x) in a.iter().enumerate() {
        ques[idx(x)].push_back(i);
    }
    let mut fen = FenwickTree::new(n - 1);
    for i in 1..=(n - 1) {
        fen.add(i, 1);
    }
    let mut ans = 0;
    for x in b {
        let i = ques[idx(x)].pop_front().unwrap();
        ans += fen.sum(1, i);
        fen.add(i + 1, -1);
    }
    println!("{}", ans);
}
0