結果

問題 No.1300 Sum of Inversions
ユーザー tentententen
提出日時 2023-02-02 09:11:58
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,225 ms / 2,000 ms
コード長 3,519 bytes
コンパイル時間 3,325 ms
コンパイル使用メモリ 81,148 KB
実行使用メモリ 86,564 KB
最終ジャッジ日時 2023-09-14 16:47:23
合計ジャッジ時間 37,669 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,640 KB
testcase_01 AC 45 ms
49,644 KB
testcase_02 AC 45 ms
49,328 KB
testcase_03 AC 991 ms
74,396 KB
testcase_04 AC 972 ms
73,872 KB
testcase_05 AC 815 ms
72,684 KB
testcase_06 AC 1,115 ms
79,620 KB
testcase_07 AC 1,076 ms
78,844 KB
testcase_08 AC 1,163 ms
76,688 KB
testcase_09 AC 1,156 ms
77,108 KB
testcase_10 AC 690 ms
65,456 KB
testcase_11 AC 694 ms
68,256 KB
testcase_12 AC 990 ms
74,024 KB
testcase_13 AC 940 ms
73,328 KB
testcase_14 AC 1,225 ms
77,696 KB
testcase_15 AC 1,132 ms
76,952 KB
testcase_16 AC 994 ms
74,024 KB
testcase_17 AC 681 ms
65,080 KB
testcase_18 AC 770 ms
67,020 KB
testcase_19 AC 897 ms
72,752 KB
testcase_20 AC 890 ms
70,108 KB
testcase_21 AC 900 ms
70,068 KB
testcase_22 AC 823 ms
72,760 KB
testcase_23 AC 1,128 ms
79,952 KB
testcase_24 AC 844 ms
72,976 KB
testcase_25 AC 733 ms
66,636 KB
testcase_26 AC 712 ms
66,576 KB
testcase_27 AC 808 ms
72,508 KB
testcase_28 AC 1,185 ms
79,116 KB
testcase_29 AC 900 ms
69,780 KB
testcase_30 AC 1,183 ms
76,720 KB
testcase_31 AC 840 ms
72,660 KB
testcase_32 AC 869 ms
70,128 KB
testcase_33 AC 267 ms
56,604 KB
testcase_34 AC 347 ms
62,188 KB
testcase_35 AC 720 ms
78,452 KB
testcase_36 AC 713 ms
86,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static final int MOD = 998244353;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int[] values = new int[n];
        TreeMap<Integer, Integer> compress = new TreeMap<>();
        for (int i = 0; i < n; i++) {
            values[i] = -sc.nextInt();
            compress.put(values[i], null);
        }
        int size = 1;
        for (int x : compress.keySet()) {
            compress.put(x, size++);
        }
        BinaryIndexedTree firstValue = new BinaryIndexedTree(size);
        BinaryIndexedTree firstCount = new BinaryIndexedTree(size);
        BinaryIndexedTree secondValue = new BinaryIndexedTree(size);
        BinaryIndexedTree secondCount = new BinaryIndexedTree(size);
        long ans = 0;
        for (int x : values) {
            int idx = compress.get(x);
            int y = -x;
            ans += secondValue.getSum(idx - 1) + y * secondCount.getSum(idx - 1) % MOD;
            ans %= MOD;
            secondCount.add(idx, firstCount.getSum(idx - 1));
            secondValue.add(idx, (firstValue.getSum(idx - 1) + y * firstCount.getSum(idx - 1) % MOD) % MOD);
            firstCount.add(idx, 1);
            firstValue.add(idx, y);
        }
        System.out.println(ans);
    }
}

class BinaryIndexedTree {
    static final int MOD = 998244353;
    int size;
    long[] tree;
    
    public BinaryIndexedTree(int size) {
        this.size = size;
        tree = new long[size];
    }
    
    public void add(int idx, long value) {
        int mask = 1;
        while (idx < size) {
            if ((idx & mask) != 0) {
                tree[idx] += value;
                tree[idx] %= MOD;
                idx += mask;
            }
            mask <<= 1;
        }
    }
    
    public long getSum(int from, int to) {
        return (getSum(to) - getSum(from - 1) + MOD) % MOD;
    }
    
    public long getSum(int x) {
        int mask = 1;
        long ans = 0;
        while (x > 0) {
            if ((x & mask) != 0) {
                ans += tree[x];
                ans %= MOD;
                x -= mask;
            }
            mask <<= 1;
        }
        return ans;
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0