Submission #42564667


Source Code Expand

#![allow(unused)]

fn main() {
    let n = read::<usize>();
    let xs = readv::<u32>();
    let ys = readv::<u32>();
 
    let (xs, _) = compress(&xs);
    let (ys, _) = compress(&ys);
    let mut xy = xs.into_iter().zip(ys.into_iter()).collect::<Vec<_>>();
    xy.sort_by_key(|&(x, y)| (x, std::cmp::Reverse(y)));
 
    let mut bit = BIT::new(n);
    let mut ans = 0;
    let mut i = 0;
    while i < n {
        let mut j = i + 1;
        while j < n && xy[j] == xy[i] {
            j += 1;
        }
 
        // xy[i..j] are same points
        let cnt = j - i;
        let (x, y) = xy[i];
        bit.add(y, cnt);
        ans += bit.sum(y, n) * cnt;
 
        i = j;
    }
 
    println!("{}", ans);
}

fn compress<T: Clone + Ord>(arr: &[T]) -> (Vec<usize>, Vec<T>) {
    let mut s = arr.to_vec();
    s.sort();
    s.dedup();
    let res = arr
        .iter()
        .map(|x| s.binary_search(x).unwrap())
        .collect::<Vec<_>>();
    (res, s)
}

struct BIT<T> {
    dat: Vec<T>,
}

impl<T: Clone + Default + std::ops::AddAssign + std::ops::Sub<Output = T>> BIT<T> {
    fn new(n: usize) -> Self {
        Self {
            dat: vec![T::default(); n + 1],
        }
    }

    // 0-based
    fn add(&mut self, mut i: usize, x: T) {
        i += 1; // convert to 1-based
        while i < self.dat.len() {
            self.dat[i] += x.clone();
            i += i & (!i + 1); // i & (-i)
        }
    }

    // 0..=i, 0-based
    fn pref(&self, mut i: usize) -> T {
        let mut res = T::default();
        i += 1; // convert to 1-based
        while i > 0 {
            res += self.dat[i].clone();
            i -= i & (!i + 1);
        }
        res
    }

    // l..i, 0-based
    fn sum(&self, mut l: usize, mut r: usize) -> T {
        if r == 0 {
            T::default()
        } else if l >= 1 {
            self.pref(r - 1) - self.pref(l - 1)
        } else {
            self.pref(r - 1)
        }
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn readv<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_ascii_whitespace()
        .map(|t| t.parse().ok().unwrap())
        .collect()
}

fn reads() -> Vec<char> {
    read::<String>().chars().collect::<Vec<char>>()
}

fn join<T: ToString>(arr: &[T], sep: &str) -> String {
    arr.iter()
        .map(|x| x.to_string())
        .collect::<Vec<String>>()
        .join(sep)
}

Submission Info

Submission Time
Task F - Jealous Two
User amoshuangyc
Language Rust (1.42.0)
Score 500
Code Size 2598 Byte
Status AC
Exec Time 116 ms
Memory 11684 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 24
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hand_01.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
hand_01.txt AC 5 ms 1948 KiB
random_01.txt AC 115 ms 11684 KiB
random_02.txt AC 58 ms 6512 KiB
random_03.txt AC 115 ms 11552 KiB
random_04.txt AC 92 ms 9672 KiB
random_05.txt AC 116 ms 11632 KiB
random_06.txt AC 37 ms 4728 KiB
random_07.txt AC 81 ms 8676 KiB
random_08.txt AC 61 ms 7252 KiB
random_09.txt AC 75 ms 11616 KiB
random_10.txt AC 27 ms 4676 KiB
random_11.txt AC 46 ms 7444 KiB
random_12.txt AC 46 ms 7756 KiB
random_13.txt AC 104 ms 11536 KiB
random_14.txt AC 78 ms 8864 KiB
random_15.txt AC 93 ms 9800 KiB
random_16.txt AC 22 ms 3764 KiB
random_17.txt AC 53 ms 11272 KiB
random_18.txt AC 32 ms 11228 KiB
random_19.txt AC 54 ms 11012 KiB
random_20.txt AC 55 ms 10920 KiB
sample_01.txt AC 1 ms 2068 KiB
sample_02.txt AC 1 ms 2028 KiB
sample_03.txt AC 1 ms 2120 KiB