結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー sca1lsca1l
提出日時 2020-07-19 14:15:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 742 ms / 2,000 ms
コード長 2,073 bytes
コンパイル時間 2,700 ms
コンパイル使用メモリ 78,156 KB
実行使用メモリ 53,224 KB
最終ジャッジ日時 2024-05-09 15:35:39
合計ジャッジ時間 17,258 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 210 ms
47,440 KB
testcase_01 AC 103 ms
41,016 KB
testcase_02 AC 103 ms
40,704 KB
testcase_03 AC 698 ms
52,012 KB
testcase_04 AC 742 ms
53,144 KB
testcase_05 AC 556 ms
52,012 KB
testcase_06 AC 538 ms
52,068 KB
testcase_07 AC 583 ms
53,212 KB
testcase_08 AC 186 ms
46,172 KB
testcase_09 AC 386 ms
48,748 KB
testcase_10 AC 519 ms
52,584 KB
testcase_11 AC 101 ms
41,248 KB
testcase_12 AC 593 ms
52,976 KB
testcase_13 AC 584 ms
53,224 KB
testcase_14 AC 616 ms
52,596 KB
testcase_15 AC 103 ms
41,136 KB
testcase_16 AC 91 ms
40,152 KB
testcase_17 AC 101 ms
40,908 KB
testcase_18 AC 106 ms
41,120 KB
testcase_19 AC 109 ms
41,104 KB
testcase_20 AC 109 ms
41,220 KB
testcase_21 AC 96 ms
40,004 KB
testcase_22 AC 102 ms
40,804 KB
testcase_23 AC 217 ms
48,016 KB
testcase_24 AC 426 ms
48,808 KB
testcase_25 AC 535 ms
50,952 KB
testcase_26 AC 296 ms
48,400 KB
testcase_27 AC 462 ms
48,948 KB
testcase_28 AC 449 ms
48,872 KB
testcase_29 AC 457 ms
51,852 KB
testcase_30 AC 590 ms
53,112 KB
testcase_31 AC 335 ms
48,932 KB
testcase_32 AC 265 ms
46,420 KB
testcase_33 AC 496 ms
51,840 KB
testcase_34 AC 109 ms
40,960 KB
testcase_35 AC 105 ms
40,804 KB
testcase_36 AC 98 ms
40,792 KB
testcase_37 AC 102 ms
40,968 KB
testcase_38 AC 104 ms
41,132 KB
testcase_39 AC 106 ms
41,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main{
    
    static final int MOD = (int)1e9+7;
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int[] a = new int[n];
        for(int i=0; i<n; i++){
            a[i] = Integer.parseInt(sc.next());
        }
        int[] b = new int[n+1];
        for(int i=0; i<n; i++){
            int idx = Integer.parseInt(sc.next());
            b[idx] = i;
        }
        
        int[] c = new int[n];
        for(int i=0; i<n; i++){
            c[i] = b[a[i]]+1;
        }
        
        Inversion inv = new Inversion();
        c = inv.shrink(c);
        long ans = inv.inv(c);
        
        System.out.println(ans);
    }
}

class Inversion{
    public long inv(int[] a){
        long cnt = 0;
        int size = a.length;
        BinaryIndexedTree bit = new BinaryIndexedTree(size);
        
        for(int i=0; i<size; i++){
            int p = a[i];
            bit.add(p, 1);
            cnt += i + 1 - bit.sum(p);
        }
        return cnt;
    }
    
    //座圧(1-indexed)
    //使うとよい
    public static int[] shrink(int[] a){
        int n = a.length;
        long[] b = new long[n];
        for (int i = 0; i < n; i++){
            b[i] = ((long)a[i] << 32) + i;
        }
        Arrays.sort(b);
        int[] ret = new int[n];
        int p = 1;//1-indexed
        for (int i = 0; i < n; i++){
            if (i > 0 && (b[i]>>32) != (b[i - 1]>>32)){
                p++;
            }
            ret[(int)b[i]] = p;
        }
        return ret;
    }
}

class BinaryIndexedTree{
    int size;
    long[] tree;
    
    public BinaryIndexedTree(int n){
        this.size = n;
        this.tree = new long[n+1];
    }
    
    public long sum(int i){
        long s = 0L;
        while(0<i){
            s += tree[i];
            i -= i & -i;
        }
        return s;
    }
    
    public void add(int i, int x){
        while(i<=size){
            tree[i] += x;
            i += i & -i;
        }
    }
}

0