結果

問題 No.1300 Sum of Inversions
ユーザー phsplsphspls
提出日時 2022-11-25 01:23:06
言語 Rust
(1.59.0)
結果
AC  
実行時間 312 ms / 2,000 ms
コード長 3,152 Byte
コンパイル時間 1,061 ms
使用メモリ 30,700 KB
最終ジャッジ日時 2022-11-25 01:23:25
合計ジャッジ時間 12,069 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 1 ms
4,900 KB
testcase_01 AC 1 ms
6,948 KB
testcase_02 AC 1 ms
6,948 KB
testcase_03 AC 252 ms
27,056 KB
testcase_04 AC 234 ms
24,852 KB
testcase_05 AC 207 ms
19,240 KB
testcase_06 AC 270 ms
27,772 KB
testcase_07 AC 259 ms
28,016 KB
testcase_08 AC 300 ms
29,600 KB
testcase_09 AC 304 ms
29,964 KB
testcase_10 AC 174 ms
16,384 KB
testcase_11 AC 150 ms
16,536 KB
testcase_12 AC 227 ms
25,336 KB
testcase_13 AC 226 ms
25,260 KB
testcase_14 AC 307 ms
30,700 KB
testcase_15 AC 312 ms
29,268 KB
testcase_16 AC 276 ms
26,728 KB
testcase_17 AC 153 ms
15,944 KB
testcase_18 AC 165 ms
18,032 KB
testcase_19 AC 185 ms
24,056 KB
testcase_20 AC 198 ms
24,100 KB
testcase_21 AC 194 ms
24,604 KB
testcase_22 AC 175 ms
19,216 KB
testcase_23 AC 273 ms
29,032 KB
testcase_24 AC 189 ms
19,760 KB
testcase_25 AC 173 ms
17,496 KB
testcase_26 AC 151 ms
17,080 KB
testcase_27 AC 176 ms
18,672 KB
testcase_28 AC 290 ms
29,324 KB
testcase_29 AC 201 ms
24,640 KB
testcase_30 AC 298 ms
29,268 KB
testcase_31 AC 191 ms
19,360 KB
testcase_32 AC 214 ms
20,068 KB
testcase_33 AC 162 ms
28,280 KB
testcase_34 AC 167 ms
30,012 KB
testcase_35 AC 164 ms
29,732 KB
testcase_36 AC 167 ms
30,300 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field is never read: `orgn`
 --> Main.rs:7:5
  |
7 |     orgn: usize,
  |     ^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: associated function is never used: `find_closed`
  --> Main.rs:29:8
   |
29 |     fn find_closed(&self, l: usize, r: usize) -> T {
   |        ^^^^^^^^^^^

warning: associated function is never used: `get`
  --> Main.rs:46:8
   |
46 |     fn get(&mut self, pos: usize) -> T {
   |        ^^^

warning: 3 warnings emitted

ソースコード

diff #

const MOD: usize = 998244353;

#[derive(Debug, Clone)]
struct SegmentTree<F, T> {
    n: usize,
    orgn: usize,
    unit: T,
    mem: Vec<T>,
    operator: F
}

impl<F, T> SegmentTree<F, T> where
T: std::fmt::Debug + Copy,
F: Fn(T, T) -> T
{
    fn new(n: usize, unit: T, operator: F) -> Self {
        let mut size = 1usize;
        while size < n { size *= 2; }
        Self {
            n: size,
            orgn: n,
            unit: unit,
            mem: vec![unit; size*2-1],
            operator: operator,
        }
    }

    fn find_closed(&self, l: usize, r: usize) -> T {
        self.find(l, r+1)
    }

    fn find(&self, l: usize, r: usize) -> T {
        self._find(l, r, 0, 0, self.n)
    }

    fn set(&mut self, pos: usize, val: T) {
        let mut pos = pos + self.n - 1;
        self.mem[pos] = val;
        while pos > 0 {
            pos = self._parent(pos);
            self.mem[pos] = (self.operator)(self.mem[self._left(pos)], self.mem[self._right(pos)]);
        }
    }

    fn get(&mut self, pos: usize) -> T {
        self.mem[pos + self.n - 1]
    }

    fn _find(&self, a: usize, b: usize, nodenum: usize, l: usize, r: usize) -> T {
        if r <= a || b <= l {
            return self.unit;
        } else if a <= l && r <= b {
            return self.mem[nodenum];
        }
        let left = self._find(a, b, self._left(nodenum), l, (l+r)/2);
        let right = self._find(a, b, self._right(nodenum), (l+r)/2, r);
        (self.operator)(left, right)
    }
    fn _left(&self, k: usize) -> usize {
        2*k+1
    }
    fn _right(&self, k: usize) -> usize {
        2*k+2
    }
    fn _parent(&self, k: usize) -> usize {
        (k-1)/2
    }
}

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut a = String::new();
    std::io::stdin().read_line(&mut a).ok();
    let a: Vec<usize> = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();

    let orga = a.clone().to_owned();
    let mut maxvalseg = SegmentTree::new(n, 0usize, |x, y| x + y);
    let mut maxcntseg = SegmentTree::new(n, 0usize, |x, y| x + y);
    let mut minvalseg = SegmentTree::new(n, 0usize, |x, y| x + y);
    let mut mincntseg = SegmentTree::new(n, 0usize, |x, y| x + y);
    let mut maxvals = vec![0usize; n];
    let mut minvals = vec![0usize; n];
    let mut maxcnts = vec![0usize; n];
    let mut mincnts = vec![0usize; n];
    let mut a = (0..n).map(|i| (a[i], i)).collect::<Vec<_>>();
    a.sort();
    for &(val, i) in a.iter() {
        minvals[i] = minvalseg.find(i, n);
        mincnts[i] = mincntseg.find(i, n);
        minvalseg.set(i, val);
        mincntseg.set(i, 1);
    }
    a.reverse();
    for &(val, i) in a.iter() {
        maxvals[i] = maxvalseg.find(0, i);
        maxcnts[i] = maxcntseg.find(0, i);
        maxvalseg.set(i, val);
        maxcntseg.set(i, 1);
    }
    let mut result = 0usize;
    for i in 0..n {
        result += maxvals[i] * mincnts[i] % MOD + minvals[i] * maxcnts[i] % MOD + maxcnts[i] * mincnts[i] % MOD * orga[i] % MOD;
        result %= MOD;
    }
    println!("{}", result);
}
0