結果

問題 No.696 square1001 and Permutation 5
コンテスト
ユーザー しらっ亭
提出日時 2018-06-08 23:51:19
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 9,722 ms / 10,000 ms
コード長 1,373 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,864 ms
コンパイル使用メモリ 86,528 KB
実行使用メモリ 92,216 KB
最終ジャッジ日時 2026-03-20 10:23:03
合計ジャッジ時間 39,515 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import java.math.BigInteger;
import java.util.Scanner;

public class Sol {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] A = new int[n];
        for (int i = 0; i < n; i++) {
            A[i] = scanner.nextInt();
        }

        BinaryIndexedTree ft = new BinaryIndexedTree(n);

        long[] factSum = new long[n];
        factSum[1]++;

        for (int i = 0; i < n - 1; i++) {
            int a = A[i];
            int cnt = a-1-ft.sum(a);
            int len = n - i;
            factSum[len-1] += cnt;
            ft.add(a, 1);
        }

        BigInteger ans = BigInteger.ZERO;
        BigInteger fact = BigInteger.ONE;
        for (int i = 1; i < n; i++) {
            fact = fact.multiply(BigInteger.valueOf(i));
            ans = ans.add(fact.multiply(BigInteger.valueOf(factSum[i])));
        }

        System.out.println(ans);
    }
}

class BinaryIndexedTree {
    private int n;
    private int[] val;

    public BinaryIndexedTree(int n) {
        this.n = n;
        val = new int[n];
    }

    public void add(int i, int x) {
        for (; i < n; i |= i + 1) val[i] += x;
    }

    int sum(int i) {
        int s = 0;
        --i;
        if (i >= n) i = n - 1;
        for (; i >= 0; i = (i & (i + 1)) - 1) s += val[i];
        return s;
    }
}
0