結果
問題 | No.1300 Sum of Inversions |
ユーザー | tenten |
提出日時 | 2022-09-01 15:28:37 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 972 ms / 2,000 ms |
コード長 | 2,913 bytes |
コンパイル時間 | 2,332 ms |
コンパイル使用メモリ | 85,880 KB |
実行使用メモリ | 67,648 KB |
最終ジャッジ日時 | 2024-04-26 17:10:40 |
合計ジャッジ時間 | 30,284 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 50 ms
37,064 KB |
testcase_01 | AC | 52 ms
36,804 KB |
testcase_02 | AC | 51 ms
37,244 KB |
testcase_03 | AC | 803 ms
62,668 KB |
testcase_04 | AC | 829 ms
62,764 KB |
testcase_05 | AC | 679 ms
56,260 KB |
testcase_06 | AC | 858 ms
62,800 KB |
testcase_07 | AC | 858 ms
63,092 KB |
testcase_08 | AC | 948 ms
63,920 KB |
testcase_09 | AC | 933 ms
63,868 KB |
testcase_10 | AC | 635 ms
54,288 KB |
testcase_11 | AC | 645 ms
54,552 KB |
testcase_12 | AC | 798 ms
63,124 KB |
testcase_13 | AC | 747 ms
62,464 KB |
testcase_14 | AC | 972 ms
64,236 KB |
testcase_15 | AC | 946 ms
64,260 KB |
testcase_16 | AC | 825 ms
62,464 KB |
testcase_17 | AC | 611 ms
54,852 KB |
testcase_18 | AC | 655 ms
55,448 KB |
testcase_19 | AC | 735 ms
59,596 KB |
testcase_20 | AC | 733 ms
59,712 KB |
testcase_21 | AC | 729 ms
60,020 KB |
testcase_22 | AC | 684 ms
56,308 KB |
testcase_23 | AC | 877 ms
63,228 KB |
testcase_24 | AC | 696 ms
57,336 KB |
testcase_25 | AC | 652 ms
55,320 KB |
testcase_26 | AC | 654 ms
56,056 KB |
testcase_27 | AC | 626 ms
55,772 KB |
testcase_28 | AC | 951 ms
64,268 KB |
testcase_29 | AC | 766 ms
59,548 KB |
testcase_30 | AC | 948 ms
63,784 KB |
testcase_31 | AC | 675 ms
56,584 KB |
testcase_32 | AC | 742 ms
58,652 KB |
testcase_33 | AC | 265 ms
45,892 KB |
testcase_34 | AC | 361 ms
54,400 KB |
testcase_35 | AC | 676 ms
67,648 KB |
testcase_36 | AC | 702 ms
64,360 KB |
ソースコード
import java.io.*; import java.util.*; 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 idx = 1; for (int x : compress.keySet()) { compress.put(x, idx++); } BinaryIndexedTree firstValue = new BinaryIndexedTree(idx); BinaryIndexedTree firstCount = new BinaryIndexedTree(idx); BinaryIndexedTree secondValue = new BinaryIndexedTree(idx); BinaryIndexedTree secondCount = new BinaryIndexedTree(idx); long ans = 0; for (int i = n - 1; i >= 0; i--) { int v = compress.get(values[i]); ans += secondValue.getSum(v - 1) + secondCount.getSum(v - 1) * (long)values[i]; ans %= MOD; long add = firstValue.getSum(v - 1) + firstCount.getSum(v - 1) * (long)values[i]; secondValue.add(v, (int)(add % MOD)); secondCount.add(v, firstCount.getSum(v - 1)); firstValue.add(v, values[i]); firstCount.add(v, 1); } System.out.println(ans); } } class BinaryIndexedTree { int size; int[] tree; static final int MOD = 998244353; public BinaryIndexedTree(int size) { this.size = size; tree = new int[size]; } public void add(int idx, int value) { int mask = 1; while (idx < size) { if ((idx & mask) != 0) { tree[idx] += value; tree[idx] %= MOD; idx += mask; } mask <<= 1; } } public int getSum(int x) { int mask = 1; int ans = 0; while (x > 0) { if ((x & mask) != 0) { ans += tree[x]; ans %= MOD; x -= mask; } mask <<= 1; } return ans; } } 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 String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }