import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static int[] minimumZooNumbers(int m, int n, List[] type, List[] start) { SegRangeAddMax seg0 = new SegRangeAddMax(m); SegRangeAddMax seg1 = new SegRangeAddMax(m); int[] res0 = new int[m]; //supposed to be that si != di for(int i=1; i ty = type[i]; List st = start[i]; for(int j=0; j=1; i--){ if(res0[i] - 1 >= 0) res[res0[i]-1] = i+1; } int prev = -1; for(int i=n-1; i>=0; i--){ if(res[i] == -1) res[i] = prev; prev = res[i]; } //System.out.println(Arrays.toString(res)); return res; //List // Return a list of length n consisting of the answers //return new int[1]; } public static void main(String[] args) { FasterScanner in = new FasterScanner(System.in); int cases = in.nextInt(); for(int a0 = 0; a0 < cases; a0++){ int m = in.nextInt(); int n = in.nextInt(); List[] type = new ArrayList[m]; List[] start = new ArrayList[m]; //stupid one-indexed elements -_- for(int i=0; i(); start[i] = new ArrayList(); } for(int i=0; i= d[d_i]) throw new RuntimeException(); //^ sanity check. Huge troll if they decide to put this in. //^ though I will say that I can cope w/ it quite easily in case they do troll. //^ just don't push to your lists when this condition fails //^ todo later: implement that since dunno if full test suite runs later. } for(int i=0; i= d[i]) continue; type[d[i]].add(t[i] == 'D' || t[i] == 'M'); start[d[i]].add(s[i]); } int[] result = minimumZooNumbers(m, n, type, start); for (int i = 0; i < result.length; i++) { System.out.print(result[i] + (i != result.length - 1 ? " " : "")); } System.out.println(""); } //int[] testA = new int[]{1, 3, 5, 4, 0}; //SegRangeAddMax test = new SegRangeAddMax(testA); //System.out.println(test.query(3,3)); } } //DUNNO IF THIS WORKS YET. gonna test on hr: animal transport class SegRangeAddMax{ int[] t, d; int n, h; SegRangeAddMax(int[] base){ this(base.length); for(int i=0; i0; i--) t[i] = t[i<<1] + t[i<<1|1]; } SegRangeAddMax(int n){ this.n = n; h = 32 - Integer.numberOfLeadingZeros(n); t = new int[2*n]; d = new int[n]; } //k is the width of the interval denoted by p void apply(int p, int val){ t[p] += val; if(p < n) d[p] += val; } void calc(int p){ t[p] = Math.max(t[p<<1],t[p<<1|1]) + d[p]; } //before and after the build() function is called, //invariant that t[p] == t[p] of both children + d[p]*k, holds. < ignore this. void build(int p){ //int k = 2; for(p+=n; p>1;){ p >>= 1; calc(p); } } //its obvious that invariant t[p] == t[p] of both children + d[p]*k holds during a push. < ignore this. void push(int p){ //int k = 1 << (h-1); int s = h; for(p+=n; s>0; s--){ int i = p >> s; if(d[i] != 0){ apply(i<<1, d[i]); apply(i<<1|1, d[i]); d[i] = 0; } } } void modify(int l, int r, int val){ r++; if(val == 0) return; //push(l); //push(r-1); int l0=l, r0=r; for(l+=n, r+=n; l>= 1, r >>= 1){ if((l&1) == 1) apply(l++, val); if((r&1) == 1) apply(--r, val); } build(l0); build(r0-1); } int query(int l, int r){ r++; push(l); push(r-1); int res = Integer.MIN_VALUE; for(l+=n, r+=n; l>= 1, r >>= 1){ if((l&1) == 1) res = Math.max(res, t[l++]); if((r&1) == 1) res = Math.max(res, t[--r]); } return res; } } class FasterScanner { private InputStream mIs; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FasterScanner() { this(System.in); } public FasterScanner(InputStream is) { mIs = is; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = mIs.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } //nextString public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } }