import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class worldCodeSprint12 { static class interval{ int start; int end; char animal; public interval(int start, int end, char animal){ this.start = start; this.end = end; this.animal = animal; } } static int[] minimumZooNumbers(int m, int n, char[] t, int[] s, int[] d) { // Return a list of length n consisting of the answers int catEle = 0; int dogMic = 0; interval[] totalStart = new interval[n]; for(int i = 0; i < t.length; i++ ) { totalStart[i] = new interval(s[i], d[i], t[i]); if(t[i] == 'E' || t[i] == 'C') catEle += 1; else if(t[i] == 'D' || t[i] == 'M') dogMic += 1; } interval[] catEleS = new interval[catEle]; interval[] dogMicS = new interval[dogMic]; interval[] catEleD = new interval[catEle]; interval[] dogMicD = new interval[dogMic]; int[] finalResult = new int[n]; for(int i = 0; i < finalResult.length; i++ ) { finalResult[i] = -1; } int pos1 = 0; int pos2 = 0; Arrays.sort(totalStart, new Comparator() { public int compare(interval i1, interval i2) { return i1.start - i2.start; } }); for(interval tmpI : totalStart) { if(tmpI.animal == 'E' || tmpI.animal == 'C') { catEleS[pos1] = tmpI; catEleD[pos1] = tmpI; pos1++; }else { dogMicS[pos2] = tmpI; dogMicD[pos2] = tmpI; pos2++; } } /* Arrays.sort(catEleS, new Comparator() { public int compare(interval i1, interval i2) { return i1.start - i2.start; } });*/ Arrays.sort(catEleD, new Comparator() { public int compare(interval i1, interval i2) { return i1.end - i2.end; } }); /* Arrays.sort(dogMicS, new Comparator() { public int compare(interval i1, interval i2) { return i1.start - i2.start; } });*/ Arrays.sort(dogMicD, new Comparator() { public int compare(interval i1, interval i2) { return i1.end - i2.end; } }); // Up to here, we got all the required info for the scheduling. // The main logic here need to follow the start time order. HashMap lookup = new HashMap<>(); pos1 = -1; pos2 = -1; for(interval currI : totalStart) { int count = -1; if(currI.animal == 'E' || currI.animal == 'C') { pos1++; int ind1 = getPreccessor(dogMicD, currI.start); // Find the not conflict intervals if(ind1 == -1) { // No such dogMic interval exist. if(pos1 > 0) { count = lookup.get(catEleS[pos1 - 1]) + 1; lookup.put(currI, count); } else { count = 1; lookup.put(currI, count); } }else { // Can find dogMic interval. if(pos1 > 0) { count = 1 + Math.max(lookup.get(dogMicD[ind1]), lookup.get(catEleS[pos1 - 1])); lookup.put(currI, count); } else { count = lookup.get(dogMicD[ind1]) + 1; lookup.put(currI, count); } } }else { pos2++; int ind1 = getPreccessor(catEleD, currI.start); // Find the not conflict intervals. if(ind1 == -1) { // No such catEle conflicting interval exists. if(pos2 > 0) { count = lookup.get(dogMicS[pos1 - 1]) + 1; lookup.put(currI, count); } else { count = 1; lookup.put(currI, count); } }else { // Can find catEle interval. if(pos2 > 0) { count = 1 + Math.max(lookup.get(dogMicS[pos2 - 1]), lookup.get(catEleD[ind1])); lookup.put(currI, count); } else { count = lookup.get(catEleD[ind1]) + 1; lookup.put(currI, count); } } } if(finalResult[count - 1] == -1) finalResult[count - 1] = currI.end; else if(finalResult[count - 1] > currI.end){ finalResult[count - 1] = currI.end; } } return finalResult; } /* private static int getSuccessor(interval[] array, int time) { return -1; }*/ private static int getPreccessor(interval[] array, int time) { // Note here, as for interval conflict, the end actually terminate in end - 1. // The general idea here is to do binary search. if(time < array[0].end) return -1; int start = 0; int end = array.length - 1; while(start < end - 1) { int mid = start + (end - start)/2; if(array[mid].end - 1 < time) start = mid; else end = mid; } if(array[end].end - 1 < time) return end; return start; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int cases = in.nextInt(); for(int a0 = 0; a0 < cases; a0++){ int m = in.nextInt(); int n = in.nextInt(); char[] t = new char[n]; for(int t_i = 0; t_i < n; t_i++){ t[t_i] = in.next().charAt(0); } int[] s = new int[n]; for(int s_i = 0; s_i < n; s_i++){ s[s_i] = in.nextInt(); } int[] d = new int[n]; for(int d_i = 0; d_i < n; d_i++){ d[d_i] = in.nextInt(); } int[] result = minimumZooNumbers(m, n, t, s, d); for (int i = 0; i < result.length; i++) { System.out.print(result[i] + (i != result.length - 1 ? " " : "")); } System.out.println(""); } in.close(); } }