結果
| 問題 | No.1300 Sum of Inversions | 
| コンテスト | |
| ユーザー |  tenten | 
| 提出日時 | 2022-09-01 15:28:37 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,040 ms / 2,000 ms | 
| コード長 | 2,913 bytes | 
| コンパイル時間 | 3,312 ms | 
| コンパイル使用メモリ | 87,652 KB | 
| 実行使用メモリ | 77,080 KB | 
| 最終ジャッジ日時 | 2024-11-14 06:39:34 | 
| 合計ジャッジ時間 | 30,066 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 34 | 
ソースコード
import java.io.*;
import java.util.*;
public class Main {
    static final int MOD = 998244353;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int[] values = new int[n];
        TreeMap<Integer, Integer> compress = new TreeMap<>();
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
            compress.put(values[i], null);
        }
        int idx = 1;
        for (int x : compress.keySet()) {
            compress.put(x, idx++);
        }
        BinaryIndexedTree firstValue = new BinaryIndexedTree(idx);
        BinaryIndexedTree firstCount = new BinaryIndexedTree(idx);
        BinaryIndexedTree secondValue = new BinaryIndexedTree(idx);
        BinaryIndexedTree secondCount = new BinaryIndexedTree(idx);
        long ans = 0;
        for (int i = n - 1; i >= 0; i--) {
            int v = compress.get(values[i]);
            ans += secondValue.getSum(v - 1) + secondCount.getSum(v - 1) * (long)values[i];
            ans %= MOD;
            long add = firstValue.getSum(v - 1) + firstCount.getSum(v - 1) * (long)values[i];
            secondValue.add(v, (int)(add % MOD));
            secondCount.add(v, firstCount.getSum(v - 1));
            firstValue.add(v, values[i]);
            firstCount.add(v, 1);
        }
        System.out.println(ans);
    }
    
}
class BinaryIndexedTree {
    int size;
    int[] tree;
    static final int MOD = 998244353;
    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;
                tree[idx] %= MOD;
                idx += mask;
            }
            mask <<= 1;
        }
    }
    
    public int getSum(int x) {
        int mask = 1;
        int ans = 0;
        while (x > 0) {
            if ((x & mask) != 0) {
                ans += tree[x];
                ans %= MOD;
                x -= mask;
            }
            mask <<= 1;
        }
        return ans;
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
            
            
            
        