結果
問題 | No.1300 Sum of Inversions |
ユーザー | Strorkis |
提出日時 | 2020-11-28 15:24:24 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 107 ms / 2,000 ms |
コード長 | 4,694 bytes |
コンパイル時間 | 14,476 ms |
コンパイル使用メモリ | 397,872 KB |
実行使用メモリ | 19,464 KB |
最終ジャッジ日時 | 2024-09-12 23:01:16 |
合計ジャッジ時間 | 16,650 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,812 KB |
testcase_02 | AC | 1 ms
6,812 KB |
testcase_03 | AC | 80 ms
15,676 KB |
testcase_04 | AC | 78 ms
15,260 KB |
testcase_05 | AC | 62 ms
12,888 KB |
testcase_06 | AC | 91 ms
17,356 KB |
testcase_07 | AC | 87 ms
16,660 KB |
testcase_08 | AC | 98 ms
18,192 KB |
testcase_09 | AC | 96 ms
18,060 KB |
testcase_10 | AC | 51 ms
11,056 KB |
testcase_11 | AC | 51 ms
11,192 KB |
testcase_12 | AC | 78 ms
15,392 KB |
testcase_13 | AC | 75 ms
14,980 KB |
testcase_14 | AC | 107 ms
19,464 KB |
testcase_15 | AC | 95 ms
17,980 KB |
testcase_16 | AC | 82 ms
16,084 KB |
testcase_17 | AC | 49 ms
10,776 KB |
testcase_18 | AC | 57 ms
12,036 KB |
testcase_19 | AC | 68 ms
13,788 KB |
testcase_20 | AC | 71 ms
14,132 KB |
testcase_21 | AC | 70 ms
14,000 KB |
testcase_22 | AC | 62 ms
12,880 KB |
testcase_23 | AC | 93 ms
17,348 KB |
testcase_24 | AC | 63 ms
13,296 KB |
testcase_25 | AC | 54 ms
11,752 KB |
testcase_26 | AC | 53 ms
11,608 KB |
testcase_27 | AC | 59 ms
12,600 KB |
testcase_28 | AC | 100 ms
18,356 KB |
testcase_29 | AC | 69 ms
13,864 KB |
testcase_30 | AC | 96 ms
18,056 KB |
testcase_31 | AC | 62 ms
13,024 KB |
testcase_32 | AC | 64 ms
13,436 KB |
testcase_33 | AC | 16 ms
12,960 KB |
testcase_34 | AC | 24 ms
12,828 KB |
testcase_35 | AC | 61 ms
19,104 KB |
testcase_36 | AC | 64 ms
19,104 KB |
ソースコード
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); }