結果
問題 | No.1300 Sum of Inversions |
ユーザー | tenten |
提出日時 | 2023-02-02 09:11:58 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,153 ms / 2,000 ms |
コード長 | 3,519 bytes |
コンパイル時間 | 2,761 ms |
コンパイル使用メモリ | 84,036 KB |
実行使用メモリ | 72,748 KB |
最終ジャッジ日時 | 2024-07-01 23:54:24 |
合計ジャッジ時間 | 35,172 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 52 ms
36,596 KB |
testcase_01 | AC | 51 ms
36,844 KB |
testcase_02 | AC | 54 ms
36,604 KB |
testcase_03 | AC | 942 ms
62,428 KB |
testcase_04 | AC | 937 ms
62,732 KB |
testcase_05 | AC | 743 ms
59,856 KB |
testcase_06 | AC | 1,065 ms
68,876 KB |
testcase_07 | AC | 1,027 ms
67,880 KB |
testcase_08 | AC | 1,152 ms
63,980 KB |
testcase_09 | AC | 1,131 ms
64,300 KB |
testcase_10 | AC | 693 ms
54,336 KB |
testcase_11 | AC | 699 ms
57,284 KB |
testcase_12 | AC | 939 ms
62,724 KB |
testcase_13 | AC | 920 ms
63,780 KB |
testcase_14 | AC | 1,153 ms
66,552 KB |
testcase_15 | AC | 1,093 ms
64,064 KB |
testcase_16 | AC | 1,009 ms
66,552 KB |
testcase_17 | AC | 694 ms
54,952 KB |
testcase_18 | AC | 746 ms
56,172 KB |
testcase_19 | AC | 830 ms
60,404 KB |
testcase_20 | AC | 863 ms
58,928 KB |
testcase_21 | AC | 902 ms
58,564 KB |
testcase_22 | AC | 771 ms
59,976 KB |
testcase_23 | AC | 1,073 ms
69,040 KB |
testcase_24 | AC | 806 ms
61,196 KB |
testcase_25 | AC | 722 ms
55,916 KB |
testcase_26 | AC | 693 ms
55,544 KB |
testcase_27 | AC | 778 ms
60,192 KB |
testcase_28 | AC | 1,145 ms
64,988 KB |
testcase_29 | AC | 904 ms
58,548 KB |
testcase_30 | AC | 1,108 ms
64,188 KB |
testcase_31 | AC | 813 ms
60,640 KB |
testcase_32 | AC | 909 ms
62,852 KB |
testcase_33 | AC | 257 ms
45,756 KB |
testcase_34 | AC | 395 ms
53,312 KB |
testcase_35 | AC | 763 ms
69,192 KB |
testcase_36 | AC | 735 ms
72,748 KB |
ソースコード
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(); } }