結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー tentententen
提出日時 2021-12-01 16:27:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 456 ms / 2,000 ms
コード長 2,089 bytes
コンパイル時間 3,132 ms
コンパイル使用メモリ 75,160 KB
実行使用メモリ 55,812 KB
最終ジャッジ日時 2023-09-17 22:53:09
合計ジャッジ時間 12,959 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
37,512 KB
testcase_01 AC 40 ms
33,592 KB
testcase_02 AC 40 ms
33,384 KB
testcase_03 AC 390 ms
49,820 KB
testcase_04 AC 428 ms
55,792 KB
testcase_05 AC 383 ms
49,316 KB
testcase_06 AC 369 ms
48,476 KB
testcase_07 AC 393 ms
50,480 KB
testcase_08 AC 97 ms
36,800 KB
testcase_09 AC 281 ms
45,708 KB
testcase_10 AC 348 ms
54,908 KB
testcase_11 AC 39 ms
33,728 KB
testcase_12 AC 445 ms
55,812 KB
testcase_13 AC 456 ms
55,568 KB
testcase_14 AC 453 ms
55,720 KB
testcase_15 AC 39 ms
33,680 KB
testcase_16 AC 39 ms
33,412 KB
testcase_17 AC 40 ms
33,468 KB
testcase_18 AC 41 ms
33,712 KB
testcase_19 AC 41 ms
33,508 KB
testcase_20 AC 40 ms
33,396 KB
testcase_21 AC 40 ms
33,556 KB
testcase_22 AC 39 ms
33,704 KB
testcase_23 AC 126 ms
37,444 KB
testcase_24 AC 239 ms
44,164 KB
testcase_25 AC 332 ms
48,788 KB
testcase_26 AC 168 ms
41,108 KB
testcase_27 AC 271 ms
45,300 KB
testcase_28 AC 311 ms
46,644 KB
testcase_29 AC 370 ms
48,832 KB
testcase_30 AC 422 ms
50,124 KB
testcase_31 AC 195 ms
42,076 KB
testcase_32 AC 152 ms
41,056 KB
testcase_33 AC 357 ms
49,944 KB
testcase_34 AC 45 ms
47,828 KB
testcase_35 AC 40 ms
33,544 KB
testcase_36 AC 40 ms
33,508 KB
testcase_37 AC 41 ms
33,420 KB
testcase_38 AC 40 ms
33,772 KB
testcase_39 AC 41 ms
33,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = n; i >= 1; i--) {
            map.put(sc.nextInt(), i);
        }
        BinaryIndexedTree bit = new BinaryIndexedTree(n + 1);
        long ans = 0;
        for (int i = 0; i < n; i++) {
            int idx = map.get(sc.nextInt());
            ans += bit.getSum(idx);
            bit.add(idx, 1);
        }
        System.out.println(ans);
    }
}
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;
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0