#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include #include #include #include #include #include #include // Site: www.hackerrank.com // Competition: World Code Sprint 12 // Problem: Animal Transport // Problem Code: Problem 5 // by lboris /* */ typedef unsigned int uint; typedef unsigned long long llu; typedef long long int lls; #define f(i,s,e) for(int i=s;i<(int)(e);++i) #define fe(i,s,e) for(int i=s;i<=(int)(e);++i) typedef std::pair ipair; typedef std::vector ipvector; typedef std::vector ivector; int N,M; ivector A1[50001]; ivector A2[50001]; ivector aT; ivector aS; ivector aD; ivector Res; ivector C1,C1a; ivector C2,C2a; int C0,C0a; struct value { int mxv; int incr; }; int bst_max(value* t, int v, int tl, int tr, int l, int r) { if (tl > tr) return 0; if (tl == l && tr == r) { return t[v].mxv + t[v].incr; } int tm = (tl + tr) >> 1; int v2 = v << 1; if (r <= tm) { return bst_max(t, v2, tl, tm, l, r) + t[v].incr;; } else if (l > tm) { return bst_max(t, v2 + 1, tm + 1, tr, l, r) + t[v].incr;; } else { int m1 = bst_max(t, v2, tl, tm, l, tm); int m2 = bst_max(t, v2 + 1, tm + 1, tr, tm + 1, r); return std::max(m1,m2)+t[v].incr; } } int bst_get(value* t, int v, int tl, int tr, int idx) { if (tl > tr) return 0; if (tl == idx && tr == idx) { return t[v].mxv + t[v].incr; } int tm = (tl + tr) >> 1; int v2 = v << 1; if (idx <= tm) { return t[v].incr + bst_get(t, v2, tl, tm, idx); } else { return t[v].incr + bst_get(t, v2 + 1, tm + 1, tr, idx); } } void bst_incr(value* t, int v, int tl, int tr, int l, int r, int val) { if (tl > tr) return; if (tl == l && tr == r) { t[v].incr+=val; return; } int tm = (tl + tr) >> 1; int v2 = v << 1; if (t[v].incr>0) { bst_incr(t, v2, tl, tm, tl, tm, t[v].incr); bst_incr(t, v2 + 1, tm + 1, tr, tm + 1, tr, t[v].incr); t[v].incr = 0; } if (r <= tm) { bst_incr(t, v2, tl, tm, l, r, val); } else if (l > tm) { bst_incr(t, v2 + 1, tm + 1, tr, l, r,val); } else { bst_incr(t, v2, tl, tm, l, tm,val); bst_incr(t, v2 + 1, tm + 1, tr, tm + 1, r,val); } t[v].mxv = std::max(t[v2].mxv + t[v2].incr, t[v2 + 1].mxv + t[v2 + 1].incr); } void bst_set(value* t, int v, int tl, int tr, int idx, int val) { if (tl > tr) return; if (tl == idx && tr == idx) { t[v].mxv = val; t[v].incr = 0; return; } int tm = (tl + tr) >> 1; int v2 = v << 1; if (t[v].incr>0) { bst_incr(t, v2, tl, tm, tl, tm, t[v].incr); bst_incr(t, v2 + 1, tm + 1, tr, tm + 1, tr, t[v].incr); t[v].incr = 0; } if (idx <= tm) { bst_set(t, v2, tl, tm, idx, val); } else { bst_set(t, v2 + 1, tm + 1, tr, idx, val); } t[v].mxv = std::max(t[v2].mxv + t[v2].incr, t[v2 + 1].mxv + t[v2 + 1].incr); } void solve() { value * T1 = new value[4 * (M + 1)]; value * T2 = new value[4 * (M + 1)]; memset(T1, 0, sizeof(value)*(M + 1) * 4); memset(T2, 0, sizeof(value)*(M + 1) * 4); C0 = 0; ivector Mx(N+1,0); int c; fe(i, 1, M) { c = bst_get(T1, 1, 0, M, i); if (c > C0)C0 = c; c = bst_get(T2, 1, 0, M, i); if (c > C0)C0 = c; if (Res[C0] > i)Res[C0] = i; if (A1[i].size() > 0) { f(k, 0, A1[i].size()) { int d = A1[i][k]; Mx[k] = k+1+bst_max(T1, 1, 0, M, i + 1, d); } f(k, 0, A1[i].size()) { int d = A1[i][k]; if (d0) { f(k, 0, A2[i].size()) { int d = A2[i][k]; Mx[k] = k + 1 + bst_max(T2, 1, 0, M, i + 1, d); } f(k, 0, A2[i].size()) { int d = A2[i][k]; if(d= 1; --i) { if (Res[i] > Res[i + 1]) Res[i] = Res[i + 1]; } delete T1; delete T2; } int main(int argc, char **argv) { int T; scanf("%d", &T); f(t, 0, T) { scanf("%d %d", &M, &N); fe(i, 0, M) { A1[i].clear(); A2[i].clear(); } aT.assign(N, 0); aS.assign(N, 0); aD.assign(N, 0); f(i, 0, N) { char buf[20]; scanf("%1s", buf); if (buf[0] == 'E' || buf[0] == 'C')aT[i] = 1; else if (buf[0] == 'D' || buf[0] == 'M')aT[i] = 2; else exit(1); } f(i, 0, N) { int x; scanf("%d", &x); aS[i] = x; } f(i, 0, N) { int x; scanf("%d", &x); aD[i] = x; } f(i, 0, N) { if (aT[i] == 1) { if (aD[i] > aS[i])A1[aS[i]].push_back(aD[i]); } else if (aT[i] == 2) { if (aD[i] > aS[i])A2[aS[i]].push_back(aD[i]); } } fe(i, 1, M) { if (A1[i].size()>1)std::sort(A1[i].begin(), A1[i].end()); if (A2[i].size()>1)std::sort(A2[i].begin(), A2[i].end()); } Res.assign(N+1, M+2); solve(); fe(i, 1, N) { printf("%d%c", (Res[i]==M+2)? (-1):Res[i], (i == N) ? '\n' : ' '); } } return 0; }