結果

問題 No.694 square1001 and Permutation 3
ユーザー tentententen
提出日時 2021-03-10 13:59:47
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,910 bytes
コンパイル時間 2,481 ms
コンパイル使用メモリ 78,988 KB
実行使用メモリ 125,520 KB
最終ジャッジ日時 2024-04-20 09:08:03
合計ジャッジ時間 17,407 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
36,916 KB
testcase_01 AC 51 ms
37,184 KB
testcase_02 AC 52 ms
36,860 KB
testcase_03 AC 56 ms
36,888 KB
testcase_04 AC 62 ms
37,148 KB
testcase_05 AC 67 ms
37,412 KB
testcase_06 AC 66 ms
37,184 KB
testcase_07 AC 500 ms
62,944 KB
testcase_08 AC 1,773 ms
76,076 KB
testcase_09 TLE -
testcase_10 AC 567 ms
63,056 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 AC 53 ms
36,884 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