Submission #33898939


Source Code Expand

Copy
use std::collections::{BTreeMap, BTreeSet};
use fenwicktree::*;
use proconio::{input, marker::Usize1};
fn f(a: &[usize]) -> usize {
let mut count = 0;
let c = a
.iter()
.copied()
.collect::<BTreeSet<_>>()
.into_iter()
.enumerate()
.fold(BTreeMap::new(), |mut m, (i, k)| {
m.insert(k, i);
m
});
let mut ft = FenwickTree::new(a.len(), 0);
for (i, a_i) in a.iter().enumerate() {
count += i - ft.sum(0, c[a_i] + 1);
ft.add(c[a_i], 1);
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
use std::collections::{BTreeMap, BTreeSet};

use fenwicktree::*;
use proconio::{input, marker::Usize1};

fn f(a: &[usize]) -> usize {
    let mut count = 0;
    let c = a
        .iter()
        .copied()
        .collect::<BTreeSet<_>>()
        .into_iter()
        .enumerate()
        .fold(BTreeMap::new(), |mut m, (i, k)| {
            m.insert(k, i);
            m
        });
    let mut ft = FenwickTree::new(a.len(), 0);
    for (i, a_i) in a.iter().enumerate() {
        count += i - ft.sum(0, c[a_i] + 1);
        ft.add(c[a_i], 1);
    }
    count
}

fn main() {
    input! {
        n: usize,
        c: [Usize1; n],
        x: [Usize1; n],
    };
    let mut d = vec![vec![]; n];
    for (i, c_i) in c.iter().copied().enumerate() {
        d[c_i].push(x[i]);
    }
    let mut ans = f(&x);
    for d_i in d {
        if d_i.is_empty() {
            continue;
        }
        ans -= f(&d_i);
    }
    println!("{}", ans);
}

//https://github.com/rust-lang-ja/ac-library-rs

pub mod fenwicktree {
    // Reference: https://en.wikipedia.org/wiki/Fenwick_tree
    pub struct FenwickTree<T> {
        n: usize,
        ary: Vec<T>,
        e: T,
    }

    impl<T: Clone + std::ops::AddAssign<T>> FenwickTree<T> {
        pub fn new(n: usize, e: T) -> Self {
            FenwickTree {
                n,
                ary: vec![e.clone(); n],
                e,
            }
        }
        pub fn accum(&self, mut idx: usize) -> T {
            let mut sum = self.e.clone();
            while idx > 0 {
                sum += self.ary[idx - 1].clone();
                idx &= idx - 1;
            }
            sum
        }
        /// performs data[idx] += val;
        pub fn add<U: Clone>(&mut self, mut idx: usize, val: U)
        where
            T: std::ops::AddAssign<U>,
        {
            let n = self.n;
            idx += 1;
            while idx <= n {
                self.ary[idx - 1] += val.clone();
                idx += idx & idx.wrapping_neg();
            }
        }
        /// Returns data[l] + ... + data[r - 1].
        pub fn sum(&self, l: usize, r: usize) -> T
        where
            T: std::ops::Sub<Output = T>,
        {
            self.accum(r) - self.accum(l)
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn fenwick_tree_works() {
            let mut bit = FenwickTree::new(5, 0i64);
            // [1, 2, 3, 4, 5]
            for i in 0..5 {
                bit.add(i, i as i64 + 1);
            }
            assert_eq!(bit.sum(0, 5), 15);
            assert_eq!(bit.sum(0, 4), 10);
            assert_eq!(bit.sum(1, 3), 5);
        }
    }
}

Submission Info

Submission Time
Task F - Sorting Color Balls
User bouzuya
Language Rust (1.42.0)
Score 500
Code Size 2657 Byte
Status AC
Exec Time 288 ms
Memory 41472 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 37
Set Name Test Cases
Sample example_00.txt, example_01.txt, example_02.txt
All example_00.txt, example_01.txt, example_02.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, random_00.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, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt
Case Name Status Exec Time Memory
example_00.txt AC 8 ms 2012 KB
example_01.txt AC 1 ms 2084 KB
example_02.txt AC 1 ms 2136 KB
hand_00.txt AC 226 ms 40768 KB
hand_01.txt AC 209 ms 41472 KB
hand_02.txt AC 142 ms 27076 KB
hand_03.txt AC 215 ms 34496 KB
hand_04.txt AC 154 ms 26128 KB
hand_05.txt AC 1 ms 2136 KB
hand_06.txt AC 1 ms 2044 KB
random_00.txt AC 117 ms 23032 KB
random_01.txt AC 111 ms 22572 KB
random_02.txt AC 76 ms 21628 KB
random_03.txt AC 72 ms 20868 KB
random_04.txt AC 71 ms 20240 KB
random_05.txt AC 126 ms 23016 KB
random_06.txt AC 113 ms 21948 KB
random_07.txt AC 100 ms 22188 KB
random_08.txt AC 95 ms 20788 KB
random_09.txt AC 87 ms 20136 KB
random_10.txt AC 159 ms 23204 KB
random_11.txt AC 163 ms 22940 KB
random_12.txt AC 160 ms 21540 KB
random_13.txt AC 149 ms 20928 KB
random_14.txt AC 137 ms 20096 KB
random_15.txt AC 187 ms 24376 KB
random_16.txt AC 216 ms 26840 KB
random_17.txt AC 212 ms 26024 KB
random_18.txt AC 235 ms 23504 KB
random_19.txt AC 260 ms 24036 KB
random_20.txt AC 226 ms 29424 KB
random_21.txt AC 224 ms 28852 KB
random_22.txt AC 230 ms 27272 KB
random_23.txt AC 259 ms 27172 KB
random_24.txt AC 288 ms 26632 KB
random_25.txt AC 226 ms 29540 KB
random_26.txt AC 222 ms 28956 KB


2025-04-11 (Fri)
11:05:21 +00:00