結果

問題 No.1300 Sum of Inversions
ユーザー StrorkisStrorkis
提出日時 2020-11-28 15:24:24
言語 Rust
(1.77.0)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 4,694 bytes
コンパイル時間 1,304 ms
コンパイル使用メモリ 156,984 KB
実行使用メモリ 19,532 KB
最終ジャッジ日時 2023-10-10 00:31:23
合計ジャッジ時間 6,213 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 0 ms
4,352 KB
testcase_03 AC 80 ms
15,796 KB
testcase_04 AC 77 ms
15,228 KB
testcase_05 AC 61 ms
12,888 KB
testcase_06 AC 86 ms
17,520 KB
testcase_07 AC 85 ms
16,564 KB
testcase_08 AC 95 ms
18,096 KB
testcase_09 AC 93 ms
18,008 KB
testcase_10 AC 48 ms
11,100 KB
testcase_11 AC 50 ms
11,164 KB
testcase_12 AC 78 ms
15,408 KB
testcase_13 AC 77 ms
15,192 KB
testcase_14 AC 105 ms
19,532 KB
testcase_15 AC 96 ms
18,084 KB
testcase_16 AC 86 ms
16,068 KB
testcase_17 AC 49 ms
10,880 KB
testcase_18 AC 55 ms
12,044 KB
testcase_19 AC 71 ms
13,768 KB
testcase_20 AC 68 ms
13,968 KB
testcase_21 AC 66 ms
14,068 KB
testcase_22 AC 57 ms
12,816 KB
testcase_23 AC 87 ms
17,144 KB
testcase_24 AC 62 ms
13,216 KB
testcase_25 AC 53 ms
11,740 KB
testcase_26 AC 54 ms
11,668 KB
testcase_27 AC 57 ms
12,560 KB
testcase_28 AC 94 ms
18,676 KB
testcase_29 AC 66 ms
14,044 KB
testcase_30 AC 99 ms
18,004 KB
testcase_31 AC 63 ms
12,924 KB
testcase_32 AC 62 ms
13,460 KB
testcase_33 AC 15 ms
12,952 KB
testcase_34 AC 23 ms
12,964 KB
testcase_35 AC 64 ms
18,944 KB
testcase_36 AC 65 ms
19,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::prelude::*;

fn input<R: BufRead>(stdin: &mut R) -> String {
    let mut input = String::new();
    stdin.read_line(&mut input).ok();
    input
}

macro_rules! read {
    ($stdin:expr, [$t:tt; $n:expr]) => {
        (0..$n).map(|_| read!($stdin, $t)).collect::<Vec<_>>()
    };
    ($stdin:expr, [$t:tt]) => {
        input($stdin).split_whitespace().map(|s| parse!(s, $t)).collect::<Vec<_>>()    
    };
    ($stdin:expr, ($($t:tt),*)) => {{
        let input = input($stdin);
        let mut iter = input.split_whitespace();    
        ($(parse!(iter.next().unwrap(), $t)),*)    
    }};
    ($stdin:expr, $t:tt) => {
        parse!(input($stdin).trim(), $t)
    };
}

macro_rules! parse {
    ($s:expr, Chars) => ($s.chars().collect::<Vec<_>>());
    ($s:expr, Usize1) => (parse!($s, usize) - 1);
    ($s:expr, $t:ty) => ($s.parse::<$t>().unwrap());
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
struct ModInt(i64);

impl ModInt {
    const MOD: i64 = 998_244_353;

    fn new(x: i64) -> ModInt {
        Self(x % Self::MOD)
    }
}

impl std::ops::Add for ModInt {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        let x = self.0 + other.0;
        if x < Self::MOD { Self(x) } else { Self(x - Self::MOD) }
    }
}

impl std::ops::AddAssign for ModInt {
    fn add_assign(&mut self, other: Self) {
        *self = *self + other;
    }
}

impl std::ops::Sub for ModInt {
    type Output = Self;

    fn sub(self, other: Self) -> Self {
        if self.0 < other.0 {
            Self(self.0 + Self::MOD - other.0)
        } else {
            Self(self.0 - other.0)
        }
    }
}

impl std::ops::SubAssign for ModInt {
    fn sub_assign(&mut self, other: Self) {
        *self = *self - other;
    }
}

impl std::ops::Mul for ModInt {
    type Output = Self;

    fn mul(self, other: Self) -> Self {
        Self::new(self.0 * other.0)
    }
}

impl std::ops::MulAssign for ModInt {
    fn mul_assign(&mut self, other: Self) {
        *self = *self * other;
    }
}

impl std::fmt::Display for ModInt {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl std::str::FromStr for ModInt {
    type Err = std::num::ParseIntError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let x = s.parse()?;
        Ok(ModInt::new(x))
    }
}

struct FenwickTree<T> {
    n: usize,
    init: T,
    data: Vec<T>,
}

impl<T: Copy + std::ops::AddAssign> FenwickTree<T> {
    fn new(n: usize, init: T) -> FenwickTree<T> {
        FenwickTree { n, init, data: vec![init; n] }
    }

    fn add(&mut self, mut p: usize, x: T) {
        p += 1;
        while p <= self.n {
            self.data[p - 1] += x;
            p += p & (!p + 1);
        }
    }

    fn sum(&self, mut r: usize) -> T {
        let mut res = self.init;
        while r > 0 {
            res += self.data[r - 1];
            r -= r & (!r + 1);
        }
        res
    }
}

fn solve<R: BufRead, W: Write>(stdin: &mut R, writer: &mut W) {
    let n = read!(stdin, usize);
    let a = read!(stdin, [i64]);

    let mut dedup_a = a.clone();
    dedup_a.sort();
    dedup_a.dedup();
    let m = dedup_a.len();
    let compressed_a: Vec<_> = {
        (0..n).map(|i| dedup_a.binary_search(&a[i]).unwrap()).collect()
    };

    let mut left_count = vec![ModInt(0); n];
    let mut right_count = vec![ModInt(0); n];
    let mut left_sum = vec![ModInt(0); n];
    let mut right_sum = vec![ModInt(0); n];

    let mut count_ft = FenwickTree::new(m, ModInt(0));
    let mut sum_ft = FenwickTree::new(m, ModInt(0));
    for i in 0..n {
        let p = m - 1 - compressed_a[i];
        left_count[i] = count_ft.sum(p);
        left_sum[i] = sum_ft.sum(p);
        count_ft.add(p, ModInt(1));
        sum_ft.add(p, ModInt::new(a[i]));
    }

    let mut count_ft = FenwickTree::new(m, ModInt(0));
    let mut sum_ft = FenwickTree::new(m, ModInt(0));
    for i in (0..n).rev() {
        let p = compressed_a[i];
        right_count[i] = count_ft.sum(p);
        right_sum[i] = sum_ft.sum(p);
        count_ft.add(p, ModInt(1));
        sum_ft.add(p, ModInt::new(a[i]));
    }

    let mut ans = ModInt(0);
    for i in 0..n {
        if left_count[i] * right_count[i] == ModInt(0) { continue; }
        ans += left_count[i] * right_sum[i];
        ans += right_count[i] * left_sum[i];
        ans += ModInt::new(a[i]) * left_count[i] * right_count[i];
    }
    writeln!(writer, "{}", ans).ok();
}

fn main() {
    let stdin = std::io::stdin();
    let stdin = &mut stdin.lock();

    let stdout = std::io::stdout();
    let writer = &mut std::io::BufWriter::new(stdout.lock());

    solve(stdin, writer);
}
0