結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー sca1lsca1l
提出日時 2020-07-19 14:15:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 618 ms / 2,000 ms
コード長 2,073 bytes
コンパイル時間 4,423 ms
コンパイル使用メモリ 79,780 KB
実行使用メモリ 67,088 KB
最終ジャッジ日時 2023-08-22 09:57:05
合計ジャッジ時間 17,659 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 250 ms
60,820 KB
testcase_01 AC 121 ms
55,792 KB
testcase_02 AC 126 ms
55,928 KB
testcase_03 AC 574 ms
65,496 KB
testcase_04 AC 615 ms
66,136 KB
testcase_05 AC 570 ms
65,352 KB
testcase_06 AC 551 ms
62,356 KB
testcase_07 AC 611 ms
65,428 KB
testcase_08 AC 213 ms
59,240 KB
testcase_09 AC 450 ms
60,812 KB
testcase_10 AC 565 ms
64,636 KB
testcase_11 AC 120 ms
55,444 KB
testcase_12 AC 618 ms
67,088 KB
testcase_13 AC 616 ms
66,276 KB
testcase_14 AC 614 ms
66,676 KB
testcase_15 AC 120 ms
56,004 KB
testcase_16 AC 124 ms
55,444 KB
testcase_17 AC 122 ms
55,640 KB
testcase_18 AC 129 ms
55,760 KB
testcase_19 AC 130 ms
55,572 KB
testcase_20 AC 126 ms
55,724 KB
testcase_21 AC 121 ms
55,764 KB
testcase_22 AC 123 ms
55,976 KB
testcase_23 AC 257 ms
60,628 KB
testcase_24 AC 412 ms
60,700 KB
testcase_25 AC 545 ms
64,412 KB
testcase_26 AC 308 ms
60,516 KB
testcase_27 AC 447 ms
60,852 KB
testcase_28 AC 495 ms
62,596 KB
testcase_29 AC 553 ms
64,296 KB
testcase_30 AC 595 ms
65,884 KB
testcase_31 AC 367 ms
61,944 KB
testcase_32 AC 295 ms
60,132 KB
testcase_33 AC 543 ms
64,064 KB
testcase_34 AC 123 ms
55,808 KB
testcase_35 AC 119 ms
55,712 KB
testcase_36 AC 119 ms
55,720 KB
testcase_37 AC 121 ms
55,984 KB
testcase_38 AC 120 ms
56,012 KB
testcase_39 AC 129 ms
56,112 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