結果

問題 No.694 square1001 and Permutation 3
ユーザー tentententen
提出日時 2021-03-10 13:59:47
言語 Java19
(openjdk 21)
結果
AC  
実行時間 2,856 ms / 3,000 ms
コード長 1,910 bytes
コンパイル時間 2,436 ms
コンパイル使用メモリ 75,580 KB
実行使用メモリ 127,676 KB
最終ジャッジ日時 2023-08-02 09:42:22
合計ジャッジ時間 16,237 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,328 KB
testcase_01 AC 42 ms
49,300 KB
testcase_02 AC 42 ms
51,212 KB
testcase_03 AC 49 ms
49,468 KB
testcase_04 AC 55 ms
50,340 KB
testcase_05 AC 58 ms
49,616 KB
testcase_06 AC 57 ms
50,308 KB
testcase_07 AC 440 ms
73,560 KB
testcase_08 AC 1,482 ms
89,400 KB
testcase_09 AC 2,856 ms
124,832 KB
testcase_10 AC 520 ms
73,924 KB
testcase_11 AC 2,665 ms
124,588 KB
testcase_12 AC 2,729 ms
127,676 KB
testcase_13 AC 42 ms
49,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	public static void main (String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		int[] arr = new int[n];
		long ans = 0;
		TreeMap<Integer, Integer> map = new TreeMap<>();
		for (int i = 0; i < n; i++) {
		    arr[i] = Integer.parseInt(br.readLine());
		    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