結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー tentententen
提出日時 2021-12-01 16:29:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 319 ms / 2,000 ms
コード長 2,070 bytes
コンパイル時間 2,192 ms
コンパイル使用メモリ 74,136 KB
実行使用メモリ 59,592 KB
最終ジャッジ日時 2023-09-17 22:55:48
合計ジャッジ時間 10,277 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 109 ms
52,684 KB
testcase_01 AC 44 ms
49,300 KB
testcase_02 AC 45 ms
49,456 KB
testcase_03 AC 290 ms
56,772 KB
testcase_04 AC 314 ms
58,768 KB
testcase_05 AC 305 ms
58,752 KB
testcase_06 AC 271 ms
56,616 KB
testcase_07 AC 289 ms
56,812 KB
testcase_08 AC 96 ms
52,008 KB
testcase_09 AC 227 ms
56,344 KB
testcase_10 AC 302 ms
58,812 KB
testcase_11 AC 46 ms
49,456 KB
testcase_12 AC 318 ms
56,924 KB
testcase_13 AC 319 ms
59,592 KB
testcase_14 AC 307 ms
58,956 KB
testcase_15 AC 44 ms
49,456 KB
testcase_16 AC 45 ms
49,660 KB
testcase_17 AC 45 ms
49,608 KB
testcase_18 AC 45 ms
49,392 KB
testcase_19 AC 46 ms
49,388 KB
testcase_20 AC 44 ms
49,464 KB
testcase_21 AC 44 ms
49,360 KB
testcase_22 AC 44 ms
49,420 KB
testcase_23 AC 109 ms
52,156 KB
testcase_24 AC 191 ms
56,468 KB
testcase_25 AC 255 ms
56,464 KB
testcase_26 AC 137 ms
54,636 KB
testcase_27 AC 214 ms
55,996 KB
testcase_28 AC 243 ms
56,536 KB
testcase_29 AC 291 ms
56,892 KB
testcase_30 AC 315 ms
56,636 KB
testcase_31 AC 159 ms
56,332 KB
testcase_32 AC 132 ms
54,380 KB
testcase_33 AC 271 ms
58,404 KB
testcase_34 AC 45 ms
49,548 KB
testcase_35 AC 44 ms
49,360 KB
testcase_36 AC 43 ms
49,476 KB
testcase_37 AC 44 ms
49,436 KB
testcase_38 AC 43 ms
49,416 KB
testcase_39 AC 44 ms
49,412 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();
        int[] orders = new int[n + 1];
        for (int i = n; i >= 1; i--) {
            orders[sc.nextInt()] = i;
        }
        BinaryIndexedTree bit = new BinaryIndexedTree(n + 1);
        long ans = 0;
        for (int i = 0; i < n; i++) {
            int idx = orders[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