結果
| 問題 | No.1209 XOR Into You | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2021-10-02 10:40:28 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 403 ms / 2,000 ms | 
| コード長 | 3,881 bytes | 
| コンパイル時間 | 3,339 ms | 
| コンパイル使用メモリ | 79,236 KB | 
| 実行使用メモリ | 68,136 KB | 
| 最終ジャッジ日時 | 2024-07-20 04:22:46 | 
| 合計ジャッジ時間 | 16,419 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 37 | 
ソースコード
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()); }
	}
}
            
            
            
        