結果

問題 No.694 square1001 and Permutation 3
ユーザー tentententen
提出日時 2021-03-10 13:57:41
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,801 bytes
コンパイル時間 2,629 ms
コンパイル使用メモリ 79,140 KB
実行使用メモリ 140,412 KB
最終ジャッジ日時 2024-04-20 09:06:32
合計ジャッジ時間 13,943 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 105 ms
53,096 KB
testcase_01 AC 119 ms
52,712 KB
testcase_02 AC 104 ms
53,020 KB
testcase_03 AC 158 ms
54,176 KB
testcase_04 AC 165 ms
54,260 KB
testcase_05 AC 163 ms
54,464 KB
testcase_06 AC 161 ms
54,500 KB
testcase_07 AC 1,185 ms
85,356 KB
testcase_08 AC 2,384 ms
98,204 KB
testcase_09 TLE -
testcase_10 AC 1,161 ms
85,652 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 AC 118 ms
54,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
		int[] counts = new int[idx];
		for (int i = 0; i < idx; i++) {
		    counts[i] = bit.getSum(i);
		}
		sb.append(total).append("\n");
		for (int i = 0; i < n - 1; i++) {
		    total -= counts[map.get(arr[i]) - 1];
		    total += counts[idx - 1] - counts[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;
    }
}
0