結果

問題 No.1209 XOR Into You
ユーザー lavoxlavox
提出日時 2021-10-02 10:40:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 367 ms / 2,000 ms
コード長 3,881 bytes
コンパイル時間 5,191 ms
コンパイル使用メモリ 75,572 KB
実行使用メモリ 81,140 KB
最終ジャッジ日時 2023-09-27 10:13:14
合計ジャッジ時間 14,363 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
49,368 KB
testcase_01 AC 38 ms
49,144 KB
testcase_02 AC 37 ms
49,204 KB
testcase_03 AC 39 ms
49,284 KB
testcase_04 AC 103 ms
52,540 KB
testcase_05 AC 103 ms
52,552 KB
testcase_06 AC 281 ms
79,292 KB
testcase_07 AC 307 ms
73,176 KB
testcase_08 AC 294 ms
70,960 KB
testcase_09 AC 259 ms
70,528 KB
testcase_10 AC 254 ms
66,652 KB
testcase_11 AC 280 ms
70,624 KB
testcase_12 AC 253 ms
68,996 KB
testcase_13 AC 295 ms
72,212 KB
testcase_14 AC 256 ms
67,176 KB
testcase_15 AC 266 ms
67,192 KB
testcase_16 AC 243 ms
67,288 KB
testcase_17 AC 289 ms
68,700 KB
testcase_18 AC 351 ms
79,344 KB
testcase_19 AC 289 ms
67,284 KB
testcase_20 AC 285 ms
72,016 KB
testcase_21 AC 251 ms
68,220 KB
testcase_22 AC 271 ms
79,204 KB
testcase_23 AC 228 ms
79,220 KB
testcase_24 AC 257 ms
81,140 KB
testcase_25 AC 367 ms
79,600 KB
testcase_26 AC 306 ms
79,484 KB
testcase_27 AC 350 ms
79,804 KB
testcase_28 AC 304 ms
79,680 KB
testcase_29 AC 342 ms
79,872 KB
testcase_30 AC 349 ms
80,068 KB
testcase_31 AC 190 ms
57,840 KB
testcase_32 AC 202 ms
59,472 KB
testcase_33 AC 200 ms
57,964 KB
testcase_34 AC 193 ms
57,640 KB
testcase_35 AC 201 ms
57,916 KB
testcase_36 AC 182 ms
57,676 KB
testcase_37 AC 190 ms
57,892 KB
testcase_38 AC 279 ms
72,480 KB
testcase_39 AC 273 ms
69,940 KB
testcase_40 AC 274 ms
79,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.NoSuchElementException;

public class Main {
	public static void main(String[] args) {
		Main o = new Main();
		o.solve();
	}

	public void solve() {
		FastScanner sc = new FastScanner(System.in);
		int N = sc.nextInt();
		int[] A = new int[N];
		int[] B = new int[N];
		for (int i = 0; i < N; i++) {
			A[i] = sc.nextInt();
		}
		for (int i = 0; i < N; i++) {
			B[i] = sc.nextInt();
		}

		if ( A[0] != B[0] || A[N - 1] != B[N - 1] ) {
			System.out.println(-1);
			return;
		}

		int[] XA = new int[N - 1];
		int[] XB = new int[N - 1];
		for (int i = 0; i < N - 1; i++) {
			XA[i] = A[i] ^ A[i + 1];
			XB[i] = B[i] ^ B[i + 1];
		}

		HashMap<Integer, ArrayDeque<Integer>> map = new HashMap<>();
		for (int i = 0; i < N - 1; i++) {
			if ( !map.containsKey(XA[i]) ) {
				map.put(XA[i], new ArrayDeque<>());
			}
			map.get(XA[i]).addLast(i);
		}
		int[] YB = new int[N - 1];
		boolean flg = true;
		for (int i = 0; i < N - 1; i++) {
			if ( map.containsKey(XB[i]) ) {
				ArrayDeque<Integer> que = map.get(XB[i]);
				if ( que.size() > 0 ) {
					YB[i] = que.pollFirst();
				} else {
					flg = false;
					break;
				}
			} else {
				flg = false;
				break;
			}
		}
		if ( !flg ) {
			System.out.println(-1);
			return;
		}

		long ans = 0;
		BinaryIndexedTree tree = new BinaryIndexedTree(N);
		for (int i = 0 ; i < N - 1; i++) {
			ans += (long)i - tree.sum(YB[i] + 1);
			tree.add(YB[i] + 1, 1);
		}

		System.out.println(ans);
	}

	class BinaryIndexedTree {
		// 個数(max + 1)
		int N = 1;
		long[] tree = null;
	
		// 1-indexed
		BinaryIndexedTree(int max) {
			this.N = max + 1;
			tree = new long[N];
		}
	
		// i番目(1-indexed)の要素にaを加える
		void add(int i, long a) {
			if ( i == 0 ) return;
			while ( i < N ) {
				tree[i] += a;
				i += i & -i;
			}
		}
	
		// 1〜i番目(含む)の和
		long sum(int i) {
			long ret = 0;
			if ( i == 0 ) return 0;
			while ( i > 0 ) {
				ret += tree[i];
				i -= (i & -i);
			}
			return ret;
		}
	}
	
	class FastScanner {
		private final InputStream in;
		private final byte[] buf = new byte[1024];
		private int ptr = 0;
		private int buflen = 0;
		FastScanner( InputStream source ) { this.in = source; }
		private boolean hasNextByte() {
			if ( ptr < buflen ) return true;
			else {
				ptr = 0;
				try { buflen = in.read(buf); } catch (IOException e) { e.printStackTrace(); }
				if ( buflen <= 0 ) return false;
			}
			return true;
		} 
		private int readByte() { if ( hasNextByte() ) return buf[ptr++]; else return -1; } 
		private boolean isPrintableChar( int c ) { return 33 <= c && c <= 126; }
		private boolean isNumeric( int c ) { return '0' <= c && c <= '9'; }
		private void skipToNextPrintableChar() { while ( hasNextByte() && !isPrintableChar(buf[ptr]) ) ptr++; }
		public boolean hasNext() { skipToNextPrintableChar(); return hasNextByte(); }
		public String next() {
			if ( !hasNext() ) throw new NoSuchElementException();
			StringBuilder ret = new StringBuilder();
			int b = readByte();
			while ( isPrintableChar(b) ) { ret.appendCodePoint(b); b = readByte(); }
			return ret.toString();
		}
		public long nextLong() {
			if ( !hasNext() ) throw new NoSuchElementException();
			long ret = 0;
			int b = readByte();
			boolean negative = false;
			if ( b == '-' ) { negative = true; if ( hasNextByte() ) b = readByte(); }
			if ( !isNumeric(b) ) throw new NumberFormatException();
			while ( true ) {
				if ( isNumeric(b) ) ret = ret * 10 + b - '0';
				else if ( b == -1 || !isPrintableChar(b) ) return negative ? -ret : ret;
				else throw new NumberFormatException();
				b = readByte();
			}
		}
		public int nextInt() { return (int)nextLong(); }
		public double nextDouble() { return Double.parseDouble(next()); }
	}
}
0