結果

問題 No.1209 XOR Into You
ユーザー tentententen
提出日時 2022-08-31 18:34:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 714 ms / 2,000 ms
コード長 2,938 bytes
コンパイル時間 6,305 ms
コンパイル使用メモリ 78,716 KB
実行使用メモリ 77,508 KB
最終ジャッジ日時 2024-04-26 00:12:31
合計ジャッジ時間 29,790 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
37,284 KB
testcase_01 AC 52 ms
37,248 KB
testcase_02 AC 54 ms
37,032 KB
testcase_03 AC 51 ms
37,016 KB
testcase_04 AC 490 ms
66,984 KB
testcase_05 AC 714 ms
77,508 KB
testcase_06 AC 461 ms
67,168 KB
testcase_07 AC 622 ms
71,828 KB
testcase_08 AC 547 ms
65,140 KB
testcase_09 AC 577 ms
65,848 KB
testcase_10 AC 458 ms
62,852 KB
testcase_11 AC 477 ms
64,880 KB
testcase_12 AC 473 ms
64,060 KB
testcase_13 AC 537 ms
67,416 KB
testcase_14 AC 487 ms
63,392 KB
testcase_15 AC 501 ms
63,908 KB
testcase_16 AC 490 ms
63,660 KB
testcase_17 AC 488 ms
63,884 KB
testcase_18 AC 671 ms
74,792 KB
testcase_19 AC 480 ms
63,124 KB
testcase_20 AC 543 ms
66,040 KB
testcase_21 AC 480 ms
62,788 KB
testcase_22 AC 489 ms
76,644 KB
testcase_23 AC 502 ms
76,684 KB
testcase_24 AC 552 ms
76,624 KB
testcase_25 AC 644 ms
76,648 KB
testcase_26 AC 663 ms
76,636 KB
testcase_27 AC 650 ms
76,724 KB
testcase_28 AC 659 ms
76,656 KB
testcase_29 AC 632 ms
76,648 KB
testcase_30 AC 670 ms
76,532 KB
testcase_31 AC 394 ms
51,332 KB
testcase_32 AC 398 ms
51,340 KB
testcase_33 AC 442 ms
51,424 KB
testcase_34 AC 383 ms
54,296 KB
testcase_35 AC 389 ms
51,328 KB
testcase_36 AC 392 ms
51,312 KB
testcase_37 AC 374 ms
53,160 KB
testcase_38 AC 579 ms
71,108 KB
testcase_39 AC 461 ms
63,888 KB
testcase_40 AC 497 ms
76,612 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[] valuesA = new int[n];
        HashMap<Integer, ArrayDeque<Integer>> idxes = new HashMap<>();
        for (int i = 0; i < n; i++) {
            valuesA[i] = sc.nextInt();
            if (i > 0) {
                int xor = valuesA[i] ^ valuesA[i - 1];
                if (!idxes.containsKey(xor)) {
                    idxes.put(xor, new ArrayDeque<>());
                }
                idxes.get(xor).add(i);
            }
        }
        BinaryIndexedTree bit = new BinaryIndexedTree(n + 1);
        long ans = 0;
        int[] valuesB = new int[n];
        for (int i = 0; i < n; i++) {
            valuesB[i] = sc.nextInt();
            if (i > 0) {
                int xor = valuesB[i] ^ valuesB[i - 1];
                if (!idxes.containsKey(xor) || idxes.get(xor).size() == 0) {
                    System.out.println(-1);
                    return;
                }
                int v = idxes.get(xor).poll();
                ans += i - 1 - bit.getSum(v);
                bit.add(v, 1);
            }
        }
        if (valuesA[0] != valuesB[0] || valuesA[n - 1] != valuesB[n - 1]) {
            System.out.println(-1);
            return;
        }
        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("");
    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();
    }
    
}
0