import java.util.*; import java.util.zip.*; import java.util.List; import java.util.regex.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; import java.util.concurrent.locks.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import javax.swing.table.*; import java.io.*; import java.net.*; import java.lang.reflect.*; import java.lang.ref.*; import java.lang.management.*; import java.security.*; import java.security.spec.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import java.math.*; class main { // is allowed to return null when result is empty // ofs is added to elements of b prior to comparison static int[] intersectSortedIntArrays_ofs_optimized2(int[] a, int[] b, int ofs) { return intersectSortedIntArrays_ofs_optimized2(a, b, ofs, null); } static int[] intersectSortedIntArrays_ofs_optimized2(int[] a, int[] b, int ofs, IntBuffer buf) { int i = 0, j = 0, la = lIntArray(a), lb = lIntArray(b); // swap if a is longer than b boolean swapped = la > lb; if (swapped) { int[] temp = a; a = b; b = temp; int temp2 = la; la = lb; lb = temp2; ofs = -ofs; } // special case zero elements if (la == 0) return null; // special case one element if (la == 1 && !swapped) return intArrayBinarySearch(b, a[0]-ofs) >= 0 ? a : null; if (buf == null) buf = new IntBuffer(); else buf.reset(); intersectSortedIntArrays_ofs_optimized2_recurse(a, b, ofs, buf, 0, la, 0, lb); int[] out = buf.toArray(); if (swapped) { int n = lIntArray(out); for (int k = 0; k < n; k++) out[k] -= ofs; } return out; } static void intersectSortedIntArrays_ofs_optimized2_recurse(int[] a, int[] b, int ofs, IntBuffer buf, int aFrom, int aTo, int bFrom, int bTo) { if (aFrom >= aTo || bFrom >= bTo) return; // nothing to do // start in the middle of a, search this element in b int i = (aFrom+aTo)/2; int x = a[i]; int j = intArrayBinarySearch(b, bFrom, bTo, x-ofs); if (j >= 0) { // element found intersectSortedIntArrays_ofs_optimized2_recurse(a, b, ofs, buf, aFrom, i, bFrom, j); buf.add(x); intersectSortedIntArrays_ofs_optimized2_recurse(a, b, ofs, buf, i+1, aTo, j+1, bTo); } else { j = -j-1; intersectSortedIntArrays_ofs_optimized2_recurse(a, b, ofs, buf, aFrom, i, bFrom, j); intersectSortedIntArrays_ofs_optimized2_recurse(a, b, ofs, buf, i+1, aTo, j, bTo); } } static int lIntArray(int[] a) { return a == null ? 0 : a.length; } // Like Arrays.binarySearch, but without range checks. static int intArrayBinarySearch(int[] a, int key) { return intArrayBinarySearch(a, 0, a.length, key); } static int intArrayBinarySearch(int[] a, int fromIndex, int toIndex, int key) { int low = fromIndex; int high = toIndex - 1; while (low <= high) { int mid = (low + high) >>> 1; int midVal = a[mid]; if (midVal < key) low = mid + 1; else if (midVal > key) high = mid - 1; else return mid; // key found } return -(low + 1); // key not found. } static class IntBuffer { int[] data; int size; IntBuffer() {} IntBuffer(int size) { if (size != 0) data = new int[size]; } void add(int i) { if (size >= lIntArray(data)) data = resizeIntArray(data, Math.max(1, lIntArray(data)*2)); data[size++] = i; } int[] toArray() { return size == 0 ? null : resizeIntArray(data, size); } void reset() { size = 0; } } static int[] resizeIntArray(int[] a, int n) { if (n == lIntArray(a)) return a; int[] b = new int[n]; arraycopy(a, 0, b, 0, Math.min(lIntArray(a), n)); return b; } static void arraycopy(Object[] a, Object[] b) { if (a != null && b != null) arraycopy(a, 0, b, 0, Math.min(a.length, b.length)); } static void arraycopy(Object src, int srcPos, Object dest, int destPos, int n) { if (n != 0) System.arraycopy(src, srcPos, dest, destPos, n); } }