結果
問題 | No.694 square1001 and Permutation 3 |
ユーザー | tenten |
提出日時 | 2021-03-10 12:57:13 |
言語 | Java21 (openjdk 21) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,711 bytes |
コンパイル時間 | 2,578 ms |
コンパイル使用メモリ | 79,660 KB |
実行使用メモリ | 120,096 KB |
最終ジャッジ日時 | 2024-04-20 08:27:02 |
合計ジャッジ時間 | 15,288 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 135 ms
46,712 KB |
testcase_01 | AC | 137 ms
41,336 KB |
testcase_02 | AC | 139 ms
41,264 KB |
testcase_03 | AC | 192 ms
41,804 KB |
testcase_04 | AC | 197 ms
41,992 KB |
testcase_05 | AC | 206 ms
41,796 KB |
testcase_06 | AC | 196 ms
42,052 KB |
testcase_07 | AC | 1,441 ms
75,836 KB |
testcase_08 | TLE | - |
testcase_09 | TLE | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
ソースコード
import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; long ans = 0; TreeMap<Integer, Integer> map = new TreeMap<>(); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); map.put(arr[i], 0); } int idx = 1; for (int x : map.keySet()) { map.put(x, idx); idx++; } BinaryIndexedTree bit = new BinaryIndexedTree(idx); long total = 0; for (int i = 0; i < n; i++) { total += bit.getSum(idx - 1) - bit.getSum(map.get(arr[i])); bit.add(map.get(arr[i]), 1); } StringBuilder sb = new StringBuilder(); sb.append(total).append("\n"); for (int i = 0; i < n - 1; i++) { total -= bit.getSum(map.get(arr[i]) - 1); total += bit.getSum(idx - 1) - bit.getSum(map.get(arr[i])); sb.append(total).append("\n"); } System.out.print(sb); } } class BinaryIndexedTree { int size; int[] tree; 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; idx += mask; } mask <<= 1; } } public int getSum(int from, int to) { return getSum(to) - getSum(from - 1); } public int getSum(int x) { int mask = 1; int ans = 0; while (x > 0) { if ((x & mask) != 0) { ans += tree[x]; x -= mask; } mask <<= 1; } return ans; } }