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 { // may return null when result is empty static int[] intersectSortedIntArrays(int[] a, int[] b) { return intersectSortedIntArrays(a, b, null); } static int[] intersectSortedIntArrays(int[] a, int[] b, IntBuffer buf) { int i = 0, j = 0, la = lIntArray(a), lb = lIntArray(b); // swap if a is longer than b if (la > lb) { int[] temp = a; a = b; b = temp; int temp2 = la; la = lb; lb = temp2; } // special case zero elements if (la == 0) return null; // special case one element if (la == 1) return Arrays.binarySearch(b, a[0]) >= 0 ? a : null; if (buf == null) buf = new IntBuffer(); else buf.reset(); intersectSortedIntArrays_recurse(a, b, buf, 0, la, 0, lb); return buf.toArray(); } static void intersectSortedIntArrays_recurse(int[] a, int[] b, 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 = Arrays.binarySearch(b, bFrom, bTo, x); if (j >= 0) { // element found intersectSortedIntArrays_recurse(a, b, buf, aFrom, i, bFrom, j); buf.add(x); intersectSortedIntArrays_recurse(a, b, buf, i+1, aTo, j+1, bTo); } else { j = -j-1; intersectSortedIntArrays_recurse(a, b, buf, aFrom, i, bFrom, j); intersectSortedIntArrays_recurse(a, b, buf, i+1, aTo, j, bTo); } } static int lIntArray(int[] a) { return a == null ? 0 : a.length; } 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); } }